Uploaded by Fatima Khalil

OOP LAB 10

advertisement
LAB TASKS
1. What is operator overloading?
Operator Overloading is an efficient way of performing arithmetic operations on objects of class just
as for the built-in data types.
2. Is it possible to overload “+” operator for data type int?
No, it is not possible.
3. How do we differentiate between prefix and postfix increment operator while
overloading them?
In prefix increment operator overloading, no arguments are provided whereas in case of post fix, int is
provided as an argument.
4. What is the syntax of overloading “*” operator for a class Matrix. (You don’t need to
write the body of function.
5. Go through example 5.2 of the manual and write output of given code segment.
counter c1(5), c2(10), c3;
c3 = c1++;
c2 = --c3;
7
6
TASK # 02
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 increment operators to increment the minutes by one. Also
make sure if minutes are 59 and you increment the Time it should update hours and minutes
accordingly.
d. Provide a function display () to display the hours and minutes.
e. In main (), create two objects of Time, initialize them using constructors and call the display
functions.
f. Test the following in your main:
a. T3= ++T1;
b. T4=T2++;
g. Print your name, registration number, section, semester in main using cout statements.
TASK # 02
#include <iostream>
using namespace std;
class Time
{
int hours;
int minutes;
public:
Time ():hours(0),minutes(0)
{
}
Time (int h,int m):hours(h),minutes(m)
{
}
Time operator ++()
{
++minutes;
if (minutes>=60)
{
minutes=minutes-60;
++hours;
}
return Time (hours,minutes);
}
Time operator ++(int)
{
minutes++;
if (minutes>=60)
{
minutes=minutes-60;
hours++;
}
return Time (hours,minutes);
}
void display ()
{
cout<<"HOURS: "<<hours<<"\t"<<"MINUTES:"<<minutes<<"\n";
}
};
int main ()
{
Time T1,T2,T3,T4;
T3=++T1;
T4=T2++;
T3.display ();
T4.display ();
cout<<"\nNAME: FATIMA KHALIL ROLL NO: 2021-BEE-005 SEMESTER:II";
return 0;
}
Download