• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Constructor overloading in C++ Language

Constructor overloading

Defining more than one constructor within class by changing

–>Number of parameters

–>Types of parameters

–>Order of parameters

is called Constructor Overloading in C++

Constructor is overloaded in order to extend functionality of existing constructor

Constructor Overloading Example

# include<iostream.h>

class  stock

{

int*a;

int size;

int top;

public:

stack();

stack(int);

void push(int);

void pop();

}

stack :: stack()

{

top=-1;

size=5;

q=new int[size];

}

stack :: stack(int s)

{

size=s;

top=-1;

a=new int[size];

void stack :: push (int e)

{

if(top)>size-1;

cout<<”\n stack is full”;

else

a[++top]=e;

}

void stack :: pop()

{

if(top<0)

cout<<”\n stack is empty”;

else

cout<<”\n element is”<<a[top—];

}

void main()

{

stack stack1;

stack  stack2(3);

stack1.push(0);

stack1.push(20);

stack1.pop();

stack2.push(60);

stack2.push(70);

stack2.push();

}

o/p :  void push(int e)

{

if(top>>size-1)

else

a[++top]=e;

}

# include<iostream.h>

#inlude<string.h>

class string

{

char *s;

int size;

public :

string();

string(int);

void read_string();

void print_string();

};

string:: string()

{

size=10;

s=new char[size];

}

string:: string (int s1)

{

size=s1;

s=new char[size];

}

void string :: read-string()

{

cout<<”\n input any string”;

cin>>s;

}

void string:: print_string()

{

cout<<”\n string is”<<s;

}

void main()

{

string string1;

string string2[20];

string1.read_string();

string2.print_string();

string2.print_string();

}

 

Copy Constructor

  • If there is no copy constructor compiler provide copy constructor within class.
  • Copy constructor is a parameterized construct have parameter within class
  • Copy constructor is used to create an object by copying contexts of existing object
  • It is used to create a copy of object

Copy Constructor Syntax

constructor-name(class-name & red-name)

{

staments;

{

}

  • If constructor having parameter of type class, it receive object.
  • copy constructor receive reference of the object
  • It is using method i.e shallow copy and also reference type.

 

Copy Constructor Example

# include<iostream.h>

class date

{

int dd, MM,YY;

public:

data(int,int,int);

date(date&)      //(copy constructor)

void point_date();

};

date:: date(int d, int M, int Y)

{

dd=d;

MM=M;

YY=Y;

}

date:: date(date &d)

{

dd=d.dd;

mm=d.MM;

YY=d.YY;

}

void date:: print_date()

{

cout<<”]n” <<d<<”\t”<<mm<<”\t”<<yy;

}

void main()

{

date dob(10,10,2014);

date cdob(dob);

dob.print_date();

colob.print_date();

}

 

// copy constructor

# include<iostream.h>

# include<string.h>

class strudent

{

int rno;

char name[10];

public :

student(int,char*);

student(student &);

void print_student();

};

student::student(int r,char *n)

{

rno=r;

strcopy(name,s.name);

}

void student::print_student()

{

count<<”\n rollno”<<rno;

cout<<”\n name”<<name;

}

void main()

{

student1 stud1(101,”RONO”);

student1 stud2(stud1);

stud1.print_student();

stud2.print_student();

 

Destructor

Destructor is a special member function

This member function get executed before destroying object.

Destructor Properties

Destructor name is same as class name and it is prefix with ~ operator

It is without any return type not even void

It is not having any parameters/ arguments

A class contain only one destructor.

Destructor Syntax

~destructor-name()

{

statements;

}

DestructorExample

# include <iostream.h>

class alpha

{

int x;

public :

alpha()

{

cout<<”\n inside construtor”;

}

~alpha()

{

cout<<”\n inside destructor”;

}

};

void main()

{

alpha  alpha1;

}

o/p : inside constructor(creating object)

inside Destructor(Deleting object)

Eg:

 

#include <iostream.h>

class array

{

int *a;

int size;

public :

array(int s)

{

size=5;

a=new int[size];

}

void read_elements()

{

cout<<”\n input elements”;

for(int i=0;i<size;i++)

cin>>a[i];

}

void print_elements()

{

cout<<”\n elements are \n”;

for(int i=0;i<size;i++)

cout<<endl<<a[i];

}

~array()

{

cout<<”\n inside destructor”;

delete a;

void main()

{

array array1(5);

array1.read_elements();

array1.print_elements();

}

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Constructor overloading in C++ Language"

    Leave a Message

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

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.