Saturday 12 August 2017

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 sh



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.

SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class s_num
{
            private:
                        int s_n;
                        static int count;
            public:
                        s_num( )
                        {
count++;
s_n=count;
}
                        void show_s_num( ) const
                        {          
cout << "\"I am Object Number " << s_n << "\"" << endl;
}
};
int s_num::count=0;
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            s_num obj1,obj3,obj2;
            cout << "Obj1 says ";  obj1.show_s_num( );
            cout << "Obj2 says ";  obj2.show_s_num( );
            cout << "Obj3 says ";  obj3.show_s_num( );
            system("pause");
}

OUTPUT:



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.

SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class s_num
{
            private:
                        int s_n;
                        static int count;
            public:
                        s_num( )
                        {
count++;
s_n=count;
}
                        void show_s_num( ) const
                        {          
cout << "\"I am Object Number " << s_n << "\"" << endl;
}
};
int s_num::count=0;
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            s_num obj1,obj3,obj2;
            cout << "Obj1 says ";  obj1.show_s_num( );
            cout << "Obj2 says ";  obj2.show_s_num( );
            cout << "Obj3 says ";  obj3.show_s_num( );
            system("pause");
}

OUTPUT:

0 comments: