SlideShare una empresa de Scribd logo
1 de 58
UNIT-2
Classes and Object, Dynamic
Memory Management,
Constructor & Destructor
BCA-2nd
Sem
Member Functions
 The program defines person as a new data of type
class.
 The class person include two basic data type items
and two function to operate on that data. it’s call
Member function.
 There are two type:
1 Outside the class definition
2 Inside the class definition
Outside the class Defination
 The member functions of a class can be defied outside
the class definitions. It is only declared inside the class
but defined outside the class. The general form of
member function definition outside the class definition
is:
 Return_typeClass_name::function_name(argumentlist)
{
Functionbody
}
 Where symbol :: is a supe resolution operator.
Inside the class definition
 The member function of a class can be
declared and defined inside the class
definition.
EX.
Class item
{
int number;
float cost;
Inside the class definition
Public:
Void getdata(int a, float b);
Void putdata(void)
{
cout<< number<<“n”;
cout<<cost<<“n”;
}
};
Data Member
 Data member depends on access control of data
member.
 If it’s public then data member can be easily accessed
using the direct member access operator with the
object of that class.
 If the data member is defined as private or protected
then we can not access the data variables directly.
Data Member
Type of data Member
1.Public
2.Private
3.protected
Data member type
#include <iostream>
using namespace std;
class alpha{
private:
int id;
static int count;
public:
alpha(){count++;
id=count;}}
Data member type
void print(){
cout<<"My id is"<<id;
cout<<"countis"<<count;} };
int alpha : :count=0;
void main (){
alpha a1,a2,a3;
a1.print();
a2.print();
a3.print();}
Friend Function
 A friend function of a class is defined outside that
class' scope but it has the right to access all private
and protected members of the class.
 Even though the prototypes for friend functions
appear in the class definition, friends are not member
functions.
Friend Function
Example:
class Box
{
double width;
public: double length;
friend void printWidth( Box box );
voidsetWidth(doublewid);
};
Friend Function Property
1) Friend of the class can be member of some other
class.
2) Friend of one class can be friend of another class or
all the classes in one program, such a friend is
known as GLOBAL FRIEND.
3) Friend can access the private or protected members
of the class in which they are declared to be friend,
but they can use the members for a specific object.
Friend Function Property
4) Friends are non-members hence do not get “this”
pointer.
5) Friends, can be friend of more than one class, hence
they can be used for message passing between the
classes.
6) Friend can be declared anywhere (in public,
protected or private section) in the class.
Friend Class
 A class can also be declared to be the friend of some
other class.
 When we create a friend class then all the member
functions of the friend class also become the friend of
the other class.
 This requires the condition that the friend becoming
class must be first declared or defined (forward
declaration).
Friend Class Example
#include <iostream>
using namespace std;
class MyClass{
friend class SecondClass;
public: MyClass() : Secret(0){}
void printMember()
{ cout << Secret << endl;}
private: int Secret; };
}
Friend Class Example
class SecondClass{
public: void change( MyClass& yourclass, int x ){
yourclass.Secret = x;} };
void main()
{
MyClass my_class;
SecondClass sec_class;
my_class.printMember();
sec_class.change( my_class, 5 );
my_class.printMember();}
Array of object
class A
{
int* myArray;
A()
{
myArray = 0;
}
A(int size)
{
Array of object
myArray = new int[size];
}
~A()
{
delete []
myArray;
}
}
Returning Objects from
Functions
#include <iostream>
using namespace std;
class Complex{
private: int real;
int imag;
public: Complex():
real(0), imag(0){ }
void Read() {
Returning Objects from Functions
cout<<"Enter real and imaginary number
respectively:"<<endl;
cin>>real>>imag;
}
Complex Add(Complex comp2)
{
Complex temp;
temp.real=real+comp2.real;
temp.imag=imag+comp2.imag;
return temp;
Returning Objects from Functions
}
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
};
int main()
{
Returning Objects from Functions
Complex c1,c2,c3;
c1.Read();
c2.Read();
c3=c1.Add(c2);
c3.Display();
return 0; }
Nested Classes
A class can be declared within the scope of another
class. Such a class is called a "nested class.
" Nested classes are considered to be within the scope
of the enclosing class and are available for use within
that scope.
 To refer to a nested class from a scope other than its
