Home | Computer Science
<
Constructor
Defination
A special method that is used to initialize objects.
Constructor
Syntax of it is
class abc{
abc();
};
abc::abc{
}
};
Four main rules of Constructor
- The constructor's and class's name must be same.
- It is only used for initialization.
- No keyword will used before constructor
- There is no use to call it.
Basic Program
INPUT
#include <iostream>
#include<conio.h>
class abc{
private:
int a,b,c;
public:
abc();
void display();
};
abc::abc{
a=10;
b=20;
c=30;
}
void abc::display{
cout<<a<<endl<<b<<endl<<c;
}
main() {
abc a1;
a1.display();
getch();
}
OUTPUT