by Dinesh Thakur Category: Classes in C++

 

#include <iostream.h>

#include<conio.h>
class MyClass
{
     public:
     int x;
      int y;
     // Overload the default constructor.
                MyClass()
           {
               x = y = 0;
           }
   // Constructor with one parameter.
               MyClass(int i)
          {
              x=y= i;
          }
  // Constructor with two parameters.
             MyClass(int i, int j)
        {
             x=i;
             y=j;
        }
};
       void main()
    {
             clrscr();
             MyClass t; // invoke default constructor
             MyClass t1 (5); // use MyClass(int)
             MyClass t2(9, 10); // use MyClass(int, int)
             cout<< "t.x: " << t.x<< ", t.y: " <<t.y<< "\n";
             cout << "t1.x: " << t1.x << ", t1.y: " << t1.y << "\n";
             cout << "t2.x: " << t2.x << ", t2.y: " <<t2.y << "\n";
             getch();
     }

Overload The Constructor

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article