by Dinesh Thakur Category: Classes in C++

 

#include <iostream.h>

#include<conio.h>
class MyClass
{
    const int numA; // const member
    const int numB; // const member
    public:
   // Initialize numA and numB using initialization syntax.
   MyClass(int x, int y) : numA(x), numB(y) { }
         int getNumA()
      {
           return numA;
      }
        int getNumB()
     {
         return numB;
     }
};
void main()
  {
       clrscr();
       MyClass object1 (7, 9), object2(5, 2);
       cout << "Values in object 1 are " << object1.getNumB() << " and " <<       
       object1.getNumA() << endl;
       cout << "Values in object 2 are " << object2.getNumB() << " and " <<  
       object2.getNumA() << endl;
       getch();
  }

Assign Values Using The Member Initialization Syntax

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