Question

In: Computer Science

Create a C++ class with a static member item so that whenever a new object is...

Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need ANYTHING to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: If you want how many objects are created from within constructor (ie when an object is created, without explicitly invoking the getObjects() method from outside.), let me know.

#include<iostream>

using namespace std;

//a class representing one Student

class Student{

                //attribute for storing Student's name

                string name;

public:

                //static variable storing number of objects created

                static int objects;

                //constructor taking student name

                Student(string nm){

                                //assigning name

                                name=nm;

                                //incrementing number of objects created

                                objects++;

                }

                //you can create a destructor here if you want that will decrement objects by 1

                //in that way, you can find out the number of active objects (under scope), right

                //now, I'm just leaving it upto you.

               

                //returns the name

                string getName(){

                                return name;

                }

                //static method to return the number of objects created

                static int getObjectsCreated(){

                                return objects;

                }

};

//initializing static member variable objects to 0

int Student::objects=0;

//main method for testing

int main(){

                //displaying number of objects created initially (should be 0)

                cout<<"Number of Student objects created: "<<Student::getObjectsCreated()<<endl;

                //creating a Student, now the value must be 1

                Student s1("John");

                cout<<"Number of Student objects created: "<<Student::getObjectsCreated()<<endl;

                //creating another Student, now the value must be 2

                Student s2("James");

                cout<<"Number of Student objects created: "<<Student::getObjectsCreated()<<endl;

                //creating another Student, now the value must be 3

                Student s3("Oliver");

                cout<<"Number of Student objects created: "<<Student::getObjectsCreated()<<endl;

                //note that static members can be called using class name (no need to create an object)

                //you can also access objects field by Student::objects since objects is public

                //also, there is no problem calling using an object. i.e s3.objects or s3.getObjectsCreated()

                //are both valid statements

                return 0;

}

/*OUTPUT*/

Number of Student objects created: 0
Number of Student objects created: 1
Number of Student objects created: 2
Number of Student objects created: 3


Related Solutions

public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
Class object in C++ programming language description about lesson static variable example.
Class object in C++ programming language description about lesson static variable example.
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List alarmTimes Create an accessor and mutator Create a method addAlarmTime which adds an alarm time to your list Create a method deleteAlarmTIme which removes an alarm time from your list Create a method – displayAlarmTimes which prints all alarm times in the list - Create a ‘Clock’ class - include at least 2 members - one member of Clock class needs to be private...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
Write a C++ program to show the use of static data member and static member_function. Give...
Write a C++ program to show the use of static data member and static member_function. Give an explanation of the scenario you used to code (e.g. why it is required to use the static members). Upload only a word document file that contains the exact code and its explanation as asked. also write comments against each line of code
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
HW_6d - Write a struct object to a binary file. Create a new C++ project and...
HW_6d - Write a struct object to a binary file. Create a new C++ project and name it as:   Cats Create a Source.cpp file. Declare a struct named Cat. (in the Source.cpp file, or in a separate header file) Each Cat object has a name and age. The name data member is a c_string. The age data member is an integer. Ask the user to enter 3 cats. Use a while loop to read the information about one cat entered...
C++ please Create a Stats class whose member data includes an array capable of storing 30...
C++ please Create a Stats class whose member data includes an array capable of storing 30 double data values, and whose member functions include total, average, lowest, and highest functions for returning information about the data to the client program. These are general versions of the same functions you created for Programming Challenge 7, but now they belong to the Stats class, not the application program. In addition to these functions, the Stats class should have a Boolean storeValue function...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0. The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>. The ContactInfo class should have accessor and mutator functions for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT