Você está na página 1de 3

Lab Journal – Lab 7

Object Oriented Programming

Lab Journal - Lab 7


Name: _________________________________

Enrollment #: _________________________________

Class: _________________________________

Objective

This lab session is dedicated to operator overloading in C++. You will learn the syntax of
overloading arithmetic and relational operators and implement them.

Go through Lab 6 of the manual, attempt the given programs and verify their outputs. Once you
are done, perform the following tasks.

Tasks :

Answer the following/Write C++ code where required.

1. What is operator overloading?

2. Is it possible to overload the ‘+’ operator for data type int?

3. How do we differentiate between prefix and postfix increment operator while


overloading them?

Object Oriented Programming Page 1


Lab Journal – Lab 7

4. What is the syntax of overloading ‘*’ operator for a class Matrix. (You do not need to
write the body of the function)

5. Go through example 6.2 of the manual and write the output of the given code segment.

counter c1(5), c2(10), c3;


c3=c1++;
c2=--c3;;
cout<<”\n”<<c1.get_count();
cout<<”\n”<<c2.get_count();
cout<<”\n”<<c3.get_count();

Exercise 1

Write a complete C++ program with the following features.


a. Declare a class Time with two fields, hour and minute.
b. Provide appropriate constructors to initialize the data members.
c. Overload the pre and postfix increm ent operators (using member
functions) to increment the minutes by one. Also make sure if m inutes are
59 and you increment the Time it should update hours and minutes
accordingly.
d. Provide a function display() to d isplay the hours and minutes.
e. In main(), create obj ects of Time, initialize them using constructors and
call the d isplay functions.
f. Test the fo llowing in your main:
a. T3= ++T1;
b. T4=T2++;

Object Oriented Programming Page 2


Lab Journal – Lab 7

Exercise 2

Write a class Complex to model complex numbers and overload the following
operato rs (using member functions).
a. ‘+’ and ‘–‘ o perators for addition and subtraction respectively.
b. ‘~’ operator to find th e conjugate of a complex number.
c. ‘*’ operator to multiply two complex numbers.
d. ‘!’ operato r to find th e magnitud e (absolute value) of a complex number.

Exercise 3

Using non-member functio ns, ov erload the following operators for the c lass
Complex.
a. The ‘>’ operator such that the statement c1>c2 should return true if th e real
part o f c1 is greater than that of c2.
b. The’==’ operator wh ich returns true if the two complex numbers are equal.

Implement the given exercises and get them evaluated by your instructor.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

3. Exercuse 3

+++++++++++++++++++++++++

Object Oriented Programming Page 3

Você também pode gostar