immediate enclosing scope, you must use a fully
qualified name.
Nested Classes Example
#include <iostream.h>
class Nest
{
public: class Display
{
private: int s;
public:
void sum( int a, int b)
{
Nested Classes Example
s =a+b; }
void show( )
{
cout << "nSum of a and b is:: " << s;
} };
};
void main() {
Nest::Display x; x.sum(12, 10);
x.show();
}
Namespaces
 Namespace is a new concept introduced by the ANSI
C++ standards committee.
 This defines a scope for the identifiers that are used in
a program.
 For using the identifier defined in the namespace
scope we must include the using directive, like
 Using namespace std;
Namespaces
 Here, std is the namespace where ANSI C++ standard
class libraries are defined.
 All ANSI C++ programs must include this directive.
 This will bring all the identifiers defined in std to the
current global scope. Using and namespace are the
new keyword of C++.
Constructor
 A constructor (having the same name as that of the
class) is a member function which is automatically
used to initialize the objects of the class type with
legal initial values.
Characteristics of Constructor
(i) These are called automatically when the objects are
created.
(ii) All objects of the class having a constructor are
initialized before some use.
(iii) These should be declared in the public section for
availability to all the functions.
Characteristics of Constructor
(iv) Return type (not even void) cannot be specified for
constructors.
(v) These cannot be inherited, but a derived class can
call the base class constructor.
(vi) These cannot be static.
Characteristics of Constructor
(x) An object of a class with a constructor cannot be
used as a member of a union.
(xi) A constructor can call member functions of its
class.
(xii) We can use a constructor to create new objects of
its class type by using the syntax
Characteristics of Constructor
(vii) Default and copy constructors are generated by the
compiler wherever required. Generated constructors
are public.
(viii) These can have default arguments as other C++
functions.
(ix) A constructor can call member functions of its
class.
Types of Constructor
1 Overloaded Constructors
2 Copy Constructor
3 Dynamic Initialization of Objects
4 Constructors and Primitive Types
5 Constructor with Default Arguments
1.Overloaded Constructors
 Besides performing the role of member data
initialization, constructors are no different from other
functions.
 This included overloading also. In fact, it is very
common to find overloaded constructors.
 For example, consider the following program with
overloaded constructors for the figure class :
1.Overloaded Constructors
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h> //for strcpy()
Class figure
{
Private:
Float radius, side1,side2,side3;
1.Overloaded Constructors
Figure (float s1, floats2, float s3)
{
ide1=s1;
side2=s2;
side3=s3;
radius=0.0;
strcpy(shape,”triangle”);
}
Char shape[10];
Public:
figure(float r)
1.Overloaded Constructors
{
radius=r;
strcpy (shape, “circle”);
}
void area()
{
float ar,s;
if(radius==0.0)
{
a
1.Overloaded Constructors
if (side3==0.0)
ar=side1*side2;
else
ar=3.14*radius*radius;
cout<<”nnArea of the
“<<shape<<”is :”<<ar<<”sq.unitsn”;
} };
Void main()
{
Clrscr();
1.Overloaded Constructors
Figure circle(10.0); /
Figure rectangle15.0,20.6);
Figure Triangle(3.0, 4.0, 5.0);
Rectangle.area();
Triangle.area();
Getch();//freeze the monitror
}
2.Copy Constructor
 It is of the form classname (classname &) and used
