UNIT - II
Classes and Objects
Course Title : Object Oriented Programming
using C++
Course Incharge: Prof. Bhagyashali Jadhav
       COURSE OUTCOMES (COs)
a. Develop C++ programs to solve problems using
  Procedure Oriented Approach
b. Develop C++ programs using classes and
  objects.
c. Implement inheritance in C++ program.
d. Use polymorphism in C++ program.
e. Develop C++ programs to perform file operations.
            Classes and Objects
Class Definition:
1. A class is a blueprint for the object.
1. Class is a user-defined data type, which holds
   its own data members and member functions,
   which can be accessed and used by creating an
   instance of that class.
For Example:
 Consider the Class of Cars. There may be many
cars with different names and brand but all of
them will share some common properties like all
of them will have 4 wheels, Speed Limit, Mileage
range etc.
So here, Car is the class and wheels, speed
limits, mileage are their properties (data
member) and member functions can be apply
brakes, increase speed etc.
➢Class allows the data members and
 member functions to be hidden from
 external use.
➢An Object is an instance of a Class. When a
 class is defined, no memory is allocated but
 when it is instantiated (i.e. an object is
 created) memory is allocated.
● A class specification has two parts:
1) Class Declaration
2) Class function definition
General form of class declaration is
 Class class_name
{
   private:
   Variable declaration;
   Function declaration;
   public:
   Variable declaration;
   Function declaration;
};
For eg.-
 class student
{
 int rollno;
 char name[30];
 public:
 void getdata();
 void putdata();
};
• The keyword class specifies, that class_name
• Body of a class is enclosed within braces and
  terminated by semicolon.
• Class body contains declaration of variables
  and functions
• These variabes are collectively called as class
  members
• The keyword PUBLIC and PRIVATE are known
  as visibility labels/modes
• Default visibility mode is PRIVATE.
Difference between structure and
              class
        Structure                     Class
1) It Collection of        1) It is collection of
  variables of different     variables of different
  data types                 data types and
2)'struct' keyword is        functions.
  used while declaring     2) 'class' keyword is
  a structure                used while declaring
3)Structure variables        a class.
  are created.             3) Class variable can
                             be created which
4)Functions are not       4) Functions are not
  enclosed in structure     enclosed in class
  definition                definition called as
5)No private and            member functions.
  public member           5)Data members and
  declaration present       member functions
  in structure.             can be defined as
6)Data hiding is not        public, private and
  achieved.                 protected.
                          6)Data hiding is
                            achieved with
                            private keyword.
7)Syntax:    7)Syntax:
Struct tag   Class class_name
{            {
member1;     private:
member2;        Variable declaration;
.               Function declaration;
.            public:
memberN         Variable declaration;
}               Function declaration;
             };
Disadvantages of structure
•Standard C does not treat structure as an built-
in data type
•Suppose we have declared C1,C2,C3 as object
of above class at that time we can not perform
operation like
C3=C1+C2
•Structure do not permit data hiding. All
structure members are public members.
 Structures in C++
• C++ support all the features as defined in C, but C++ has
  some expanded capabilities such as data hiding,
  Inheritance.
• In C++ a structure can have both function and variable
  as member(declaration)
• It also declare some members as private so that they
  cannot accessed directly by external function.
• C++ incorporates all these extension in another user
  defined type known as Class.
 What is Class in C++
• A class is a way to bind data and its associated
  function together.
• It allows the data and function to be hidden, if
  necessary.
• When defining class we are creating new abstract
  data type that can be treated as other built-in
  data type
Example
Creating Object
Syntax
Class_name object_name;
Example: item x;
            OR
item x,y,z    //multiple object
We can also create object after class declaration as
we do for structures(i.e declaring after semicolon)
     Defining member function
Member function can be defined in two different
way:
(1) Inside class
(2) Outside class
(1) Inside Class:
When we declare the function in the class at the same
 time we can also give the definition of the function in
 the class as shown below:
