0% found this document useful (0 votes)
46 views67 pages

Unit II

The document discusses object-oriented programming concepts like classes, objects, member functions, and constructors. It provides examples of defining a class with data members and member functions both inside and outside the class. An object is created by declaring a variable of the class type. Member functions can access private data members of the class. Memory is allocated separately for each object on the heap. Constructors initialize objects and are used to pass values to objects.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views67 pages

Unit II

The document discusses object-oriented programming concepts like classes, objects, member functions, and constructors. It provides examples of defining a class with data members and member functions both inside and outside the class. An object is created by declaring a variable of the class type. Member functions can access private data members of the class. Memory is allocated separately for each object on the heap. Constructors initialize objects and are used to pass values to objects.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Unit-II:

Classes and Objects


Weightage of Marks: 18 Marks
Previous Year Questions:-
▪ Differentiate between structure and class.
▪ What is data abstraction ?(2M-S14)
▪ Define the terms: (1)object (2)class(2M-S14)
▪ Define class with its syntax.(2M-S14, W-14)
▪ Explain how memory is allocated to an object of a class with diagram.(4M-W14, W16)
▪ How memory is allocated when multiple object of class are created ? Explain with example.(4M-S17)
▪ How many ways we can define member function in class ? Give its syntax(4M-W14)
▪ Explain syntax for declaring the function inside the class and outside the class with example. (4M-S15)
▪ Give syntax for defining a member function inside and nesting of functions in a class with example. (4M-W16)
▪ Program:- write a program to find area of circle using object oriented programming such that the class circle
must have three member functions namely: a)read() to accept the radius from the user. b)compute() for
calculating the area. C)display for displaying the result.(4M-S15,W16)
▪ Program:- Write a program to find area of circle using object oriented programming such that the class circle
must have three externally defined functions namely: a)read() to accept the radius from the user.
b)compute()for calculating the area. c)display for displaying the result.(2M-W15)
▪ What do you mean by inline function ? Write its syntax and example(4M-W16)
▪ Program:- Write a program to find area of circle using object oriented programming such that the class circle
must have three inline functions namely: a)read() to accept the radius from the user. b)compute() for
calculating the area. c)display for displaying the result.(2M-W16)
▪ Program:-write a program to declare a class student having data member as name and percentage. Write a
constructor to initialize these members accept and display data for one student.(4M-W16)
▪ What is nesting of member function ? Give one example.(4M-S16)
▪ Illustrate the concept of constructor with default argument with suitable example.(4M-S14)
▪ State any four characteristics of constructor.(4M-S14)
▪ List four types of constructor(4M-W16)
▪ What is constructor ? How user can declared constructor in derived class ? Explain with example.(8M-W17)
▪ What is parameterized constructor ? Give the syntax and example of it(4M-S14)
▪ Explain multiple constructor in class with example.(4M-S16)
▪ Program:- Write a program to find area of circle using object oriented programming. The value of the radius
must be accepted from the user in the main program and passed to the parameterized constructor and the
class circle must have two inline functions namely: a)compute() for calculating the area. b)display for
displaying the result.(2M-W17)
▪ What is copy constructor ? Explain with example(4M-W17)
▪ What do you mean by overloaded constructor.(2M-W17)
▪ State the characteristics of destructor.(2M-S14)
▪ What is destructor ? How destructor is declared ? When destructor is invoked ?(4M-W17)
▪ Explain the concept of friend function.(4M-S14)
▪ Program:- Write a program to find area of circle using object oriented programming. The value of the radius
must be accepted from the user in the main program and passed to the copy constructor and the class circle
must have two inline functions namely: a)compute() for calculating the area. b)display() for displaying the
result.
▪ What do you mean by default argument ? Illustrate concept of constructor with default argument using suitable
example.(4M-S17)
▪ Write any two rules to define friend function. (2M-W14, W16)
▪ Define friend function. Write syntax of declaring it.(4M-W14)
▪ Write a program to add two complex numbers using a friend function to add the complex numbers(8M-S16)
▪ Write any two characteristics of static member function.(4M-W14,W16)
▪ Explain the need of static member function with example.(4M-W17)
▪ Program:- Write a program to declare a class staff having data members as name and department. Accept this
data for 10 staff and display names of staff that are in cm department.(8M-S14)
▪ Program:- Write a program to declare a class staff having data members as name and post. Accept and display
data for five staff members(using array of object)(8M-W14)
▪ Program:- Write a program to declare class Account having data member as acc_no and balance. Accept and
display data for five object using pointer to array of object.(4M-S17)
▪ Program:- Write a program to declare a class mobile having data members as price and model number. Accept
and display this data for ten objects.(4M-W16)
▪ Explain with example object as member function argument(4M-S16)
No Structures Classes

