0% found this document useful (0 votes)
22 views8 pages

65fd1506c9f69OOP LAB 4

Object oriented programming

Uploaded by

bscs23f06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

65fd1506c9f69OOP LAB 4

Object oriented programming

Uploaded by

bscs23f06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Namal University Mianwali

Department of Computer Science

LAB MANUAL # 4

Subject: OOP Instructor Name: Ammar Ahmad Khan

Code: CSC-101 Lab Timings: 10:30 AM to 01:00 PM (FRIDAY)

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.

Benefits of using object composition in C++:


Improved code organization: Complex objects are broken down into smaller,
more manageable pieces.
Increased code reusability: Existing objects can be used as building blocks for
new classes.
Enhanced maintainability: Code becomes easier to understand and modify.
Syntax:
class A
{
// body of a class
};

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.

C++ Operator Overloading


In C++, we can change the way operators work for user-defined types like objects
and structures. This is known as operator overloading. For example,
Suppose we have created three objects c1, c2 and result from a class named
Complex that represents complex numbers.
Since operator overloading allows us to change how operators work, we can
redefine how the + operator works and use it to add the complex numbers of c1
and c2 by writing the following code:

result = c1 + c2;

instead of something like

result = c1.addNumbers(c2);

Syntax for C++ Operator Overloading


To overload an operator, we use a special operator function. We define the
function inside the class or structure whose objects/variables we want the
overloaded operator to work with.
class className {
... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};

Here,

• returnType is the return type of the function.


• operator is a keyword.
• symbol is the operator we want to overload. Like: + , < , - , ++ , etc.
• arguments is the arguments passed to the function.

Operator Overloading in Unary Operators

Unary operators operate on only one operand. The increment operator ++ and
decrement operator -- are examples of unary operators.

Example1: ++ Operator (Unary Operator) Overloading


// Overload ++ when used as prefix

#include <iostream>
using namespace std;

class Count {
private:
int value;

public:

// Constructor to initialize count to 5


Count() : value(5) {}

// Overload ++ when used as prefix


void operator ++ () {
++value;
}

void display() {
cout << "Count: " << value << endl;
}
};

int main() {
Count count1;

// Call the "void operator ++ ()" function


++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.

void operator ++ (int) {


// code
}

Notice the int inside the parentheses. It's the syntax used for using unary operators as
postfix; it's not a function parameter.

Operator Overloading in Binary Operators

Binary operators work on two operands. For example,


result = num + 9;

Here, + is a binary operator that works on the operands num and 9 .


When we overload the binary operator for user-defined types by using the code:

obj3 = obj1 + obj2;

The operator function is called using the obj1 object and obj2 is passed as an argument
to the function.

Example 4: C++ Binary Operator Overloading

#include <iostream>

class Complex {
private:
double real, imag;

public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

Complex operator+(const Complex& other) {


return Complex(real + other.real, imag + other.imag);
}

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;
}

In this program, the operator function is:

Complex operator + (const Complex& obj) {


// code
}

Instead of this, we also could have written this function like:

Complex operator + (Complex obj) {


// code
}

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)

You might also like