class Test{
int a,b;
public:
void input (){
cout<<"Enter Value of a";
cin>>a>>b;
}
};
The function defined inside class becomes inline by
  default.
Outside definition:
•An important difference between a member
function and normal function is that a member
function incorporates a membership ‘identity label’
in the header i.e class_name::
•This ‘label’ tells the compiler which class the
function belongs to.
Syntax
Example
Characteristics of Member function are:
•Several different classes can use the same
function name. the membership label will resolve
their scope.
•Member function can access private data of the
class
•The member function can call another member
function directly, without using the dot operator
Inside class definition:
•Another method of defining member function is
to replace the function declaration by the actual
function definition.
•When function is defined inside class is called as
inline function
•Normally small functions are defined inside a
class.
class Test
{
int a,b;
public:
void input ()
{
cout<<"Enter Value of a";
cin>>a>>b;
}
};
                    Inline Function
●   One of objective of using functions in a program is to save
    memory space.
●   Every time a function is called, it takes a lot of extra time
    in executing a series of tasks such as jumping to the
    function, pushing arguments, returning to calling function.
●   C++ has solution to this problem—inline finction.
●   An inline function is a function that is expanded in line
    when it is invoked.
●   That is compiler replaces the function call with the
    corresponding function code.
Making outside function inline:
      when we define member function outside the
class definition and still make it inline by just using
the qualifier inline in the header line of function
definition
             Accessing class members
● Private members of a class can be accessed by
  member functions of that class.
● Syntax for calling public members
object_name.function_name(actual_arguments);
eg.-- x.getdata(100,12.25);
x.rollno=10;
#include<iostream.h>
#include<conio.h>
class student{
int rollno;
void get();
public:
int marks;
void put();
};
void student::get(){
Rollno=5;
Marks=88;
 }
void student::put(){
get();
cou<<rollno;
cout<<marks;
}
void main(){
student s;
s.rollno=5; //error
s.marks=88;
s.get(); //error
s.put()
}
●   Members declared as public can be accessed by
    objects.
●   A private member function can be called by
    another function that is a member of its class.
●   An object cannot invoke a private function
    using the dot operator
Prg1- WAP to declare a class 'student' having data
     members as stud_id, name, roll_no. Accept and display
     this data for one object.
#include<iostream.h>
#include<conio.h>
class student{
int stud_id, roll_no;
char name[30];
public:
void getdata();
void putdata();
};
void student::getdata(){
cout<<”Enter student ID, roll number and name”;
cin>>stud_id>>roll_no>>name;
}
void student :: putdata(){
 cout<<”\n Student ID”<<stud_id;
cout<<”\n Student Roll Number”<<roll_no;
cout<<”\n Student Name”<<name;
}
void main(){
student s;
clrscr();
s.getdata();
s.putdata();
getch();
}
Prg 2-- WAP to declare a class 'Journal' having data members as
   journal_nm, price, ISSN_No. Accept this data for two objects
   and display the name of journal having greater price.
#include<iostream.h>
#include<conio.h>
 class journal{
public:
char journal_nm[20];
int price, ISSN_No;
void accept(){
cout<<”Enter journal name, price and ISSN number”;
cin>>journal_nm>>price>>ISSN_No;
}
};
void main(){
journal j1,j2;
clrscr();
j1.accept();
j2.accept();
if(j1.price>j2.price)
cout<<j1.journal_nm<<”has greater price”<<j1.price;
else
cout<<j2.journal_nm<<”has greater price”<<j2.price;
getch();
}
J1---Enter journal name, price and ISSN number
A
10
112
J2---Enter journal name, price and ISSN number
B
20
445
B has greater price 20
Prg 3-- wap to declare a class 'Day' having data members as
  hours, min, sec. Accept min from user convert in
  appropriate hours, min and sec. Display it for one object
  of a class.
#include<iostream.h>
#include<conio.h>
class day{
int min;
public:
void accept(){
cout<<”enter minute”;
cin>>min;
}
void display(){
cout<<”seconds=”<<min*60;
cout<<”minute=”<<min;
cout<<”hours=”<<min/60;
}};
void main(){
day d;
clrscr();
d.accept();
d.display();
getch();
}
Nesting of member function--
A member function can be called by using its name inside
  another function of the same class. This is known as
  nesting of member functions.