1 Structure is a value type Class is s reference type

2 Structures can not be inherited Classes can be inherited

3 Variables are created on the stack memory Objects are created on the heap memory

4 Only variables are declared Both variables and functions are declared

5 Structures don’t have constructor and destructor Classes have constructor and destructor

6 Data is not hidden (like private or protected) Can be hidden outside the class (like private or
protected)
7 The member variables can not be initialized The member variables can be initialized directly.
directly
2.1] Class & Object: Introduction, Specifying a class, access specifies, defining
member functions, creating objects, memory allocation for objects

Specifying a Class:-
Definition:- A class is a way to bind the data and its associated member functions together.
A class specification has two parts:
l. Class declaration:-
2. Class function definitions:-
The general form of a class declaration is:
▪ The ‘class’ is the keyword which follows the class_name.
▪ The body of a class is enclosed within braces and terminated by a semicolon.
▪ The class body contains the declaration of variables and functions. The variables are known as data members
and functions are known as member functions. They are grouped under two sections, namely, private and
public.
▪ The use of the keyword private is optional. By default, the members of a class are private.
▪ The data members are kept private
and member functions are kept public.
▪ Private members can have access only
within the class and public members can
have access outside the class.
▪ This is illustrated in fig.
Fig: Data hiding in classes
A Simple Class Example:-
#include<iostream.h>
#include<conio.h>
class Item
{
private:
int number; //variables declaration private by default
float cost;

public:
void getdata(int a, float b);//function declaration using prototype
void showdata(void);
}; //end with semicolon

void Item::getdata(int x, float y)


{
number=x;
cost=y;
}
void Item::showdata(void)
{
cout<<"Number :"<<number<<"\t"<<"cost :"<<cost;
}

int main(void)
{
clrscr();
Item t1;
t1.getdata(2,45.78);
t1.showdata();
getch();
return 0;
}

Fig: Representation of a class


Creating Objects:-
▪ Once a class has been declared, we can create variables of that type by using the class name.
e.g. Item t1; creates a variable(object) t1 of type Item.
▪ In C++, the class variables are known as objects. Therefore, t1 is called an object of type item.
▪ The necessary memory space is allocated to an object at this stage. Note that class specification, like a
structure provides only a template and does not create any memory space for the objects.
▪ Objects can also be created when a class is defined by placing their names immediately after the closing brace

Accessing class Members:-


The following is the format for calling a member function:
object_name.function_name(actual_agruments);
For example, x.getdata(l00,75.5);
Similarly, the statement
x.putdata() ;
x.number= 100;
Defining Member Functions:-
Member functions can be defined in following ways:
1. Outside the class definition:-
2. inside the class definition:-
3. Inline functions:-
1. Outside the Class Definition:-
The general form of a member function definition is:
return_type class_name::function_name(argument declaration)
{
function body
}
The class-name :: tells the compiler that the function , function-name belongs to the class class-name.
That is, the scope of the function is restricted to the class-name specified in the header line. The symbol :: is
called the scope resolution operator. Above program is example of outside the class definition.
Consider the member functions getdata() and showdata().They may be coded as follows:
void Item :: getdata{ int a, float b)
{
number= a;
cost = b;
}

void Item::showdata(void)
{
cout << “Number :” <<number << “\n“;
cout << “cost :” <<cost << “\n”;
}
Since these functions do not return any value, their return type is void.
2. Inside the Class Definition:-
Function is defined inside the class as follows:
#include<iostream.h>
#include<conio.h>
class Item {
private:
int number; //variables declaration private by default
float cost;

public:
void getdata(int a, float b)
{
number=a;
cost=b;
}
void showdata(void) //definition inside the class
{
cout<<"Number :"<<number<<"\t"<<"Cost :"<<cost;
}
};

