Program to create and display number of object || Static Operator || C++
In this program, we are going to see how to create and display number of object in C++ Programming Language.

The Code given below can be used in TURBO C++ Compilers: -


// C++ program to create Account and display number of objects
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class Account
{
    public:
    int accno; //data member (also instance variable)
    string name;
    static int count;

    Account(int accno, string name)
    {
	this->accno = accno;
	this->name = name;
	count++;
    }
    void display()
    {
	cout << accno << " " << name << endl;
    }
};

int Account::count = 0;

void main()
{
    system("cls");
    Account a1 = Account(201, "Sanjay"); //creating an object of Account
    Account a2 = Account(202, "Nakul");
    Account a3 = Account(203, "Ranjana");
    a1.display();
    a2.display();
    a3.display();
    cout << "Total Objects are: " << Account::count;
    getch();
}
//.........Coded by YASH ALAPURIA

    

The Code given below can be used in gcc/g++ Compilers: -


// C++ program to create Account and display number of objects
#include<iostream>
#include<stdlib.h>
using namespace std;

class Account
{
    public:
    int accno; //data member (also instance variable)
    string name;
    static int count;

    Account(int accno, string name)
    {
        this->accno = accno;
        this->name = name;
        count++;
    }
    void display()
    {
        cout << accno << " " << name << endl;
    }
};

int Account::count = 0;

int main(void)
{
    system("cls");
    Account a1 = Account(201, "Sanjay"); //creating an object of Account
    Account a2 = Account(202, "Nakul");
    Account a3 = Account(203, "Ranjana");
    a1.display();
    a2.display();
    a3.display();
    cout << "Total Objects are: " << Account::count;
    return 0;
}
//.........Coded by YASH ALAPURIA

    

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post