eg.-- #include<iostream.h>
#include<conio.h>
class set{
int m,n;
public:
void input();
void display();
int largest();
};
void set::input(){
cout<<”Input value for m and n”;
cin>>m>>n;
}
void set::display(){
cout<<”Largest value=”<<largest();
}
int set::largest(){
if(m>n)
return(m);
else
return(n);
}
void main(){
set A;
A.input();
A.display();
getch();
}
      Memory allocation for object
• The memory space for object is allocated when they are
  declared and not when the class is specified.
• Member function are created and placed in the memory space
  when they are defined.
• Since all the objects belonging to that class use the same
  member function,no separate space is allocated for member
  functions when the objects are created
               Array of Object
• Array of a Variables that are of type class are
  called array of object.
Syntax:
classname objectname[size];
Example:
employee e[3];
(three objects e[0], e[1], e[2] are created)
Prg.1—WAP to declare a class 'student' having data
members rollno and name. Accept and display this data for
three objects.
#include<iostream.h>
#include<conio.h>
class student{
int rollno;
char name[30];
public:
void get();
void put();
};
void student::get(){
cout<<”Enter rollno and name”;
cin>>rollno>>name;
}
void sudent::put(){
cout<<”Roll Number and name:”<<rollno<<name;
}
void main(){
student s[3];
for(int i=0;i<3;i++)
  s[i].get();
for(int i=0;i<3;i++)
  s[i].put();
getch();
}
.
                 get()                     put()
             S[0]]            S[1]          S[2]]
    rollno      name     rollno   name   rollno     name
Prg2—WAP to declare a class 'staff' having data members as
name and department. Accept this data for 10 staffs and
display names of staff that are in IT department.
#include<iostream.h>
#include<conio.h>
class staff{
char name[20], dept[20];
public:
void get(){
cout<<”Enter name and department”;
cin>>name>>dept;
}
void put(){
if(strcmp(dept,'IT')==0)
  cout<<name;
}};
void main(){
staff s[10];
for(int i=0;i<10;i++)
s[i].get();
for(int i=0;i<10;i++)
s[i].put();
}
Prg3—WAP to declare a class 'account' having data members
as account no. And balance. Accept this data for 10 accounts
and give interest of 10% where balance is equal or greater
than 5000 and display them.
#include<iostream.h>
#include<conio.h>
class account{
public:
int acc_no, balance;
void get(){
cout<<Enter account no and balance”;
cin>>acc_no>>balance;
}
void put(){
if(balance>=5000)
 cout<<acc_no<<”has balance”<<balance+(balance*10/100);
}};
void main(){
account A[10];
clrscr();
for(int i=0;i<10;i++)
A.get();
for(int i=0;i<10;i++)
A.put();
getch();
}
            Object as function argument
Object can be used as function argument and this is done
 by two ways.
1. A copy of the entire object is passed to the function
2.Only the address of the object is transferred to the
  function
  1. A copy of the entire object is passed to the
                       function
●   The first method is called pass-by-value
●   A copy of a object is passed to the function
   any changes made to the object inside the
   function do not affect the object used to call
   the function
  2.Only the address of the object is transferred
                     to the function
●   The second method is called pass-by-
   reference.
●   When an address of the object is passed, the
   called function directly work on actual object
   used in the call.
●   Any changes made to the object inside the
   function will reflect in the actual object.
Prg 1—WAP to declare a class 'book' having data members as
book_name,price and no_of_pages. Accept this data for two
objects and display the name of book having greater price.
#include<iostream.h>
#include<conio.h>
class book{
int no_of_pages, price;
char book_name[20];
public:
void accept();
void display(book, book);
};
void book::accept(){
cout<<”Enter book name,no of pages and price”;
cin>>book_name>>no_of_pages>>price;
}
Void book::display(book b1, book b2){
if(b1.price>b2.price)
 cout<<b1.name<<”has greater price as”<<b1.price;
else
cout<<b2.name<<”has greater price as”<<b2.price;
}
void main(){
book b1,b2;
b1.accept();
b2.accept();
b1.display(b1,b2);
getch();
}
                  Static data members