int main(void)
{
clrscr();
Item t1;
t1.getdata(2,45.78);
t1.showdata();
getch();
return 0;
}
Inline functions:-
Inlining is only request to the compiler and not a command. Compiler can ignore the request for inlining.
Compiler may not perform inlining in such circumstances like:
1. If a function contains a loop e.g. for , while ,do-while
2. If a function contains static variables.
3. If a function is recursive.
4. If a function return type is other than void and the return statement doesn’t exist in a function body.
5. If a function contains switch or goto statement.

Advantages:-
1. Function call overhead doesn’t occur.
2. It also saves the overhead of push/pop variables on the stack when function is called.

3. It also saves overhead of a return call from a function.


function. Such optimizations are not possible for normal function calls. Other optimizations can be obtained by
considering the flows of calling context and the called context.
5) Inline function may be useful (if it is small) for embedded systems because inline can yield less code than the
function call preamble and return.

Inline function disadvantages:-


1) The added variables from the inlined function consumes additional registers, After in-lining function if variables
number which are going to use register increases than they may create overhead on register variable resource
utilization. This means that when inline function body is substituted at the point of function call, total number of
variables used by the function also gets inserted. So the number of register going to be used for the variables will
also get increased. So if after function inlining variable numbers increase drastically then it would surely cause an
overhead on register utilization.
2) If you use too many inline functions then the size of the binary executable file will be large, because of the
duplication of same code.
3) Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch
from that of cache memory to that of primary memory.
4) Inline function may increase compile time overhead if someone changes the code inside the inline function
then all the calling location has to be recompiled because compiler would require to replace all the code once
again to reflect the changes, otherwise it will continue with old functionality.
5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is
more important than speed.
6) Inline functions might cause thrashing because inlining might increase size of the binary executable file.
Thrashing in memory causes performance of computer to degrade.
Making an Outside Function inline:
We can define a member function outside the class definition and still make it inline by just
using the qualifier inline in the header line of function definition. Example:
#include<iostream.h>
#include<conio.h>
class Item
{
private:
int number; //variables declaration private by default
float cost;

public:
void getdata(int a, float b);//function declaration using prototype
void showdata(void);
}; //end with semicolon
inline void Item::getdata(int x, float y)
{
number=x;
cost=y;
}
inline void Item::showdata()
{
cout<<"Number :"<<number<<"\t"<<"cost :"<<cost;
}

int main()
{
clrscr();
Item t1;
t1.getdata(2,45.78);
t1.showdata();
getch();
return 0;
}
Nesting of member function:-
A member function can be called by using its name inside another member function of the same class.
This is known as nesting of member functions.
class set
{
int m,n;
public:
void input(void);
void display(void);
int largest(void);
};
int set :: largest( void)
{
if(m >= n)
return (m);
else return(n); }
void set ::input(void)
{
cout <<"Input values of m and n" << "\n";
cin>>m>>n;
}

void set :: display(void)


{
cout <<"largest value="<< largest() <<"\n";
}
int main()
{
set A;
A.input();
A.display();

return 0;
}
Memory allocation for objects:-
▪ Memory space for objects is allocated when they are declared and not when the class is specified. This
statement is only partly true.
▪ Actually, the member functions are created and placed in the memory space only once when they are
defined as a part of a class specification.
▪ Since all the objects belonging to that class use the same member functions, no separate space is allocated
for member functions when the objects are created.
▪ Only space for member variables is allocated separately for each object.
▪ Separate memory locations for the objects are essential, because the member variables will hold different
data values for different objects. This is shown in below fig.
2.2] Static data member, Static member Function, friend Function
Static data member:-
A static member variable has certain special characteristics as below:
• 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 objects of that class, no
matter how many objects are created.
• It is visible only within the class, but its lifetime is the entire program. Sample program is as below:
#include<iostream.h>
#include<conio.h>
class Counter
{
static int count;
int number;
public:
void getdata(int a)
{
number =a; count++;
}

void getCount()
{
cout<<"the value of counter :"<<count<<endl;
}
};

int Counter::count; //definition of static data member

int main()
{
Counter c1,c2,c3;
clrscr();
c1.getCount();
c2.getCount();
c3.getCount();

c1.getdata(100);
c2.getdata(200);
c3.getdata(300);

cout<<"After reading data :"<<endl;


c1.getCount();
c2.getCount();
c3.getCount();
getch();
return 0;
}
▪ Type and scope of each static member variable must be defined outside the class definition because the static
data members are stored separately 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
variables.
▪ The static variable count is initialized to zero when the objects are created. The count is incremented whenever
the data is read into an object.
▪ Since the data is read into objects three times, the variable count is incremented three times. Because there is
only one copy of count shared by all the three objects, all the three output statements cause the value 3 to be
displayed. Below fig. shows how a static variable is used by the objects.