for the initialization of an object form another object
of same type. For example,
2.Copy Constructor
Class fun {
Float x,y;
Public:
Fun (floata,float b)
{
x = a;
y = b;
}
Fun (fun &f)
{
2.Copy Constructor
cout<<”ncopy constructor at workn”;
X = f.x;
Y = f.y;
}
Void display (void)
{
{
Cout<<””<<y<<end1;
}
};
3.Dynamic Initialization of Objects
 The class objects can be initialized at run time
(dynamically).
 We have the flexibility of providing initial values at
execution time.
3.Dynamic Initialization of Objects
#include <iostream.h>
#include <conio.h>
Class employee {
Int empl_no;
Float salary;
Public:
Employee() {}
Employee(int empno,float s) {
Empl_no=empno;
Salary=s; }
3.Dynamic Initialization of Objects
Employee (employee &emp)
{
Cout<<”ncopy constructor workingn”;
Empl_no=emp.empl_no;
Salary=emp.salary;
}
Void display (void)
{
Cout<<”nEmp.No:”<<empl_no<<”salary:”<<salary<<
end1; }
3.Dynamic Initialization of
Objects
};
Void main()
{ int eno;
float sal;
clrscr();
cout<<”Enter the employee number and salaryn”;
cin>>eno>>sal;
employee obj1(eno,sal);
3.Dynamic Initialization of Objects
cout<<”nEnter the employee number and salaryn”;
cin>eno>>sal;
employee obj2(eno,sal);
obj1.display();
employee obj3=obj2;
obj3.display();
getch();
}
4.Constructors and Primitive Types
 In C++, like derived type, i.e. class, primitive types
(fundamental types) also have their constructors.
 Default constructor is used when no values are given
but when we given initial values, the initialization
take place for newly created instance.
 Example:
 float x,y;
 int a(10), b(20);
 float i(2.5), j(7.8);
4.Constructors and Primitive Types
Class add
{
Private:
Int num1, num2,num3;
Public:
Add(int=0,int=0);
Void enter (int,int);
Void sum();
Void display();
};
4.Constructor with Default Arguments
add::add(int n1, int n2)
{
num1=n1;
num2=n2;
num3=n0; }
Void add ::sum()
{
Num3=num1+num2;
}
Void add::display () {
Cout<<”nThe sum of two numbers is “<<num3<<end1;
}
Destructor
 The syntax for declaring a destructor is :
-name_of_the_class()
{
}
 It does not take any parameter nor does it return any
value. Overloading a destructor is not possible and
can be explicitly invoked. I
 n other words, a class can have only one destructor. A
destructor can be defined outside the class. The
following program illustrates this concept :
Destructor
#include<iostream.h>
#include<conio.h>
class add {
private :
int num1,num2,num3;
public :
add(int=0, int=0);
void sum();
void display();
~ add(void); };
Destructor
Add:: ~add(void)
{
Num1=num2=num3=0;
Cout<<”nAfter the final execution, me, the object has
entered in the”
<<”ndestructor to destroy myselfn”;
}
Add::add(int n1,int n2)
{
num1=n1;
num2=n2;
num3=0;
}
Destructor
Void add::sum() {
num3=num1+num2;
}
Void add::display () {
Cout<<”nThe sum of two numbers is “<<num3<<end1;
}
void main()
{
Destructor
Addobj1,obj2(5),obj3(10,20):
Obj1.sum();
Obj2.sum();
Obj3.sum();
cout<<”nUsing obj1 n”;
obj1.display();
cout<<”nUsing obj2 n”;
obj2.display();
cout<<”nUsing obj3 n”;
obj3.display();
}
Characteristics of Destructors
(i) These are called automatically when the objects are
destroyed.
(ii) Destructor functions follow the usual access rules
as other member functions.
(iii) These de-initialize each object before the object
goes out of scope.
(iv) No argument and return type (even void) permitted
with destructors.
Characteristics of Destructors
(v) These cannot be inherited.
(vi) Static destructors are not allowed.
(vii) Address of a destructor cannot be taken.
(viii) A destructor can call member functions of its
class.
(ix) An object of a class having a destructor cannot be a
member of a union.
REFRENCES
• Learn Programming in C++ By Anshuman
Sharma, Anurag Gupta, Dr.Hardeep Singh,
Vikram Sharma

Más contenido relacionado

La actualidad más candente

Procedure oriented programming
Procedure oriented programmingProcedure oriented programming
Procedure oriented programmingMrShahbazRafiq
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)Jay Patel
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...Pallavi Seth
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 

La actualidad más candente (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Procedure oriented programming
Procedure oriented programmingProcedure oriented programming
Procedure oriented programming
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 

Destacado

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++HalaiHansaika
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSaharsh Anand
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismEduardo Bergavera
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)akmalfahmi
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments rajni kaushal
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Abdullah khawar
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramDeepak Singh
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 
Constructor overloading in C++
Constructor overloading in C++Constructor overloading in C++
Constructor overloading in C++Learn By Watch
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmigAppili Vamsi Krishna
 

