OOCP (CPP) : Example # 09 : Constructor & Copy Constructor Example using c++
* Lets start learning CPP (C++) Using Example.
* Constructor Example using c++.
//Written by Latest Technology Guide
//Title : OOCP (CPP) : Example # 09 : Constructor & Copy Constructor Example using c++
/*
Copy Constructor
Characteristics of Constructor
- Name of Const and Name of Class must be Same
- No Return Value (not even void)
*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
class Item
{
private:
int item_no;
char *item_name;
public :
Item() //Constructor without para
{
cout << "Cunstructor is called without para" << endl;
}
Item(int a,char *ptr) //Constructor with para
{
cout << "Constructor is called with 2 Para" << endl;
item_no = a;
item_name = ptr;
}
Item(Item &x) //Copy Constructor
{
cout << "Copy constructor is called" << endl;
item_no = x.item_no;
item_name = x.item_name;
}
void display()
{
cout << "Item No :" << setw(10) << item_no << setw(10)
<< "item Name :" << setw(10) << item_name << endl;
}
};
//=================================================
// Main Function
//=================================================
void main()
{
clrscr();
//Constructor with Para
Item a(100,"Ram");
cout << "Object A :===> ";
a.display();
//Copy Constructor
Item b(a);
cout << "Object B :===> ";
b.display();
//Copy Constructor
Item c = a;
cout << "Object C :===> ";
c.display();
//equalto operator overload
Item d;
d.display();
d = b;
cout << "Object D :===> ";
d.display();
getch();
}
Output:
* CPP Constructor & Copy Constructor Example.
--------------------------------------------------------------------------------
OOCP (CPP) : Example # 09 : Constructor & Copy Constructor Example using c++
* Example demonstrate how to work with Constructor & Copy Constructor.
Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements.
--------------------------------------------------------------------------------
OOCP (CPP) : Example # 09 : Constructor & Copy Constructor Example using c++
* Example demonstrate how to work with Constructor & Copy Constructor.
Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements.
.thank you for sharing useful post.
ReplyDeletec++ programming tutorial
welookups
nice article for beginners.thank you.
ReplyDeletejavacodegeeks