0% found this document useful (0 votes)
4 views6 pages

Lec 5

Uploaded by

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

Lec 5

Uploaded by

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

Object Oriented

Lecture
Programing
5
Object (Pass by Value)

• Passing by value means that a copy of the object is


created and passed to the function. This means that
any changes made to the object within the function will
not affect the original object.
Example:
void myFunction(MyClass object)
{
// modify object as needed
}

MyClass myObject;
myFunction(myObject);
Object (Pass by Reference)

• Passing by reference, on the other hand, means that a


reference to the original object is passed to the
function. This means that any changes made to the
object within the function will also affect the original
object.
•void
Example:
myFunction(MyClass& object)
{
// modify object as needed
}
MyClass myObject;
myFunction(myObject);
Operator Overloading

Operator overloading in C++ allows you to define custom behavior


for operators when they are applied to instances of user-defined
classes. This makes it possible to use the standard arithmetic,
comparison, and other operators with objects of your own class,
just as you would with built-in data types like int and double.
• Arithmetic Operator (+, - , * ,/ )
Overloading
Arithmetic operator overloading in C++ is the process of defining
custom behavior for the standard arithmetic operators (+, -, *, /, %)
when they are applied to instances of user-defined classes.
Con…

Example:
class Complex
{
public: double real, imag;
Complex operator+(const Complex &other) const
{
Complex result;
result.real = real + other.real;
result.imag = imag + other.imag;
return result;
}
};
• Comparison Operator (==, <= , >= ,!= ) Overloading
Comparison operator overloading in C++ is the process of
defining custom behavior for the standard comparison operators
(==, !=, <, >, <=, >=) when they are applied to instances of
user-defined classes.

Example:
class Complex
{
public:
double real, imag;

bool operator==(const Complex &other) const


{
return real == other.real && imag == other.imag;
}
};

You might also like