There are three types of constructors in c++.
  
  
 
 
 
 
  1) Default constructor
   2)parametric constructor
   3)Copy constructor
Have a look on this simple program.
#include<conio.h>
#include<iostream>
using namespace std;
class student
{
 private:
  int marks;
  double percentage;
 public:
  student()  // default constructor
  {
   marks=0;
   percentage=0.0;
  }
  student(int a, double b)  // Parametric constructor
  {
   marks = a;
   percentage = b;
  }
  student( student &s)  // copy constructor
  {
   marks=s.marks;
   percentage=s.percentage;
  }
  void show()
  {
   cout<<"\n marks ="<<marks;
   cout<<"\n percentage ="<<percentage;
  }
};
int main()
{
 student s1;  // calling default constructor
 s1.show();
 student s2(95,95.6);  // calling parametric constructor
 s2.show();
 student s3(s2);  // calling copy constructor
 s3.show();
 getch();
 return 0; 
}
 
No comments:
Post a Comment