***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Monday, April 26, 2021

TYPES OF CONSTRUCTORS

The constructors in C++ can be classified as

  •  Default Constructors     
  • Parameterized Constructors   
  • Copy Constructors

1. Default Constructors

A constructor that does not take any parameter (argument) is called a default constructor. It simply allocates storage for the data members.

Ex.                    

//Demonstration on Default constructor

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int n;

public:

sample()

{ n=100; }

void show()

{

cout<<"\n N= "<<n;              

}                                                                                                                        

};                                                                                                                                                  

void main()                                                                                                                                   

{                                                                                                            

sample s;                                                                                                                                       

s.show();                                                                                                                                       

}                                                                                                                                                   

2. Parameterized Constructors:-

A constructor that is defined with parameters is called a parameterized constructor. It is used to initialize the data members of a class.

For a parameterized constructor, the data will be sending through that class object declaration only, because constructors are executed by the object declaration.

Ex. //Demonstration on parameterized constructor

#include<iostream.h>

#include<conio.h> class sample

{

private: int n; public:

sample(int k)

{

 n=k;

}

void show()

{

cout<<"\n N= "<<n;

}

};                                                            

void main()                                             

{                                                             

clrscr();

sample s1(50);

s1.show();

getch();

}

3. Copy Constructors

It is used to copy the data values of one object into the members of another object. This constructor takes an object of the class as an argument.

It takes only one argument, also known as one argument constructor.

It creates a new object from an existing one by initialization. A copy constructor takes a reference to an object of the same class as an argument.

Ex.

//Demonstration on copy constructor

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int n;

public:

sample(int k)

{

 n=k;

}

sample(sample &s)

{

n=s.n;

}

void show()

{

cout<<"\n N= "<<n;

}

};                                                                                                                                  

void main()                                                                                                                   

{                                                                                                                                   

clrscr();

sample s1(50);                                                                                                              

s1.show();                                                                       

sample s2(s1);                                                                                                              

s2.show();                                                                          

getch();                                                                                                                         

}

No comments:

Post a Comment