Object Oriented Programming · Unit 2
Classes, Objects, and Data Members
Exam-focused notes for Classes, Objects, and Data Members (Object Oriented Programming, BIT153): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.
Classes, Objects, and Data Members in short
A class is the base of all C++ object-oriented code. This chapter shows how to declare a class, control access to its members, create objects and pass them to and from functions.
What this unit covers
- Class definition and declaration
- Data members and member functions
- Access specifiers: public, private, protected
- Objects as instances of classes
- Objects as function arguments
- Returning objects from functions
Returning objects from functions
2081 · 5 marks
How do you return object in function? Describe with an example. [5]
2080 · 5 marks
Write a program to demonstrate returning object from functions in C++. [5]
More from this unit
2079 · 10 marks
Explain the chain of constructor and destructor between sub classes and super classes during inheritance with example.Create a class named 'Point' with data members x and y. Overload the + operator to add the value of two objects of this class.[5+5]
Show solved answerHide answer
Chain of Constructor and Destructor in Inheritance + Operator Overloading
(a) Chain of Constructor and Destructor in Inheritance
Concept
In C++ inheritance, when an object of a derived (sub) class is created or destroyed, constructors and destructors are called in a specific order involving both the base (super) class and the derived class.
Constructor Chaining (Order of Execution)
When a derived class object is created:
- Base class constructor is called FIRST
- Derived class constructor is called SECOND
This ensures that the base part of the object is fully initialized before the derived part uses it.
Destructor Chaining (Order of Execution)
When a derived class object is destroyed:
- Derived class destructor is called FIRST
- Base class destructor is called SECOND
The destructor order is the exact reverse of constructor order.
Constructor Order: Base --> Derived
Destructor Order: Derived --> Base
Example
#include <iostream>
using namespace std;
// Super class (Base class)
class Animal {
public:
Animal() {
cout << "Animal Constructor called" << endl;
}
~Animal() {
cout << "Animal Destructor called" << endl;
}
};
// Sub class (Derived class)
class Dog : public Animal {
public:
Dog() {
cout << "Dog Constructor called" << endl;
}
~Dog() {
cout << "Dog Destructor called" << endl;
}
};
// Further derived class
class Puppy : public Dog {
public:
Puppy() {
cout << "Puppy Constructor called" << endl;
}
~Puppy() {
cout << "Puppy Destructor called" << endl;
}
};
int main() {
cout << "Creating Puppy object:" << endl;
Puppy p;
cout << "\nObject going out of scope:" << endl;
return 0;
}
Output
Creating Puppy object:
Animal Constructor called
Dog Constructor called
Puppy Constructor called
Object going out of scope:
Puppy Destructor called
Dog Destructor called
Animal Constructor called
Summary Table
| Event | Order |
|---|---|
| Object Creation | Base --> Intermediate --> Derived |
| Object Destruction | Derived --> Intermediate --> Base |
Key Rule: Constructors run from top (most base) to bottom (most derived). Destructors run from bottom (most derived) to top (most base).
(b) Class Point with Overloaded + Operator
Concept of Operator Overloading
Operator overloading allows C++ operators (like +, -, *) to work with user-defined objects. The keyword operator is used to define the overloaded function.
Program
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
// Constructor
Point(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
// Overload + operator
Point operator+(Point p) {
Point temp;
temp.x = this->x + p.x;
temp.y = this->y + p.y;
return temp;
}
// Display function
void display() {
cout << "Point(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(3, 4);
Point p2(5, 6);
Point p3;
cout << "Point 1: ";
p1.display();
cout << "Point 2: ";
p2.display();
// Using overloaded + operator
p3 = p1 + p2;
cout << "Sum of Points: ";
p3.display();
return 0;
}
Output
Point 1: Point(3, 4)
Point 2: Point(5, 6)
Sum of Points: Point(8, 10)
Explanation
| Step | Description |
|---|---|
Point p1(3, 4) | Creates object p1 with x=3, y=4 |
Point p2(5, 6) | Creates object p2 with x=5, y=6 |
p1 + p2 | Calls p1.operator+(p2) |
| Inside operator+ | temp.x = 3+5 = 8, temp.y = 4+6 = 10 |
Returns temp | Result stored in p3 |
Note: The
operator+function takes one argument (the right-hand operand) because the left-hand operand is the calling object (this). The function returns a newPointobject containing the sum.
Objects as function arguments
2078 · 5 marks
Explain use of objects as function arguments with example [5]
Make Unit 2 stick
Practice BIT153 with flashcards & quizzes