Constructors in C++

Constructors in C++ with proper examples and explanation

 


What is constructor? What are the types of Constructors?

Constructors

In this article, You will learn about Proper explanation and examples of Constructors and destructors in C++. Before going to that, We will discuss about Different types of Member functions of a class in C++.

If you find this article helpful, Then share this article with your friends, Family Members and relatives.


The member function of a class is of 3 types,

  1. Accessor Function / getter function
  2. Mutator Function / setter function
  3. Manager Function

ALSO SEE:Static Members of a Class in C++

(1).Accessor Function: Those member functions of a class that are allowed to access the data members but not allowed to change the values of data members.

(2).Mutator Function: Those member functions that are allowed to change the value of data members of a class.

(3).Manager Function: Those member functions of a class that automatically allocate or deallocate the memory for an object.

For example:- Constructors and Destructors.

Consider the following example:-

class box

 {

  int l,b;

  public:void change()    //Mutator Function

   {

    l=l+1;

    b=b+1;

   }

  void input()    //Accessor Function

   {

    cin>>l>>b;

   }

  void output()

   {

    cout<<l<<“,”<<b;

   }

 };

What is Constructor?

Constructors are those member functions of a class which are used to initialize the object automatically.

Features of constructors,

  • They are called automatically by the compiler whenever the object is created.
  • They are having same name as that of class.
  • They are not having any return type not even void.
  • A constructor may or may not be parameterized.
  • A constructor should be created in public visibility mode.

Types of constructors

  1. Default constructors
  2. Parameterized constructors
  3. Copy constructors

(1).Default Constructors: Those constructors which are not having any parameter.

If this constructor is not created by user then by default it is created by the compiler and the object will be initialized by some garbage value.

Syntax,

class name()

 {

  //body

 }

For example:-

//required header files

class box

 {

  int l,b;

  public:box()    //function 1

   {

    l=1;

    b=2;

   }

  void display()    //function 2

   {

    cout<<l<<“,”<<b<<endl;

   }

 };

void main()

 {

  box b1;    //called function 1

  b1.display();

  getch();

 }

OUTPUT

1,2

ALSO SEE:Top 10 Programming Languages for Future Demand

(2).Parameterized constructors: Those constructors which posses parameters.

Syntax

class name(parameters)

 {

  //body

 }

For example:-

//Required header files

class box

 {

  int l,b;

  public: box()    //function 1

   {

    l=1;

    b=1;

   }

  box(int i, int j)    //function 2

   {

    l=1;

    b=j;

   }

  box(int i)    //function 3

   {

    l=i;

    b=1

   }

 };

void main()

 {

  box b1;    //called function 1

  box b2(4,5);    //called function 2

  box b3(5);    //called function 3

}

NOTE: In the above example, the class containing more than one constructors which is known to be constructor overloading(which implements Polymorphism in C++.

We can call Parameterized constructor in two ways,

  1. box b1(4,5);    //implicit call to constructor
  2. box b1=box(4,5);    //explicit call to constructor

(3).Copy constructor: This constructor is used to copy the values of one object in another object.

Syntax

classname(classname &objectname)

 {

   //body

 }

For example:-

Example 1

//required header files

class box

 {

  int l,b;

  public:box()    //function 1

   {

    l=1;

    b=2;

   }

  box(box &B)    //function 2

   {

    l=B.l;

    b=B.b;

   }

 };

void main()

 {

  box B1;    //call function 1

  box B2=B1; OR box B2(B1);    //call function 2

 }

Example 2

//required header files

class student

 {

  int rno,marks;

  public:student()

   {

    rno=1;

    marks=100;

   }

  student(student &s)

    {

    rno=s.rno;

    marks=s.marks;

   }

 };

void main()

 {

  student s1;

  student s2(s1);

 }

NOTE: In case of copy constructor, the object should be passed with reference sign in parameter, if the reference sign is not there, then it will call itself until the program run out of memory.

Constructors with default arguments

Consider the following example,

//Required header files

class student

 {

  int rno,marks;

  public:student(int r=1,int m=100)

   {

    rno=r;

    marks=m;

   }

 };

void main()

 {

  student s1(2,30);

  student s2;

 }

If in a class, there is a Parameterized constructor with all its arguments default, then it also works as a default constructor. Therefore, a class never contains these two constructors at same time.

What is Destructor?

A destructor is a member function of a class which is used to deinitialize the object when the scope of object gets over.

Features of Destructor

  • They are the member function of a class having same name as that of class.
  • They have no return type(not even void).
  • A destructor is created with tilde sign(~).
  • Destructor is created in public visibility mode.
  • Through destructor, object is deinitialized automatically by the compiler.

ALSO SEE:Javacript Conditional Statements: Learn JavaScript if, else if, else

Syntax

~classname()

 {

  //body

 }

For example:-

//required header files

class box

 {

  int l,b;

  public:box()    //function 1

   {

    l=1;

    b=1;

   }

  ~box()    //function 2

   {

    cout<<“destructor called:”;

   }

 };

void main()

 {

  box b1;    //called function 1

.

.

.

 }    //called function 2 because here, scope of b1 gets over

If you have any query related to this topic, then you can drop your query in the comment section or you can contact us by going to our contact us form.

Follow and like us:
error

1 comment on “Constructors in C++ with proper examples and explanation

Leave a Reply

Your email address will not be published. Required fields are marked *