Destacado (20)

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
Apclass
ApclassApclass
Apclass
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Constructor overloading in C++
Constructor overloading in C++Constructor overloading in C++
Constructor overloading in C++
 
Loop c++
Loop c++Loop c++
Loop c++
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Array in c++
Array in c++Array in c++
Array in c++
 

Similar a Bca 2nd sem u-2 classes & objects

A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objectskhaliledapal
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classesrsnyder3601
 

Similar a Bca 2nd sem u-2 classes & objects (20)

Class and object
Class and objectClass and object
Class and object
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Class and object
Class and objectClass and object
Class and object
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classes
 

Más de Rai University

Brochure Rai University
Brochure Rai University Brochure Rai University
Brochure Rai University Rai University
 
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,
Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,Rai University
 
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02Rai University
 
Bsc agri 2 pae u-4.3 public expenditure
Bsc agri  2 pae  u-4.3 public expenditureBsc agri  2 pae  u-4.3 public expenditure
Bsc agri 2 pae u-4.3 public expenditureRai University
 
Bsc agri 2 pae u-4.2 public finance
Bsc agri  2 pae  u-4.2 public financeBsc agri  2 pae  u-4.2 public finance
Bsc agri 2 pae u-4.2 public financeRai University
 
Bsc agri 2 pae u-4.1 introduction
Bsc agri  2 pae  u-4.1 introductionBsc agri  2 pae  u-4.1 introduction
Bsc agri 2 pae u-4.1 introductionRai University
 
Bsc agri 2 pae u-3.3 inflation
Bsc agri  2 pae  u-3.3  inflationBsc agri  2 pae  u-3.3  inflation
Bsc agri 2 pae u-3.3 inflationRai University
 
Bsc agri 2 pae u-3.2 introduction to macro economics
Bsc agri  2 pae  u-3.2 introduction to macro economicsBsc agri  2 pae  u-3.2 introduction to macro economics
Bsc agri 2 pae u-3.2 introduction to macro economicsRai University
 
Bsc agri 2 pae u-3.1 marketstructure
Bsc agri  2 pae  u-3.1 marketstructureBsc agri  2 pae  u-3.1 marketstructure
Bsc agri 2 pae u-3.1 marketstructureRai University
 
Bsc agri 2 pae u-3 perfect-competition
Bsc agri  2 pae  u-3 perfect-competitionBsc agri  2 pae  u-3 perfect-competition
Bsc agri 2 pae u-3 perfect-competitionRai University
 

Más de Rai University (20)

Brochure Rai University
Brochure Rai University Brochure Rai University
Brochure Rai University
 
Mm unit 4point2
Mm unit 4point2Mm unit 4point2
Mm unit 4point2
 
Mm unit 4point1
Mm unit 4point1Mm unit 4point1
Mm unit 4point1
 
Mm unit 4point3
Mm unit 4point3Mm unit 4point3
Mm unit 4point3
 
Mm unit 3point2
Mm unit 3point2Mm unit 3point2
Mm unit 3point2
 
Mm unit 3point1
Mm unit 3point1Mm unit 3point1
Mm unit 3point1
 
Mm unit 2point2
Mm unit 2point2Mm unit 2point2
Mm unit 2point2
 
Mm unit 2 point 1
Mm unit 2 point 1Mm unit 2 point 1
Mm unit 2 point 1
 
Mm unit 1point3
Mm unit 1point3Mm unit 1point3
Mm unit 1point3
 
Mm unit 1point2
Mm unit 1point2Mm unit 1point2
Mm unit 1point2
 
Mm unit 1point1
Mm unit 1point1Mm unit 1point1
Mm unit 1point1
 
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,
Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,Bdft ii, tmt, unit-iii,  dyeing & types of dyeing,
Bdft ii, tmt, unit-iii, dyeing & types of dyeing,
 
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02Bsc agri  2 pae  u-4.4 publicrevenue-presentation-130208082149-phpapp02
Bsc agri 2 pae u-4.4 publicrevenue-presentation-130208082149-phpapp02
 
Bsc agri 2 pae u-4.3 public expenditure
Bsc agri  2 pae  u-4.3 public expenditureBsc agri  2 pae  u-4.3 public expenditure
Bsc agri 2 pae u-4.3 public expenditure
 
