#include<iostream.h>
#include<conio.h>
class student
{
private:
int sid;
float marks;
public:student();
void showdetails()
{
cout<<"Student Id:"<<sid<<endl<<"Marks:"<<marks;
}
};
student::student()
{
sid=1;
marks=80.0;
}
void main()
{
clrscr();
student s1;
s1.showdetails();
getch();
}
Student Id:1
Marks:80
Now that we know what is constructor, lets discuss how a constructor is different from member function of the class.
1. Constructor doesn’t have a return type. Member function has a return type.
2. Constructor is automatically called when we create the object of the class. Member function needs to be called explicitly using object of class.
3. When we do not create any constructor in our class, C++ compiler generates a default constructor and insert it into our code. The same does not apply to member functions.