Following are the characteristics of static member
●    It is initialized to zero when the first object of its class is
    created. No other initialization is permitted
●    Only one copy of that member is created for the entire
    class and is shared by all the object of that class, no
    matter how many objects are created.
●   It is visible only within class, but its lifetime is the entire
    program.
●   Static variables are used to maintain values common to
    the entire class.
●Each static variable must be defined outside the class
definition.
●Static data members are stored seperately rather than as a
part of an object.
●Since they are associated with the class itself rather than
with any class object, they are also known as class variable.
Syntax:1)Inside the class:
static datatype variblename;
Eg:static int a;
2)outside the class:
return_type classname::variablename;
eg-int student::a;
int student::a=5;
Example:
#include<iostream.h>
#include<conio.h>
class item{
static int count;
public:
void DisplayCounter() {
count++;
cout<<"count:"<<count;
}
};
int item::count;
int main()
{
item a,b,c;
a.DisplayCounter();
b.DisplayCounter();
c.DisplayCounter();
return 0;
}
Output:
Count: 1
Count: 2
Count: 3
           Static member function
●   Like a static member variable we can also have static
    member function. A member function declared with static
    keyword is called as static member function. static
    member function has following characteristics:
1.Static function can have access to only other static
   members/functions declared in same class
2.Static member function can be called using class
   name(instead of object) as
Class_name:: function_name;
Example:
#include <iostream.h>
#include <conio.h>
class test {
private:
static int count;
public:
void setCount();
static void DisplayCounter();
};
void test :: setCount(void)
{
count++;
}
void test :: DisplayCounter(void)
{
cout<<"Count:"<< count;
}
int test::count;
int main(void){
test t1, t2;
clrscr();
t1.setCount();
test :: DisplayCounter();
t2.setCount();
test :: DisplayCounter();
getch();
return(0);
}
Output: Count: 1
            Count: 2
Prg 1:WAP to calculate weight of object at different planets
    by using formula weight=m*g where m=mass of object
    and g=gravitational force. Declare g as static member
    variable.
#include<iostream.h>
#include<conio.h>
class weight{
float m;
static float g;
public:
void get();
void put();
};
int weight::g=9.8;
void weight::get(){
cout<<”Enter mass of planet”;
cin>>m;
}
void weight::put(){
cout<<”Weight of planet:”<<m*g;
}
void main(){
weight w;
w.get();
w.put();
getch();
}
Prg 2—WAP to define a class having data members principle,
  duration and rate of interest. Declare rate of interest as
  static member variable calculate the simple interest and
  display it.
#include<iostream.h>
#include<conio.h>
class SI{
static int r;
int p,d;
public:
void get();
void put();
};
int SI::r=8;
void SI::get(){
cout<<”Enter principle and duration”;
cin>>p>>d;
}
void SI::put(){
cout<<”Simple Interest:”<<p*n*r;
}
void main(){
SI s;
s.get();
s.put();
getch();
}
                 Friend Function
• A friend function is a function that is not a
  member of a class but it can access private
  and protected member of the class in which
  it is declared as friend.
• Since friend function is not a member of
  class it can not be accessed using object of
  the class.
• Sometimes it is required that private
A function can be declared as a friend by
   preceding function declaration with friend
   keyword as shown below:
friend Return_Type Function_Name (Argument
   List);
Eg: Friend void add();
Friend function can be called as foll
function_name(actual arguments);
• The function that is declared with the keyword
  friend is known as friend function
• This function can be defined else where in the
  program.
• The function definition does not use either keyword
  friend or the scope operator::
• A function can be declared as a friend in any
  number of classes.
• A friend function although not a member function,
  has full access right to the private members of the
  class
