3 Constructors And Destructors

Object Oriented Programming · Unit 3

Constructors and Destructors

Exam-focused notes for Constructors and Destructors (Object Oriented Programming, BIT153): what the TU syllabus asks and how it has actually been tested, with 7 solved past questions from this unit.

What this unit covers

  • Constructor definition and purpose
  • Types of constructors: default, parameterized, copy
  • Constructor overloading
  • Initialization of data members
  • Destructor definition and characteristics
  • Constructor and destructor execution order
  • Constructor invocation in derived classes

Constructor overloading

208210 marks

Write a program to create a class Student with data members id, name, and marks. Define constructors to initialize data members and demonstrate constructor overloading. Implement member functions to read students details, display them and calculate average marks.[10]

Constructor overloading means defining multiple constructors in the same class with different parameter lists. The compiler selects the appropriate constructor based on the arguments passed during object creation. --- --- --- Constructor Parameters Purpose ...

Full solved answer →
2080.15 marks

What is constructor? Write a program to demonstrate constructor overloading in C++. [1+4]

A constructor is a special member function of a class that is automatically called when an object of that class is created. It has the same name as the class and has no return type (not even void). Its main purpose is to initialize the data members of an ob...

Full solved answer →

Constructor definition and purpose

208010 marks

What is constructor? Explain its types with suitable example.How is the constructor called in a derived class in C++?[6+4]

A constructor is a special member function of a class that is automatically called when an object of that class is created. It is used to initialize the data members of the class. - Has the same name as the class - Has no return type (not even void) - Calle...

Full solved answer →
207810 marks

Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. A main() program should create two initialized time objects (should they be const) and one that isn't initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally, it should display the value of this third variable Make appropriate member functions const.[10]

--- --- Field t1 t2 Raw Sum Carry Final ---------------------------------------- Seconds 30 45 75 1 min 15 Minutes 45 20 65 + 1 = 66 1 hr 06 Hours 5 6 11 + 1 = 12 -- 12 Result: 12:06:15 -- correct. --- Concept Detail ------ Default Constructor Sets hours = ...

Full solved answer →
010 marks

Write a program to create a class Customer with data members customer Id, Name, and age. Put member functions to read and display values of data members. Also define constructors to initialize data members with default value. Finally create object to read and display data of 10 customers.[10]

A class in C++ is a user-defined data type that bundles data members and member functions together. A constructor is a special member function that is automatically called when an object is created, used to initialize data members. --- --- --- Concept Descr...

Full solved answer →

Types of constructors

20785 marks

Describe types of constructors with an example. [5]

A constructor is a special member function that has the same name as the class, has no return type, and is automatically called when an object is created. --- A constructor that takes no parameters. It is called automatically when an object is created witho...

Full solved answer →

Destructor definition and characteristics

05 marks

What is destructor? Discuss its characteristics with suitable example. [5]

A destructor is a special member function of a class that is automatically called when an object goes out of scope or is explicitly deleted. It is used to release resources (such as memory, file handles, etc.) that were acquired by the object during its lif...

Full solved answer →