A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.
#include<conio.h>
#include<iostream>
using namespace std;
class abc
{
private:
int a=10;
float b=15.3;
public:
abc()
{
cout<<"\n Object is being created: ";
cout<<"\n a="<<a;
cout<<"\n b="<<b;
}
};
int main()
{
abc x;
getch();
return 0;
}
No comments:
Post a Comment