▪ Static variables are like non-inline member functions as they are declared in a class declaration and
defined in the source file.
▪ While defining a static variable, some initial value can also be assigned to the variable. For instance, the
following definition gives count. the initial value 10.
int Counter::count=10;

Sharing of static data member:-


Static member Functions:-
A member function that is declared static has the following properties:
▪ A static function can have access to only other static members (functions or variables) declared in the same
class.
▪ A static member function can be called using the class name (instead of its objects) as follows:
class-name:: function -name;

Below program illustrates the implementation of these characteristics:

The static function showCount() displays the number of objects created till that moment. A count of
number of objects created is maintained by the static variable count. The function showCode() displays the code
number of each object.
#include<iostream.h>
#include<conio.h>
class Test
{
int code;
static int count;

public:
void setCode(void)
{code= ++count;}

void showCode(void)
{cout<<"oject number :"<<code<<endl;}

static void showCount()


{ cout<<"count :"<<count<<endl; }
};

int Test::count;
int main()
{ clrscr();
Test t1, t2;
t1.setCode();
t2.setCode();

Test::showCount();

Test t3;
t3.setCode();

Test::showCount();

t1.showCode();
t2.showCode();
t3.showCode();
getch();
return 0;
}
The statement
code =++count;
is executed whenever setCode( ) function is invoked and the current value of count is assigned to code. Since
each object has its own copy of code, The value contained in code represents a unique number of its object.
Friend Function:
A non member function cannot have access to the private data of a class. But there could be a situation
where we would like two classes to share a particular function.
▪ In some situations, C++ allows the common function to be made friendly with both the classes, thereby
allowing the function to have access to the private data of these classes. Such a function need not be a
member of any of these classes.
▪ To make an outside function "friendly" to a class, we have to simply declare this function as a friend of the class
as shown below:
A friend function possesses certain special characteristics:
▪ It is not in the scope of the class to which it has boon declared as friend.
▪ Since it is not in the scope of the class, it cannot be called using the object of that class.
▪ It can be invoked like a normal function without the help of any object.
▪ Unlike member functions, it cannot access the member names directly and has to use an object name and dot
membership operator with each member name.(e.g. A.x).
▪ It can be declared either in the public or the private part of a class without affecting its meaning.
▪ Usually, it has the objects as arguments.
#include<iostream.h>
#include<conio.h>
class Sample
{
int a,b;

public:
void setvalue(){a=20;b=40;}

friend float getMean(Sample s);


};

float getMean(Sample s)
{
return (s.a+s.b)/2;
}
int main(void)
{
clrscr();
Sample x;

x.setvalue();

cout<<"Mean of two variables is :"<<getMean(x);


getch();
return 0;
}

Output:

Mean of two variables is : 30


How friend functions work as a bridge between the classes:
When the function maxValue() is declared as a friend in XYZ for the first time, the compiler will not
acknowledge the presence of ABC unless its name is declared in the beginning as class ABC; This is known as
'forward' declaration.
#include<iostream.h>
#include<conio.h>
class ABC; //forward declaration
class XYZ
{
int x;
public:
void setValue(int i){x=i;}
friend void maxValue(XYZ, ABC);
};
class ABC
{
int a;
public:
void setValue(int i){a=i;}
friend void maxValue(XYZ, ABC);
};

void maxValue(XYZ p, ABC q) //definition of friend function


{
if(p.x>q.a)
cout<<"Greater value is :"<<p.x;
else
cout<<"Greater value is :"<<q.a;
}
int main(void)
{
clrscr();
ABC abc;
abc.setValue(20);

XYZ xyz;
xyz.setValue(30);
maxValue(xyz,abc);
getch();
return 0;
}

Output:

Greater value is :30


Program shows how to use a common friend function to exchange the private values of two classes. The
function is called by reference.
#include<iostream.h>
#include<conio.h>
class ABC;
class XYZ
{
int value1;
public:
void getdata(int X){value1=X;}
void showdata(void){cout<<"The value of value1 is:"<<value1<<endl;}
friend void exchangeData(XYZ &, ABC &);
};
class ABC{
int value2;
public:
void getdata(int A){value2=A;}
void showdata(void){cout<<"The value of value2 is:"<<value2<<endl;}
friend void exchangeData(XYZ &, ABC &);
};