Friend function having following characteristics:
(1) A friend function can be declared inside class but it is not
   member of the class.
(2) It can be declared either public or private without
   affecting its meaning.
(3) A friend function is not a member of class so it is not
   called using object of the class. It is called like normal
   external function.
(4) A friend function accepts object as an argument to access
   private or public member of the class.
(5) A friend function can be declared as friend in any number
   of classes.
Example:
#include<iostream.h>
#include<conio.h>
class Circle {
int r;
public:
void input(){
cout<<"Enter Radius:";
cin>>r;
}
friend void area(Circle C);
};
void area(Circle C)
{
cout<<"Area of Circle is:"<<3.14*C.r*C.r;
}
void main()
{
Circle c1;
c1.input();
Area(c1);
Getch();
}
Prg 1-- WAP to create two classes test1 and test2 which
stores marks of a student. Read values for class objects and
calculate average of two tests.
#include<iostream.h>
#include<conio.h>
class test1;
class test2{
int m2;
public:
 void accept(){
cout<<”Enter test2 marks:”;
cin>>m2;
}
friend void avg(test1,test2);
};
class test2{
int m1;
public:
void accept(){
cout<<”Enter test1 marks:”;
cin>>m1;
}
friend void avg(test1,test2);
};
void avg(test1 t1,test2 t2){
cout<<”\n Average Marks:”<<(t1.m1+t2.m2)/2;
}
void main(){
test1 t1;
test2 t2;
clrscr();
t1.accept();
t2.accept();
avg(t1,t2);
getch();
}
         Constructor in C++
Constructor is a member function of the class.
It is called special member function because
the name of the constructor is same as name
of the class.
Constructor is used to construct the values for
the data member of the class automatically
when the object of class is created.
class rectangle {
 private:
   float height;
   float width;
   int xpos;
   int ypos;
 public:
   rectangle(float, float); // constructor
   void draw();            // draw member function
   void posn(int, int);     // position member function
   void move(int, int);     // move member function
};
 rectangle::rectangle(float h, float w)
{
 height = h;
 width = w;
 xpos = 0;
 ypos = 0;
Like other member function there is no need to
call constructor explicitly. It is invoked
automatically each time the object of its class is
created.
Every class having at least one constructor
defined in it. If you do not define any constructor
in the class then compiler will automatically
create a constructor inside the class and
assigns default value (0) to the data member of
the class.
    Types of constructor--
1)   Default constructor
2)   Parameterised constructor
3)   Copy constructor
4)   Dynamic constructor
        Default Constructor.
A constructor that accepts no parameters is
called the default constructor.
If default constructor isnot defined in program
then the compiler supplies a default
constructor.
      Constructor declaration
class student
{
 int rollno;
 char name[20];
 public:
  student();
}
Constructor definition--
student::student(){
rollno=0;
name=”Priya”;
}
Constructor calling--
void main(){
student s;
....
}
Constructor can be defined inside class as shown
   below:
class Rectangle
{
int Height, Width;
public:
Rectangle()
{
Height = 1;
Width = 1;
}
};
Constructor can be defined outside class as
 shown below:
class Rectangle
{
int Height, Width;
public:
Rectangle();
}
Rectangle :: Rectangle()
{
Height = 1;
Width = 1;
}
Now when you create an object of the class
 Rectangle as shown below:
Rectangle R1;
It will invoke constructor and assigns value 1 to its
   data member Height and Width.
The constructor that does not accept any
  argument is known as default constructor.
  Characteristics of constructor
 (1) It is called automatically when object of its
  class is created.
(2) It does not return any value.
(3) It must be declared inside public section of
  the class.
(4) It can have default arguments.
(5) Inheritance of constructor is not possible.
(6) It can not virtual.
Disadvantage of default constructor is that each
  time an object is created it will assign same
  default values to the data members of the class.
  It is not possible to assign different values to the
  data members for the different object of the
  class using default constructor.