Bsc agri 2 pae u-4.2 public finance
Bsc agri  2 pae  u-4.2 public financeBsc agri  2 pae  u-4.2 public finance
Bsc agri 2 pae u-4.2 public finance
 
Bsc agri 2 pae u-4.1 introduction
Bsc agri  2 pae  u-4.1 introductionBsc agri  2 pae  u-4.1 introduction
Bsc agri 2 pae u-4.1 introduction
 
Bsc agri 2 pae u-3.3 inflation
Bsc agri  2 pae  u-3.3  inflationBsc agri  2 pae  u-3.3  inflation
Bsc agri 2 pae u-3.3 inflation
 
Bsc agri 2 pae u-3.2 introduction to macro economics
Bsc agri  2 pae  u-3.2 introduction to macro economicsBsc agri  2 pae  u-3.2 introduction to macro economics
Bsc agri 2 pae u-3.2 introduction to macro economics
 
Bsc agri 2 pae u-3.1 marketstructure
Bsc agri  2 pae  u-3.1 marketstructureBsc agri  2 pae  u-3.1 marketstructure
Bsc agri 2 pae u-3.1 marketstructure
 
Bsc agri 2 pae u-3 perfect-competition
Bsc agri  2 pae  u-3 perfect-competitionBsc agri  2 pae  u-3 perfect-competition
Bsc agri 2 pae u-3 perfect-competition
 

Último

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 

