3 Classes Objects

Object Oriented Programming · Unit 3 · 8 hrs

Classes & Objects

Exam-focused notes for Classes & Objects (Object Oriented Programming, CSC166): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.

What this unit covers

  • A Simple Class and Object
  • Accessing members of class
  • Initialization of class objects: (Constructor, Destructor)
  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor
  • The Default Copy Constructor
  • Objects as Function Arguments
  • Returning Objects from Functions
  • Structures and Classes
  • Memory allocation for Objects
  • Static members
  • Member functions defined outside the class

Initialization of class objects

208010 marks

What is constructor? Why constructor is needed in a class? Illustrate the types of constructor with an example.[10]

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 is used to initialize the data members of the class. - It has the same name as the class - It h...

Full solved answer →
207910 marks

What is the use of constructor and destructor? Write a program for illustrating default constructor, parameterized constructor and copy constructor.[10]

--- A constructor is a special member function of a class that is automatically called when an object is created. Its main uses are: - Automatic initialization of data members when an object is created - Eliminates the need to manually initialize each objec...

Full solved answer →
20765 marks

What is destructor? Write a program to show the destructor call such that it prints the message 'memory is released'. [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 memory and other resources that were allocated to the object during its lifetime. Feature Desc...

Full solved answer →
2080.110 marks

What is destructor? List its characteristics. Explain the use of default copy constructor with an appropriate example.[10]

--- A destructor is a special member function of a class that destructs or deletes an object and performs cleanup operations (such as releasing memory) when the object is no longer needed. A destructor is called automatically when: - The function ends (obje...

Full solved answer →

Static members

20805 marks

Write a C++ program to display the number of objects created using static member. [5]

A static data member is shared by all objects of a class. It is initialized only once and retains its value throughout the program. It is used here to count how many objects have been created, because every time a constructor is called (i.e., an object is c...

Full solved answer →
207810 marks

Write a program according to the specification given below: - Create a class Account with data members acc_no, balance, and min_balance(static) - Include methods for reading and displaying values of objects - Define static member function to display min_balance - Create array of objects to store data of 5 accounts and read and display values of each object[10]

--- --- - Declared inside the class with keyword static - Only one copy is shared among all objects of the class - Must be defined and initialized outside the class: - Declared with keyword static - Can be called without creating an object, using the class ...

Full solved answer →
20755 marks

Create a real scenario where static data members are useful. Explain with suitable program. [5]

A static data member is a class member that is shared by all objects of that class. It is declared using the static keyword inside the class. Unlike regular data members (where each object has its own copy), a static data member has only one copy in memory,...

Full solved answer →

A Simple Class and Object

20795 marks

Define class and object with suitable examples. How members of class can be accessed? [5]

A class is a user-defined data type that contains data members and member functions together. It acts as a blueprint or template for creating objects. Like structures and unions in C, a class groups related data and functions, but adds the concept of access...

Full solved answer →
2080.110 marks

Write a program to realize the above hierarchy. Create necessary member functions (constructor) to initialize and display necessary information.[10]

Based on the reference context, we need to create a hierarchy where a derived class inherits from a base class, and use constructors to initialize and display member variables of both classes. --- --- --- --- Concept Description ------ Base Class A Contains...

Full solved answer →

Default Constructor

20785 marks

What is constructor? Explain the concept of default and default copy with suitable example. [5]

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. Key characteristics: - Has the same name as the class - Has no return type (n...

Full solved answer →

The Default Copy Constructor

20755 marks

Explain the default action of the copy constructor. Write a suitable program that demonstrates the technique of overloading the copy constructor. [5]

A copy constructor is a special constructor that creates a new object as a copy of an existing object of the same class. It is called automatically when: - An object is initialized with another object of the same class - An object is passed by value to a fu...

Full solved answer →

Objects as Function Arguments

2080.15 marks

What is the benefit of passing object as arguments? Write a program to create a class named actor with data members name and rating. Initialize the data members and display those names whose rating is greater than 5 using the concept of constant object. [5]

As stated in the notes, passing objects as arguments to member functions is useful in the following ways: - Initialization of data members: When we want to initialize all data members of one object with another object, we can pass objects and assign the val...

Full solved answer →