Prg 1-- WAP to declar a class student having data members as
name and percentage. Write a constructor to initialize these
data members. Accept and display data for one object.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
 float per;
 char name[20];
public:
 student();
void put();
};
student::student(){
 strcpy(name,”abc”);
per=98.8;
}
void student::put(){
cout<<”Name of student”<<name;
cout<<”percentage”<<per;
}
void main(){
student s;
s.put();
getch();
}
OR
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
 float per;
 char name[20];
public:
 student(){
strcpy(name,”abc”);
per=98.8;
}
void put(){
cout<<”Name of student”<<name;
cout<<”percentage”<<per;
}
};
void main(){
student s;
s.put();
getch();
}
       Parameterized Constructor
   It is possible to pass arguments to constructor
    functions. These arguments help to initialize
    an object when it is created.
   To create parameterized constructor, simply
    add parameters to the constructor function
    and when you define the constructors body
    use parameters to initialize objects.
   The constructor that can take arguments are
    called parameterized constructor.
     Parameterized Constructor
           declaration
class student
{
 int rollno;
 char name[20];
 public:
  student(int rn, char nm[20]);
}
Parameterized Constructor definition--
student::student(int rn, char nm[20]){
rollno=rn;
strcpy(name,nm);
}
Constructor calling--
void main(){
student s(1,”abc”),s2(2,”aa”);
....
}
Prg 1-- WAP to declare a class student having data members as
name and percentage. Write a constructor to initialize these
data members. Accept and display data for one object.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
 float per;
 char name[20];
