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...
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
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...
Write a C++ programs to: 1. Create a class called Student with four (4) private member...
Write a C++ programs to: 1. Create a class called Student with four (4) private member variables, name (string), quiz, midterm, final (all double type); 2. Include all necessary member functions for this class (at least 1 constructor, 1 get function, and 1 grading function – see #6); 3. Declare three (3) objects of Student type (individual or array); 4. Read from the keyboard three (3) sets of values of name, quiz, midterm, final and assign them to each object;...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT