65fd1506c9f69OOP LAB 4
65fd1506c9f69OOP LAB 4
LAB MANUAL # 4
Objective:
➢ Object Composition
➢ Operator Overloading
Object Composition
In C++, object composition is achieved by using classes and objects. A class can
have member variables that are instances of other classes. This creates a "has-a"
relationship between the classes. For example, a Car class might have an Engine
class as a member variable. The Car class "has an" engine.
In object-oriented programming languages, object composition is used for objects
that have a “has-a” relationship with each other. Therefore, the complex object is
called the whole or a parent object whereas a simpler object is often referred to as a
child object.
class B
{
A objA;
public:
B(arg-list) : objA(arg-list1);
};
In the classes given above, B uses objects of class A as its data members. Hence, B
is a complex class that uses a simple class A. Let’s have a look at the program that
makes use of the composition.
Below is the implementation of the composite class:
#include <iostream>
class Engine {
public:
void start() {
std::cout << "Engine started!" << std::endl;
}
};
class Car {
private:
Engine engine; // Car has an Engine
public:
void turnOn() {
engine.start();
}
};
int main() {
Car myCar;
myCar.turnOn(); // Call start() through Car's engine member
return 0;
}
In this example, the Car class composes an Engine object. The Car class
can then call the start () method of the engine to simulate turning on the
car. This demonstrates how composition allows you to create complex
objects from simpler ones.
result = c1 + c2;
result = c1.addNumbers(c2);
Here,
Unary operators operate on only one operand. The increment operator ++ and
decrement operator -- are examples of unary operators.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
Here, when we use ++count1; , the void operator ++ () is called. This increases
the value attribute for the object count1 by 1.
The above example works only when ++ is used as a prefix. To make ++ work as a
postfix we use this syntax.
Notice the int inside the parentheses. It's the syntax used for using unary operators as
postfix; it's not a function parameter.
The operator function is called using the obj1 object and obj2 is passed as an argument
to the function.
#include <iostream>
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
void print() {
std::cout << real << " + " << imag << "i" << std::endl;
}
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
c3.print(); // Output: 6 + 8i
return 0;
}
However,
• using & makes our code efficient by referencing the complex2 object instead of
making a duplicate object inside the operator function.
• using const is considered a good practice because it prevents the operator
function from modifying complex2 .
Things to Remember in C++ Operator Overloading
1. Two operators = and & are already overloaded by default in C++. For example,
to copy objects of the same class, we can directly use the = operator. We do not
need to create an operator function.
2. Operator overloading cannot change the precedence and associativity of
operators. However, if we want to change the order of evaluation, parentheses
should be used.
3. There are 4 operators that cannot be overloaded in C++. They are:
a. :: (scope resolution)
b. . (member selection)
c. .* (member selection through pointer to function)
d. ?: (ternary operator)