C++ Program For Constructors

Free C++ course with real-time projects Start Now!!

Program 1

// Program for Cnstructor
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Test
{
     int n;
    public:
    Test()
    {
        n=100;
    }
    void incr();
    void display();
};
void Test::incr()
{
    ++n;
}      
void Test::display()
{
    cout<<n;
}

int main()
{
    Test T1,T2;
    T1.incr();
    T1.incr();
    T1.incr();
    cout<<"\nT1 Object";
    T1.display();

    cout<<"\nT2 Object";
    T2.display();
}

 

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

1 Response

  1. Abhay Kumar Ray says:

    you great sir

Leave a Reply

Your email address will not be published. Required fields are marked *