Saturday, 12 November 2016

Create a class that includes a data member that holds a “serial number” for each object created from the class. That is, the first object created will be numbered 1, the second 2, and so on.

Q)Create a class that includes a data member that holds a “serial number” for each object created from the class. That is, the first object created will be numbered 1, the second 2, and so on.To do this, you’ll need another data member that records a count of how many objects have been created so far. (This member should apply to the class as a whole; not to individual objects. What keyword specifies this?) Then, as each object is created, its constructor can examine this count member variable to determine the appropriate serial number for the new object.
Add a member function that permits an object to report its own serial number. Then write a main() program that creates three objects and queries each one about its serial number. They should respond I am object number 2, and so on.


#include<iostream>

using namespace std;
class Serial
{
       static int s; //variable to store the serial no.
public:
       Serial(); //constructor to set serial number

};
int main()
{
       Serial  obj1, obj2, obj3; //creating the object
       system("pause");
       return 0;
      
}

Serial::Serial()//definition of a constructor
{
       s++;
       cout << "I am object Number " << s << endl;
}
int Serial::s = 0; //intialize s to zero

5 comments:

  1. i love it very easy and perfect

    ReplyDelete
  2. Casino | Jackson County, Mississippi - JT Hub
    Contact 속초 출장마사지 Casino 태백 출장샵 at 877-876-3200. Contact 광주광역 출장마사지 Casino at 877-876-3200. JTG's email address is 계룡 출장샵 jtim@scoop.com. 광주 출장안마 JTG's office is

    ReplyDelete
  3. thanks bro.....it helped a loooooooooooottttt!!

    ReplyDelete