OOPS Notes - Unit-4
OOPS Notes - Unit-4
Credits: 4
• 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.
aa
• Then how we do it in OOPS??
By using member function using dot operator
Constructors and Destructors >> 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
• Parameterized Constructor is a type of constructor whose name is same as class name and is automatically
invoked. A Parameterized constructor has arguments.
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
2. Demonstration Program
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.
•The destructor is only one way to destroy the object created by the constructor. Hence destructor can-not be
overloaded.
•Destructor release memory space occupied by the objects created by the constructor.
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