Wednesday, May 1, 2019

How to Use Constructor in C++ | Use of constructor in c++ with Example




#include <iostream> using namespace std; class Overclass{
public:
int x,y;
Overclass(){x = y = 0;}
Overclass(int a){x = y = a;}
Overclass (int a, int b){x = a; y = b;} };
int main (){
Overclass A;
Overclass A1(5);
Overclass A2(96, 28);
cout<<"Overclass  A's x, y value:: " <<A.x<<", "<<A.y<<"\n";
cout<<"Overclass  A1's x, y value:: " <<A1.x<<", "<<A1.y<<"\n";
cout<<"Overclass  A2's x, y value:: " <<A2.x<<", "<<A2.y<<"\n";
return 0;


Output:
Overclass  A's x, y value:: 0, 0
Overclass  A1's x, y value:: 5, 5
Overclass  A2's x, y value:: 96, 28



No comments:

Post a Comment