DEFAULT CONSTRUCTOR
Constructor with parameters is not considered as the default constructor. It is invoked as soon as object of the class is declared. Any class can have only one Default constructor.
Syntax: class-name object-name;
Even when we didn't declare a constructor in a class, the compiler automatically invokes a default constructors with no arguments.So, if a class has no explicit constructor the compiler supplies a default constructor.
Example:
class student{
int roll_no;
public:
void getdata(void);
void showdata(void);
.
. //Definitions
};
student s1; /*The compiler uses default constructor with no arguments for creating object 's1'. Default constructor is no public member of the class */
void main()
{
s1.getdata();
s1.showdata();
}
No comments:
Post a Comment