Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OOP Group A1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Department of Computer Engineering Subject : OOPCGL

Correctness of Documentation of Timely Dated Sign of


Write-up Program Program Viva Completion Total Subject Teacher
4 4 4 4 4 20

Date of Performance:..................................... Expected Date of Completion:.......................


Actual Date of Completion:..........................................

----------------------------------------------------------------------------------------------------------------

Assignment No: 1
----------------------------------------------------------------------------------------------------------------
Title of the Assignment: Arithmetic operations on complex numbers using
operator overloading.
----------------------------------------------------------------------------------------------------------------
Problem Statement:
1. Implement a class Complex which represents the Complex Number data type.
Implement the following operations:
1. Constructor (including a default constructor which creates the complex number
0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >>to print and read Complex Numbers.

----------------------------------------------------------------------------------------------------------------
Prerequisite:
1. Object Oriented Programming
---------------------------------------------------------------------------------------------------------------
Objective of the Assignment: To learn the concept of constructor, default constructor,
operator overloading using member function and friend function.
---------------------------------------------------------------------------------------------------------------
Outcomes:
1. Students should understand different types of Constructors.
Department of Computer Engineering Subject : OOPCGL

2. Students can able to overload the operators


---------------------------------------------------------------------------------------------------------------
Software & Hardware Requirements:
CPP Compiler
---------------------------------------------------------------------------------------------------------------
Theory:

Operator Overloading
It is a specific case of polymorphism where different operators have different implementations
depending on their arguments. In C++ the overloading principle applies not only to functions, but to
operators too. That is, of operators can be extended to work not just with built-in types but also classes.
A programmer can provide his or her own operator to a class by overloading the built-in operator to
perform some specific computation when the operator is used on objects of that class.

An Example of Operator Overloading


Complex a(1.2,1.3); //this class is used to represent complex numbers
Complex b(2.1,3); //notice the construction taking 2 parameters for the real and imaginary part
Complex c = a+b; //for this to work the addition operator must be overloaded
Arithmetic Operators
Arithmetic Operators are used to do basic arithmetic operations like addition, subtraction,
multiplication, division, and modulus.
The following table list the arithmetic operators used in C++.
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

With C++ feature to overload operators, we can design classes able to perform operations using
standard operators. Here is a list of all the operators that can be overloaded:

Over loadable operators

+ - * / = <> += -= *= /= <<>>

<<= >>= == !=<=>=++--% & ^ ! |


Department of Computer Engineering Subject : OOPCGL

~ &= ^= |= && || %= []

• The operator keyword declares a function specifying what operator-symbol means when applied to
instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler
distinguishes between the different meanings of an operator by examining the types of its operands.

Syntax:

return_typeclass_name :: operator op(arg_list)

//function body

where,

• Return type is the value returned by the specified operation

• op is the operator to be overload.

• op is proceeding by the keyword operator.

• operator op is the function name

Process of the overloading has 3 steps

1. Create a class that define a data types that is used in the overloading operation 2. Declare the operator
function operator op() in the public part of the class.

It may be either a member function or a friend function.

3. Define the operator function to implement the required operation

e.g.

Overloading Binary operators:

A statement like

C = sum (A, B); // functional notation

This functional notation can be replaced by a natural looking expression


Department of Computer Engineering Subject : OOPCGL

Complex No 1:

Real Part :5

Imaginary part :4

Complex No 2:

Real Part :3

Imaginary part :4

Facilities:

Linux Operating Systems, G++

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

Input:
Complex numbers with real and imaginary values for two complex numbers.
Example :
Conclusion:
Hence, we have studied the concept of operator overloading.

Questions:
1. What is operator overloading?
Department of Computer Engineering Subject : OOPCGL

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?

You might also like