0% found this document useful (0 votes)
17 views

OOPS Notes - Unit-4

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

OOPS Notes - Unit-4

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Course Name: Object Oriented Programming Systems (OOPS)

Course Code : CS/IT103

Credits: 4

Prepared by: Dr. Rakesh Chowdhury

Assistant Professor (EEE)


Email: [email protected]
UNIT-4: Constructors and Destructors
Constructors and Destructors >> Constructors

• Suppose when you write: 5

int a = 5; Built in data type

• With this code, you have created a variable “a” of int data type and also assigned value to it.
a
• In C++, we make classes and objects which is a user defined data type.

• Can you write ??


5
Not possible
Class (user defined data type)
Item x = 5;
object

aa
• Then how we do it in OOPS??
By using member function using dot operator
Constructors and Destructors >> Constructors

class item //main program


{ int main ( ) To include the features
int number;
float cost; {
of built in data type in
item x; //object x created user defined data type
Public:
void getdata (int a , float b);
cout<< “\nobject x” << \n”; i.e. we want some
x.getdata(100, 299.95) mechanism by which
void putdata (void); x.putdata(); we can initialize the
{
cout<<“Number : “ << number << \n”; item y; //another object y created data members
cout<<“Cost : “ << cost << \n”;
}
cout<< “\nobject y” << \n”; automatically by
};
y.getdata(200, 175.95) whatever value we
y.putdata();
want when the object
void item : : <getdata (int a , float b)
Return 0;
{ is created for the class.
number = a;
cost = b;
} To achieve this, we use
}
constructors
Constructors and Destructors >> Constructors

What are Constructors??


Constructors are special member function which is used to initialize the value of a variable
inside an object. It Construct the values for the object in a class.
Important points about a constructor are:
• Constructor is a special member function.
• A Constructor has the same name as its class name.
• A Constructor is automatically invoked as soon as an object of its class is created. Every time a new object is
created constructor is automatically called’
• A constructor has no any return type. Not even void return type.
• A Constructor allows default argument concept.
• A Constructor cannot be inherited

• A Constructor is always defined in public section.

• A Constructor cannot be virtual.


Constructors and Destructors >> Types of Constructors

Types of Constructors??
There are three types of constructors based on argument type:
• Default Constructor
All constructors have
• Parameterized Constructor all features discussed
in previous slide
• Copy constructor
Constructors and Destructors >> Default Constructors

• Default Constructor is a type of constructor whose name is same as class name and is automatically invoked. A
default constructor has no arguments.
• A Constructor can be defined inside or outside a function

class Demo int main ()


{ {
int a, b; Demo aa; // objects aa is created
public:
aa.putdata ( );
Demo ( ) // constructor declared
{
Return 0;
a = 10;
b = 15; }
}
void putdata(); There is no need to write any statement to invoke the constructor function
}; as we do in normal member functions. This is very helpful in case of large
number of objects

//Outside the class definition of constructor


Void Demo : : putdata()
Demo : : Demo ()
{
{
cout << “ a= “ << a << “b = “ << b;
………………..
}
}
Constructors and Destructors >> Parameterized Constructors

• Parameterized Constructor is a type of constructor whose name is same as class name and is automatically
invoked. A Parameterized constructor has arguments.

class Demo int main ()


{ {
int a, b; Demo aa (5, 10) // another method is Demo aa = Demo (5, 10);
public:
Demo (int x, int y ) // constructor declared aa.putdata ( );
{ Return 0;
a = x; }
b = y;
}
void putdata(); int main () // taking parameters from user
}; {
int x, y;
cout << “enter two numbers”
Void Demo : : putdata() cin >> x >> y;
{ Demo aa (x, y); // objects aa is created
cout << “ a= “ << a << “b = “ << b; aa.putdata ( );
} Return 0;
}
Constructors and Destructors >> Parameterized Constructors

What is parameterized constructor advantage?


The advantage of a parameterized constructor lies in its ability to customize object
initialization. By accepting parameters, the constructor enables the creation of objects with
specific attributes, providing flexibility and reusability in the code. This customization is
particularly useful when different instances of the same class need to be initialized with
distinct values.

Question on Constructor:
You are developing a program to manage information about rectangles. Define a class
named Rectangle to represent a rectangle, with private data members for the rectangle's
width and height. Include a constructor that initializes these details when a Rectangle object
is created. Implement a member function to calculate the area of the rectangle.
Constructors and Destructors >> Constructors

What is output?
Constructors and Destructors >> Constructors

What is output?
Constructors and Destructors >> Constructors

What is output?
Constructors and Destructors >> Constructors

• What is Copy Constructor?


• Demonstration program
• Problem
• Constructor Overloading
• Problem
• Destructor
• Demonstration
• Order of destruction
• Problem

1. What is Copy Constructor?


• When we need to initialize the variables of an object with the values of variables of another
object of same type, then we use the concept of copy constructor.
• In C++, a copy constructor is a special type of constructor that is used to create a new object
as a copy of an existing object of the same class.
• It is invoked whenever a new object is initialized with an existing object, either by direct
initialization or by passing the object by value.
• The purpose of a copy constructor is to ensure that the new object is a separate copy of the
original object, rather than just a reference to it.
• To define a copy constructor, Default or parametrized constructor should be defined
Constructors and Destructors >> Constructors

2. Demonstration Program

class Demo int main ()


{ {
int a; Demo aa;
public: • Address of Demo bb (aa); //Copy constructor
Demo ( ) object Demo cc = bb;
{ • No new object is aa.putdata ();
a = 10; created bb.putdata ();
} cc.putdata ();
Demo (Demo &zz)
{ return 0;
a = zz.a;
} }
void putdata( );
};
• Why “&” ??
Void Demo : : putdata()
{ • Why extra constructor is required?
cout << “ a= “ << a ;
} • What if “&” is not there??
Constructors and Destructors >> Constructors

What is output?
Default Constructor: 10
Copy Constructor: 11
Copy Constructor: 11
Constructors and Destructors >> Constructors

What is output?
Constructor class A
Hello
Hello
Constructors and Destructors >> Destructor

Destructor is an instance member function that is invoked automatically whenever an object is going to be
destroyed. Meaning, a destructor is the last function that is going to be called before an object is destroyed.

•A destructor is also a special member function like a constructor. Destructor destroys the class objects created by
the constructor.

•Destructor has the same name as their class name preceded by a tilde (~) symbol.

•It is not possible to define more than one destructor.

•The destructor is only one way to destroy the object created by the constructor. Hence destructor can-not be
overloaded.

•Destructor neither requires any argument nor returns any value.

•It is automatically called when an object goes out of scope.

•Destructor release memory space occupied by the objects created by the constructor.

•In destructor, objects are destroyed in the reverse of an object creation.


Constructors and Destructors >> Destructor

What is output?

Constructor: 1
Constructor: 2
Constructor: 3
Destructor: 3
Destructor: 2
Destructor: 1
Constructors and Destructors >> Destructor

What is output?

Constructor: 1
Constructor: 2
Constructor: 3
Constructor: 4
Destructor: 4
Constructor: 5
Destructor: 5
Destructor: 3
Destructor: 2
Destructor: 1
Constructors and Destructors >> Destructor

What is output?

Constructor: 1
Constructor: 2
Constructor: 3
Constructor: 4
Constructor: 5
Destructor: 3
Destructor: 2
Destructor: 1
Destructor: 5
Static Destructor: 4
Constructors and Destructors >> Destructor

What is output?
ABAB
Constructors and Destructors >> Destructor

What is output?
1211

You might also like