void exchangeData(XYZ &x, ABC &y)


{ int temp= x.value1;
x.value1= y.value2;
y.value2=temp;
}
int main()
{
clrscr();
XYZ xyz;
ABC abc;
xyz.getdata(200);
abc.getdata(100);
cout<<"Values before exchange"<<endl;
xyz.showdata();
abc.showdata();
exchangeData(xyz,abc);
cout<<"Values after exchange"<<endl;
xyz.showdata();
abc.showdata();
getch();
return 0;
}

Output:-
Values before exchange
The value of value1 is:200
The value of value2 is:100

Values after exchange


The value of value1 is:100
The value of value2 is:200
2.3] Array of objects, object as function arguments
Array of objects:-
#include<iostream.h>
#include<conio.h>
class Employee
{
char name[50];
float age;
public:
void getdata(void);
void showdata(void);
};
void Employee::getdata(void)
{
cout<<"Enter name"<<endl;
cin>>name;
cout<<"Enter age";
void Employee::showdata(void)
{
cout<<"Name :"<<name<<endl;
cout<<"Age :"<<age<<endl;
}
int main(void)
{ clrscr();
int i;
Employee manager[3]; //array of object
for( i=1; i<=3; i++)
{
manager[i].getdata();
cout<<“Enter Details of manager ”<<i<<endl;
}
for(i=1; i<=3; i++)
{
cout<<“Details of manager :”<<i<<endl;
manager[i].showdata(); }
getch(); }
An array of objects is stored inside the memory in the same way as a multi-dimensional array. The array
manager is represented in Fig. Only the space for data items of the objects is created. Member functions are
stored separately and will be used by all the objects.

Output:-
Details of manager :1
Name: Amit
Age:23
Details of manager :2
Name: Pankaj
Age:34
Details of manager :3
Name: Amruta
Age:33 Fig: Storage of data items of an object array
Object as function argument:-
An object may be used as a function argument in two ways:
▪ A copy of the entire object is passed to the function.
▪ Only the address of the object is transferred to the function.

▪ The first method is called pass by value i.e. Since a copy of the 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.
▪ The second method is called pass by reference. When an address of the object is passed, the called
function works directly on the actual object used in the call. This means that any changes made to the
object inside the function will reflect in the actual object.
▪ The pass by reference method is more efficient since it requires to pass only the address of the object and
not the entire object.
Below program illustrates the use of objects as function arguments. It performs the addition of time in the
hour and minutes format:
#include<iostream.h>
#include<conio.h>
class Time
{
int hours, minutes;
public:
void getTime(int h, int m){hours=h; minutes=m;}
void showTime(void)
{
cout<<"Hours : "<<hours<<"\t";
cout<<"Minutes : "<<minutes<<endl;
}

void sum(Time, Time);//declaration with object as argument


};
void Time::sum(Time t1, Time t2)
{
minutes= t1.minutes+t2.minutes;
hours= minutes/60;
minutes= minutes%60;
hours= hours+t1.hours+t2.hours;
}
int main(void)
{
clrscr();
Time t1, t2, t3;
t1.getTime(2,30);
t2.getTime(3,45);
t3.sum(t1,t2);
cout<<"T1 : ";t1.showTime(); Output:
cout<<"T2 : ";t2.showTime(); T1 = 2 hours and 30 minutes
cout<<"T3 : ";t3.showTime(); T2 = 3 hours and 45 minutes
getch(); T3 = 6 hours and 15 minutes
return 0; }
Fig: Accessing members of objects within a called function
2.4] concept of constructors, Types of constructors
Concept of constructors:-
A constructor is a 'special' member function who initialize the objects of its class. It has same as the
class name. The constructor is invoked automatically whenever an object of its associated class is created. It is
called constructor because it constructs the values of data members of the class.

The constructor functions have following features:-


