Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Thursday, September 20, 2012

How to count the number of objects created of a class

#include <iostream>
using namespace std;

class Demo
{
    public:
        static int totalObject;
       
        Demo()
        {
            totalObject++;
        }
};

int Demo::totalObject(0);    //static variable initialized to 0

int main()
{
    Demo d1;
    Demo d2;
    Demo d3;
    Demo d4;
   
    cout << "\n Total no of object of Demo class is: " << Demo::totalObject << endl;
    return 0;
}


OUTPUT:

Tuesday, May 8, 2012

Program to understand array of object

#include <iostream>
using namespace std;

class info
{
    char name[20];
    int roll;
   
    public:
        void
getinfo()
        {
            cout << "\n Enter name: ";
            cin >> name;
            cout << "\nEnter roll number: ";
            cin >> roll;
        }
   
        void displayinfo()
        {
            cout << "\n\n\tName: " << name;
            cout << "\n\tRollNumber:" << roll;
        }
};

int main()
{
    info obj[5];     //Here i am creating 5 object using array


    for(int i=0; i<5; i++)
    {
        obj[i].getinfo();
    }
   
    for(int i=0; i<5; i++)
    {
        obj[i].displayinfo();
    }
   
    cout << "\n\n";   
}