[go: up one dir, main page]

0% found this document useful (0 votes)
27 views4 pages

OOP Experiment No 1-1

this is experiment no .01 for sppu aids se

Uploaded by

gameralexander61
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)
27 views4 pages

OOP Experiment No 1-1

this is experiment no .01 for sppu aids se

Uploaded by

gameralexander61
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/ 4

Object Oriented Programming Language

Experiment No 1

Title: Implement a class Complex which represents the Complex Number data type. Implement the
following
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overload operator+ to add two complex numbers.
3. Overload operator* to multiply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers.

Theory:
Operator Overloading
Operator overloading is a feature in C++ that enables operators (such as +, -, etc.) to work
with user-defined data types. This mechanism is known as compile-time polymorphism and
provides the advantage of customizing operator behavior for different data types.
For example, we can overload "+" operator to perform addition on integers, concatenation on
strings, and addition on complex numbers. This enhances the versatility of operators,
allowing them to operate on awider range of data types.

What is operator function?


An operator function is a specialized type of function that provides an alternate
implementation for a particular operator. It is similar in syntax to a regular function, but its
name starts with the "operator" keyword followed by the operator symbol.
We can define multiple operator functions for the same operator, which can be differentiated
based on the number and type of operands they are used with. For example, the "+" operator
can have a different operator function implementation for integers, strings, and complex
numbers. This allows for the operator to be customized to meet specific requirements.

AI & DS Department Page 1


Object Oriented Programming Language

class ClassName
{
...
public
ReturnType operator OperatorSymbool(argument list)
{

// Implementation logic
}
... }

Return Type: Type of value returned by an operator function.


operator: Keyword used in programming languages to perform specific operations.
Operator Symbol: Symbol of the operator being overloaded in the program.
argument list: List of arguments passed to a function when it is called.

Operators that can be overloaded Examples


Binary Arithmetic +, -, *, /, %
Unary Arithmetic +, -, ++, —
Assignment =, +=,*=, /=,-=, %=
Bitwise & , | , << , >> , ~ , ^
De-referencing (->)
Dynamic memory allocation,
New, delete
De-allocation
Subscript []
Function call ()
Logical &, | |, !
Relational >, < , = =, <=, >=

AI & DS Department Page 2


Rules to Define the Operator Function

1. In the case of a non-static member function, the binary operator should have only one
argument and the unary should not have an argument.
2. In the case of a friend function, the binary operator should have only two arguments and the
unary should have only one argument.
3. Operators that cannot be overloaded are .* :: ?:
4. Operators that cannot be overloaded when declaring that function as friend function are = () [] -
>.
5. The operator function must be either a non-static (member function), global free function or a
friend function.

Three ways to implement operator overloading in C++

1. Operator overloading via member function


A member function is a function that is defined inside a class and acts upon objects of that
class. In regards to operator overloading, unary operators (operators that operate on a single
argument) have no arguments in their list, while binary operators (operators that operate on
two arguments) have one argument.

2. Operator overloading via friend function


A friend function is not a member of a class, but has direct access to the private and
protected members and can be declared in either the private or public section of the class. It
offers greater flexibility compared to member functions.
In other words, if the operator function needs access to the private and protected members of a
class, it can be defined as a friend function. In this case, unary operators have a single
argument, while binary operators have two arguments

3. Operator overloading via non-member function


Non-member function is not a member of the class and does not have access to the
private and protected members.

AI & DS Department Page 3


Algorithm:
Step 1: Start the program
Step 2: Create a class complex
Step 3: Define the default constructor.
Step 4: Declare the operator function which are going to be overloaded and display function
Step 5: Define the overloaded functions such as +, -,/,* and the display function
For Addition:
(a+bi) + (x + yi) = ((a+x)+(b+y)i)
For Multiplication:
(a+bi) * (x + yi) = (((a*x)-(b*y)) + ((a*y) + (x*b))i)
Step 6: Create objects for complex class in main() function
Step 7: Create a menu for addition, multiplication of complex numbers and display the result
Step 8: Depending upon the choice from the user the arithmetic operators will invoke the
overloaded operator automatically and returns the result
Step 9: Display the result using display function

FAQs

1. What is operator overloading?


2. What are the rules for overloading the operators?
3. State clearly which operators are overloaded and which operator are not overloaded?
4. State the need for overloading the operators.
5. Explain how the operators are overloaded using the friend function.
6. What is the difference between “overloading” and “overriding”?
7. What is operator function? Describe the syntax?
8. When is Friend function compulsory? Give an example?

AI & DS Department Page 4

You might also like