▪ They should be declared in the public section.
▪ They are invoked automatically when the objects are created.
▪ They do not have return types and therefore they cannot return values.
▪ They cannot be inherited, though a derived class can call the base class constructor.
▪ They can have default arguments.
▪ Constructors cannot be virtual.
▪ We cannot refer to their addresses.
▪ An object with a constructor (or destructor) cannot be used as a member of a union.
▪ They make 'implicit calls' to the operators new and delete when memory allocation is required.
class Employee
{ int id,salary;
public:
Employee(void); //constructor declared
};
void Employee::Employee(void) //constructor defined
{ id=0;
salary=0;
}
Types of constructors:-
1] default constructor:-
2] parameterized constructor:-
3] copy constructor:-

#include<iostream.h>
#include<conio.h>
class Integer
{
int m,n;
public:
Integer(int , int);
void display(void)
{
cout<<"M= :"<<m;
cout<<"N= :"<<n<<endl;
}
};
Integer::Integer(int x, int y)
{
m=x;n=y;
}
int main(void)
{
clrscr();

Integer int1(0,100);
Integer int2=Integer(25,25);

cout<<"Object 1 :";int1.display();
cout<<"Object 2 :";int2.display();
getch();
return 0;
}
Default Constructor:-
• It is possible to define constructors with default arguments. For example, the constructor complex() can be
declared as follows:

complex(float real , float imag=0) ;


• The default value of the argument imag is zero. Then, the statement
• complex C(5.0) ;
Parameterized constructor:-
▪ The constructors that can take arguments are called parameterized constructors.
▪ We must pass the initial values as arguments to the constructor function when an object is declared. This can
be done in two ways:-
1] By calling the constructor explicitly.
2] By calling the constructor implicitly.
Copy Constructor:-
▪ A copy constructor is used to declare and initialize an object from another object.
▪ A copy constructor takes a reference to an object of the same class as itself as an argument.
▪ A simple example of constructing and using a copy constructor is as below:
#include<iostream.h>
#include<conio.h>
class Code
{
int id;
public:
Code(){}//constructor
Code(int a){id=a;}
Code(Code &x)//copy constructor
{
id= x.id;
}
void display(void)
{ cout<<"Id :"<<id<<endl; }
};
int main()
{ clrscr();
Code A(200); //object A is initialized
Code B(A); //copy constructor is called
Code C=A; //copy constructor is called again
Code D; //D is created but not intialized
D=A; //copy constructor is not called
cout<<"ID of A :";A.display();
cout<<"ID of B :";B.display();
cout<<"ID of C :";C.display();
cout<<"ID of D :";D.display();
getch(); return 0; }
2.5] multiple constructors in a class, Constructors with default arguments

Multiple constructors in a class/overloaded Constructor:-


More than one constructor with different number parameters for each constructor is called as
constructor overloading. e.g.
Complex()
Complex(float a)
Complex(float real, float imag)
Where Complex is the name of the constructor which in turn name of the class
#include<iostream.h>
#include<conio.h>
class Complex
{
float x,y;
public:
Complex(){}
Complex(float a){x=y=a;}
Complex(float real, float imag){x=real;y=imag;}

friend Complex sum(Complex, Complex);


friend 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 show(Complex c)
{
cout<<c.x<<" + j"<<c.y<<endl;
}

int main()
{ clrscr();
Complex A(3.5,4.7);
Complex B(1.2);
Complex C;

C=sum(A,B);

cout<<"A=";show(A);
cout<<"B=";show(B);
cout<<"C=";show(C);
getch();
return 0;
}
2.6] Destructors:-
▪ A destructor is used to destroy the objects that have been created by a constructor.
▪ The destructor is a member function whose name is the same as class but is preceded by a tilde sign(~).
▪ A destructor never takes any argument and does not it return any value.
▪ It will be invoked implicitly by the compiler on exit from the program to clean up storage (objects)that is no
longer in use.
▪ It releases memory space for future use.
e.g. the destructor for the class Employee can be defined as shown below:
~Employee()
{
}
#include<iostream.h>
#include<conio.h>
int count=0;
class Alpha
{
public:
Alpha()
{
count++;
cout<<"Number of objects created :"<<count<<endl;
}

~Alpha()
{
cout<<"Number of objects destroyed :"<<count<<endl;
count--;
}
};
int main(void)
{
clrscr();

Alpha a1, a2;


{
cout<<"ENTER BLOCK 1 :"<<endl;
Alpha a3;
}

{
cout<<"ENTER BLOCK 2 :"<<endl;
Alpha a4;
}

cout<<"RE-ENTER MAIN BLOCK :"<<endl;


getch();
return 0;
}

You might also like