Copy constructor, Multiple Constructor, Constructor Overloading

Copy constructor:-

In C++ language copy constructor is used to initialize data members of one class object from another class object. It means, if programmer wants to copy initialization of one class object into another class object then he/she can use copy constructor. This process is called copy initialization. For this purpose, copy constructor will take reference of an object of same class as input. 
C++ में कॉपी कंस्ट्रक्टर द्वारा किसी क्लास के डाटा मेम्बर्स को दुसरे ऑब्जेक्ट के डाटा मेम्बर्स के प्रारंभिक मान प्रदान किये जाते है अर्थात यदि प्रोग्रामर एक क्लास ऑब्जेक्ट के प्रारंभिक मानों को दुसरे ऑब्जेक्ट में कॉपी करना चाहता है तब वह कॉपी कंस्ट्रक्टर का प्रयोग करता है। यह प्रक्रिया कॉपी इनिशियलाईजेशन कहलाती है। इस हेतु कॉपी कंस्ट्रक्टर, उसी क्लास के ऑब्जेक्ट का रिफरेन्स इनपुट के रूप में ग्रहण करता है।    

Syntax:-
public:
class_name (class_name&object_name){
statements/initialization;
..............
..............
       };
Constructor Overloading:-

Constructor overloading is a special concept in which a class has two or more different types of constructors to perform different jobs.
कंस्ट्रक्टर ओवरलोडिंग, एक विशेष सिद्धांत है जिसमे एक क्लास के द्वारा विभिन्न कार्यो को पूर्ण करने के लिए दो या दो से अधिक प्रकार के कंस्ट्रक्टर रखे जाते है 

C++ programs for Copy Constructor/ Multiple Constructor/ Constructor Overloading :-

1.)  C++ program for addition class constructor overloading.
#include<iostream.h>
#include<conio.h>
class addition{
float a,b,c;
public:
//default constructor
addition(){
a=0;b=0;
c=0;
}
//parameterized constructor
addition(float m,float n,float o){
a=m;
b=n;
c=o;
}
//copy constructor
addition(addition &ob1){
a=ob1.a;
b=ob1.b;
c=ob1.c;
}
void display();
void addab();
};
void addition::display(){
cout<<"a="<<a<<endl<<"b="<<b<<endl<<"addition c="<<c<<endl;
}
void addition::addab(){
cout<<"Enter value of a and b"<<endl;
cin>>a>>b;
c=a+b;
cout<<"Additon Completed"<<endl;
}
int main(){
addition x,y(3.2,5.5,8.7);
addition z(x),w(y);
clrscr();
x.display();
y.display();
z.display();
w.display();
getch();
x.addab();
y.addab();
z.addab();
w.addab();
getch();
x.display();
y.display();
z.display();
w.display();
getch();
return 1;
}

2.)  C++ program for rational class constructor overloading.

#include<iostream.h>
#include<conio.h>
class rational{
int x,y;
public:
rational(){
x=y=0;
}
rational(int a){
x=y=a;
}
rational(int a,int b){
x=a;
y=b;
}
rational(rational &c){
x=c.x;
y=c.y;
}
rational sum(rational c);
void show();
};
rational rational::sum (rational c){
rational c3;
c3.x=x*c.y+c.x*y;
c3.y=y*c.y;
return c3;
}
void rational::show(){
cout<<"rational number is:"<<endl<<x<<"/"<<y<<endl;
}
void main(){
rational A,B(4),C(7,5);
rational D(B);
clrscr();
A.show();
B.show();
C.show();
D.show();
getch();
A=B.sum(C);
cout<<"Addition =";
A.show();
getch();
}

3) C++ program for rational class constructor overloading and addition using friend function. 
#include<iostream.h>
#include<conio.h>
class rational{
int x,y;
public:
rational(){
x=y=0;
}
rational(int a){
x=y=a;
}
rational(int a,int b){
x=a;
y=b;
}
rational(rational &c){
x=c.x;
y=c.y;
}
friend rational sum(rational c1, rational c2);
friend void show(rational c);
};
rational rational::sum (rational c1 , rational c2){
rational c3;
c3.x=c1.x*c2.y+c2.x*c1.y;
c3.y=c1.y*c2.y;
return c3;
}
void rational::show(rational c){
cout<<"rational number is:"<<endl<<c.x<<"/"<<c.y<<endl;
}
void main(){
rational A,B(4),C(7,5);
rational D(B);
clrscr();
show(A);
show(B);
show(C);
show(D);
getch();
A=sum(B,C);
cout<<"Addition =";
show(A);
getch();
}

4.) C++ program for complex class constructor overloading.
#include<iostream.h>
#include<conio.h>
class complex{
float x,y;
public:
complex(){x=y=0;}
complex(float a){
x=y=a;
}
complex(float real,float img){
x=real;
y=img;
}
complex(complex &c){
x=c.x;
y=c.y;
}
complex sum(complex c);
void show();
};
complex complex::sum(complex c){
complex c3;
c3.x=x+c.x;
c3.y=y+c.y;
return c3;
}
void complex:: show(){
cout<<"complex number is :"<<endl<<x<<"+("<<y<<")i"<<endl;
}
void main(){
complex A,B(2),C(3,-5);
complex D(B);
clrscr();
//Display initial values of objects.
A.show();
B.show();
C.show();
D.show();
getch();
A=B.sum(C);
A.show();
getch();
}

5.) C++ program for complex class constructor overloading and addition using friend function. 
#include<iostream.h>
#include<conio.h>
class complex{
float x,y;
public:
complex(){x=y=0;}
complex(float a){
x=y=a;
}
complex(float real,float img){
x=real;
y=img;
}
complex(complex &c){
x=c.x;
y=c.y;
}
friend complex sum(complex c1, complex c2);
void show();
};
complex complex::sum(complex c1, complex c2){
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;
}
void complex:: show(){
cout<<"complex number is :"<<endl<<x<<"+("<<y<<")i"<<endl;
}
void main(){
complex A,B(2),C(3,-5);
complex D(B);
clrscr();
//Display initial values of objects.
A.show();
B.show();
C.show();
D.show();
getch();
A=B.sum(C);
A.show();
getch();
}

No comments:

Post a Comment