public:
 student(char nm[20], float p){
strcpy(name,nm);
per=p;
}
void put();
};
void student::put(){
cout<<”Name of student”<<name;
cout<<”percentage”<<per;
}
void main(){
student s(“abc”,90);
s.put();
getch();
}
Prg 2—Define a class to represent a bank account which
includes:
Data members: name, acc_no, acc_type, bal_amt and
constructor to assign initial value.
Member function: to deposit an amount, to withdraw an
amount and to display the name and balance.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank_account{
int acc_no, bal_amt;
char name[20], acc_type[20];
public:
bank_account(char n[30], int an, char at[20], int ba);
void deposit();
void withdrawl();
void display();
bank_account::bank_account(char n[30], int an, char
at[20], int ba){
acc_no=an;
bal_amt=ba;
strcpy(name, n);
strcpy(acc_type, at);
}
void bank_account::deposit(){
int dep_amt;
cout<<”\n Enter deposit amount”;
cin>>dep_amt;
bal_amt=bal_amt+dep_amt;
cout<<”\n Amount=”<<dep_amt<<”is deposited to an
account”<<acc_no<<”\n available balance”<<bal_amt;
}
void bank_account::withdrawl(){
int wd_amt;
cout<<”Enter withdrawl amount”;
cin>>wd_amt;
bal_amt=bal_amt-wd_amt;
cout<<”\n Amount”<<wd_amt<<”is withdrawl from an
   account”<<acc_no<<”\n available balance ”<<bal_amt;
}
void bank_account::display(){
 cout<<”\n name”<<name<<”available balance”<<bal_amt;
}
void main(){
bank_account x(“ABC”, 765, “Fix”, 10000);
clrscr();
x.deposit();
x.withdrawl();
x.display();
getch();
Constructor with default argument
   It is possible to define constructor with default
    argument.
   For eg- student(int rn, float per=80);
    The default value of argument per is 80. then
     the statement
         student s(2);
    assigns the value 2 to rn and 80 to per(by
      default).
   Initialization of default value to variable is done
    only in function declaration or function definition
    not in both.
   Default argument is written after normal
    argument
   For eg--
student(int rn, float per=80); //correct
student( float per=80,int rn); //wrong
Prg1--Define a class salary which will contain member
variable BASIC, TA, DA, HRA. Develop a program using
constructor with default values for DA and HRA and
calculate salary of employee.
#include<iostream.h>
#include<conio.h>
class salary{
float BASIC, TA, DA, HRA;
public:
salary(float, float, float, float);
void display();
};
salary::salary(float ta, float basic, float da=200, float
hra=400){
 BASIC=basic;
 TA=(BASIC*ta)/100;
 DA=(BASIC*da)/100;
 HRA=(BASIC*hra)/100
}
void salary::display(){
cout<<”\n Salary of employee:”<<( BASIC+TA+DA+HRA);
}
void main(){
salary s(110, 6000);
s.display();
getch();
}
Prg2--Define a class college which will contain member
variable as rollno, name and course. Write a program using
constructor with default values as “Information Technology”
for course. Accept this data for two objects of class and
display the data.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
int rollno;
char name[30], course[30];
public:
student(char str[30]=”Information Technology”);
void accept();
void display();
};
student::student(char str[30]){
strcpy(course,str);
}
void student::accept(){
cout<<”Enter rollno and name:”;
cin>>rollno>>name;
}
void student::display(){
cout<<”Roll Number=”<<rollno;
cout<<”name=”<<name;
cout<<”Course=”<<course; }
void main(){
student s1,s2;
s1.accept();
s2.accept();
s1.display();
s2.display();
getch(); }
              Copy constructor
   A constructor that accept a reference to its
    own class as a parameter which is called as a
    copy constructor.
   A copy constructor takes a reference to an
    object of the same class as itself as an
    argument.
Eg:-
#include<iostream.h>
#include<conio.h>
class code{
  int id;
  public:
  code(int a){
  id=a;
}
code(code &x){
  id=x.id;
}
void display(){
cout<<id;
}
};
void main(){
code A(11);
code B(A);
clrscr();
cout<<”ID of A”;
A.display();
cout<<”ID of B”;
B.display();
getch();
}
o/p--
ID of A 11
ID of B 11
    The statement
                         code B(A);
    Would invoke the second constructor which copies the
     values of A into B.
     In other words, it sets the values of every data element
     of B to the value of the corresponding data element of
     A. Such constructor is called copy constructor.
        Multiple constructor in a
     class/Constructor Overloading
   We have used no argument(default), one
    argument(copy) and parameterized
    constructor.
   C++ permits to use all these constructors in
    the same class.
   When more than one constructor function is
    defined in a class, we say that the constructor
    is overloaded.
#include<iostream.h>
#include<conio.h>
class student{
int rollno, per;
public:
student(){              //default constructor
 rollno=0;
 per=0;
}
student(int rn, int p){     //parameterized constructor
 rollno=rn;
 per=p;
}
student(student &s){              //copy constructor
 rollno=s.rollno;
 per=s.per;
}
void display(){
cout<<”\nRoll Number=”<<rollno;
cout<<”\nPercentage=”<<per;
}
};
void main(){
student s1();     //default con. Invoked
student s2(2,90);  //parameterized con. Invoked
student s3(s2);       //copy con. Invoked
s1.display();
s2.display();
s3.display();
getch();
}
o/p--
Roll Number=0
Percentage=0
Roll Number=2
Percentage=90
Roll Number=2
Percentage=90
               Destructor
Destructor is a member function of the class.
It is called special member function because
the name of the destructor is same as name of
the class but it is preceded by the tilde (~)
sign.
Destructor is used to destroy the object that is
created using constructor.
Like other member function there is no need to
call destructor explicitly. It is invoked
automatically(implicitly) by the compiler upon
exit from the program to clean up storage(free
memory space) that is no longer accessible.
A destructor does not accept any argument and
it does not return any value.
Destructor can be defined inside class as shown
 below:
Class Rectangle {
int Height, Width;
public:
Rectangle () {
Height = 1;
Width = 1;
cout << "Object Created";
}
~Rectangle (){
cout << "Object Destroyed";
}
}
Destructor can be defined outside class as shown
 below:
Class Rectangle {
int Height, Width;
public:
Rectangle ();
~Rectangle ();
}
Rectangle :: Rectangle () {
Height = 1;
Width = 1;
cout << "Object Created";
}
Rectangle :: ~Rectangle () {
cout<<"Object Destroyed";
}