Último (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 

Bca 2nd sem u-2 classes & objects

  • 1. UNIT-2 Classes and Object, Dynamic Memory Management, Constructor & Destructor BCA-2nd Sem
  • 2. Member Functions  The program defines person as a new data of type class.  The class person include two basic data type items and two function to operate on that data. it’s call Member function.  There are two type: 1 Outside the class definition 2 Inside the class definition
  • 3. Outside the class Defination  The member functions of a class can be defied outside the class definitions. It is only declared inside the class but defined outside the class. The general form of member function definition outside the class definition is:  Return_typeClass_name::function_name(argumentlist) { Functionbody }  Where symbol :: is a supe resolution operator.
  • 4. Inside the class definition  The member function of a class can be declared and defined inside the class definition. EX. Class item { int number; float cost;
  • 5. Inside the class definition Public: Void getdata(int a, float b); Void putdata(void) { cout<< number<<“n”; cout<<cost<<“n”; } };
  • 6. Data Member  Data member depends on access control of data member.  If it’s public then data member can be easily accessed using the direct member access operator with the object of that class.  If the data member is defined as private or protected then we can not access the data variables directly.
  • 7. Data Member Type of data Member 1.Public 2.Private 3.protected
  • 8. Data member type #include <iostream> using namespace std; class alpha{ private: int id; static int count; public: alpha(){count++; id=count;}}
  • 9. Data member type void print(){ cout<<"My id is"<<id; cout<<"countis"<<count;} }; int alpha : :count=0; void main (){ alpha a1,a2,a3; a1.print(); a2.print(); a3.print();}
  • 10. Friend Function  A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class.  Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
  • 11. Friend Function Example: class Box { double width; public: double length; friend void printWidth( Box box ); voidsetWidth(doublewid); };
  • 12. Friend Function Property 1) Friend of the class can be member of some other class. 2) Friend of one class can be friend of another class or all the classes in one program, such a friend is known as GLOBAL FRIEND. 3) Friend can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object.
  • 13. Friend Function Property 4) Friends are non-members hence do not get “this” pointer. 5) Friends, can be friend of more than one class, hence they can be used for message passing between the classes. 6) Friend can be declared anywhere (in public, protected or private section) in the class.
  • 14. Friend Class  A class can also be declared to be the friend of some other class.  When we create a friend class then all the member functions of the friend class also become the friend of the other class.  This requires the condition that the friend becoming class must be first declared or defined (forward declaration).
  • 15. Friend Class Example #include <iostream> using namespace std; class MyClass{ friend class SecondClass; public: MyClass() : Secret(0){} void printMember() { cout << Secret << endl;} private: int Secret; }; }
  • 16. Friend Class Example class SecondClass{ public: void change( MyClass& yourclass, int x ){ yourclass.Secret = x;} }; void main() { MyClass my_class; SecondClass sec_class; my_class.printMember(); sec_class.change( my_class, 5 ); my_class.printMember();}
  • 17. Array of object class A { int* myArray; A() { myArray = 0; } A(int size) {
  • 18. Array of object myArray = new int[size]; } ~A() { delete [] myArray; } }
  • 19. Returning Objects from Functions #include <iostream> using namespace std; class Complex{ private: int real; int imag; public: Complex(): real(0), imag(0){ } void Read() {
  • 20. Returning Objects from Functions cout<<"Enter real and imaginary number respectively:"<<endl; cin>>real>>imag; } Complex Add(Complex comp2) { Complex temp; temp.real=real+comp2.real; temp.imag=imag+comp2.imag; return temp;
  • 21. Returning Objects from Functions } void Display() { cout<<"Sum="<<real<<"+"<<imag<<"i"; } }; int main() {
  • 22. Returning Objects from Functions Complex c1,c2,c3; c1.Read(); c2.Read(); c3=c1.Add(c2); c3.Display(); return 0; }
  • 23. Nested Classes A class can be declared within the scope of another class. Such a class is called a "nested class. " Nested classes are considered to be within the scope of the enclosing class and are available for use within that scope.  To refer to a nested class from a scope other than its immediate enclosing scope, you must use a fully qualified name.
  • 24. Nested Classes Example #include <iostream.h> class Nest { public: class Display { private: int s; public: void sum( int a, int b) {
  • 25. Nested Classes Example s =a+b; } void show( ) { cout << "nSum of a and b is:: " << s; } }; }; void main() { Nest::Display x; x.sum(12, 10); x.show(); }
  • 26. Namespaces  Namespace is a new concept introduced by the ANSI C++ standards committee.  This defines a scope for the identifiers that are used in a program.  For using the identifier defined in the namespace scope we must include the using directive, like  Using namespace std;
  • 27. Namespaces  Here, std is the namespace where ANSI C++ standard class libraries are defined.  All ANSI C++ programs must include this directive.  This will bring all the identifiers defined in std to the current global scope. Using and namespace are the new keyword of C++.
  • 28. Constructor  A constructor (having the same name as that of the class) is a member function which is automatically used to initialize the objects of the class type with legal initial values.
  • 29. Characteristics of Constructor (i) These are called automatically when the objects are created. (ii) All objects of the class having a constructor are initialized before some use. (iii) These should be declared in the public section for availability to all the functions.
  • 30. Characteristics of Constructor (iv) Return type (not even void) cannot be specified for constructors. (v) These cannot be inherited, but a derived class can call the base class constructor. (vi) These cannot be static.
  • 31. Characteristics of Constructor (x) An object of a class with a constructor cannot be used as a member of a union. (xi) A constructor can call member functions of its class. (xii) We can use a constructor to create new objects of its class type by using the syntax
  • 32. Characteristics of Constructor (vii) Default and copy constructors are generated by the compiler wherever required. Generated constructors are public. (viii) These can have default arguments as other C++ functions. (ix) A constructor can call member functions of its class.
  • 33. Types of Constructor 1 Overloaded Constructors 2 Copy Constructor 3 Dynamic Initialization of Objects 4 Constructors and Primitive Types 5 Constructor with Default Arguments
  • 34. 1.Overloaded Constructors  Besides performing the role of member data initialization, constructors are no different from other functions.  This included overloading also. In fact, it is very common to find overloaded constructors.  For example, consider the following program with overloaded constructors for the figure class :
  • 36. 1.Overloaded Constructors Figure (float s1, floats2, float s3) { ide1=s1; side2=s2; side3=s3; radius=0.0; strcpy(shape,”triangle”); } Char shape[10]; Public: figure(float r)
  • 37. 1.Overloaded Constructors { radius=r; strcpy (shape, “circle”); } void area() { float ar,s; if(radius==0.0) { a
  • 38. 1.Overloaded Constructors if (side3==0.0) ar=side1*side2; else ar=3.14*radius*radius; cout<<”nnArea of the “<<shape<<”is :”<<ar<<”sq.unitsn”; } }; Void main() { Clrscr();
  • 39. 1.Overloaded Constructors Figure circle(10.0); / Figure rectangle15.0,20.6); Figure Triangle(3.0, 4.0, 5.0); Rectangle.area(); Triangle.area(); Getch();//freeze the monitror }
  • 40. 2.Copy Constructor  It is of the form classname (classname &) and used for the initialization of an object form another object of same type. For example,
  • 41. 2.Copy Constructor Class fun { Float x,y; Public: Fun (floata,float b) { x = a; y = b; } Fun (fun &f) {
  • 42. 2.Copy Constructor cout<<”ncopy constructor at workn”; X = f.x; Y = f.y; } Void display (void) { { Cout<<””<<y<<end1; } };
  • 43. 3.Dynamic Initialization of Objects  The class objects can be initialized at run time (dynamically).  We have the flexibility of providing initial values at execution time.
  • 44. 3.Dynamic Initialization of Objects #include <iostream.h> #include <conio.h> Class employee { Int empl_no; Float salary; Public: Employee() {} Employee(int empno,float s) { Empl_no=empno; Salary=s; }
  • 45. 3.Dynamic Initialization of Objects Employee (employee &emp) { Cout<<”ncopy constructor workingn”; Empl_no=emp.empl_no; Salary=emp.salary; } Void display (void) { Cout<<”nEmp.No:”<<empl_no<<”salary:”<<salary<< end1; }
  • 46. 3.Dynamic Initialization of Objects }; Void main() { int eno; float sal; clrscr(); cout<<”Enter the employee number and salaryn”; cin>>eno>>sal; employee obj1(eno,sal);
  • 47. 3.Dynamic Initialization of Objects cout<<”nEnter the employee number and salaryn”; cin>eno>>sal; employee obj2(eno,sal); obj1.display(); employee obj3=obj2; obj3.display(); getch(); }
  • 48. 4.Constructors and Primitive Types  In C++, like derived type, i.e. class, primitive types (fundamental types) also have their constructors.  Default constructor is used when no values are given but when we given initial values, the initialization take place for newly created instance.  Example:  float x,y;  int a(10), b(20);  float i(2.5), j(7.8);
  • 49. 4.Constructors and Primitive Types Class add { Private: Int num1, num2,num3; Public: Add(int=0,int=0); Void enter (int,int); Void sum(); Void display(); };
  • 50. 4.Constructor with Default Arguments add::add(int n1, int n2) { num1=n1; num2=n2; num3=n0; } Void add ::sum() { Num3=num1+num2; } Void add::display () { Cout<<”nThe sum of two numbers is “<<num3<<end1; }
  • 51. Destructor  The syntax for declaring a destructor is : -name_of_the_class() { }  It does not take any parameter nor does it return any value. Overloading a destructor is not possible and can be explicitly invoked. I  n other words, a class can have only one destructor. A destructor can be defined outside the class. The following program illustrates this concept :
  • 52. Destructor #include<iostream.h> #include<conio.h> class add { private : int num1,num2,num3; public : add(int=0, int=0); void sum(); void display(); ~ add(void); };
  • 53. Destructor Add:: ~add(void) { Num1=num2=num3=0; Cout<<”nAfter the final execution, me, the object has entered in the” <<”ndestructor to destroy myselfn”; } Add::add(int n1,int n2) { num1=n1; num2=n2; num3=0; }
  • 54. Destructor Void add::sum() { num3=num1+num2; } Void add::display () { Cout<<”nThe sum of two numbers is “<<num3<<end1; } void main() {
  • 56. Characteristics of Destructors (i) These are called automatically when the objects are destroyed. (ii) Destructor functions follow the usual access rules as other member functions. (iii) These de-initialize each object before the object goes out of scope. (iv) No argument and return type (even void) permitted with destructors.
  • 57. Characteristics of Destructors (v) These cannot be inherited. (vi) Static destructors are not allowed. (vii) Address of a destructor cannot be taken. (viii) A destructor can call member functions of its class. (ix) An object of a class having a destructor cannot be a member of a union.
  • 58. REFRENCES • Learn Programming in C++ By Anshuman Sharma, Anurag Gupta, Dr.Hardeep Singh, Vikram Sharma