0% found this document useful (0 votes)
50 views

Object Oriented Programming EE-123L: Dha Suffa University Department of Electrical Engineering - Semester III

The document describes a lab assignment on operator overloading in C++. Students are asked to: 1. Create a class representing time with hours, minutes, seconds data. Include constructors and a function to add two time objects and display in 00:00:00 format. 2. Modify the program to overload the + operator to add two time objects instead of using an add function. 3. Further modify to overload the - operator to subtract two time objects.

Uploaded by

DaanyalSiingh
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)
50 views

Object Oriented Programming EE-123L: Dha Suffa University Department of Electrical Engineering - Semester III

The document describes a lab assignment on operator overloading in C++. Students are asked to: 1. Create a class representing time with hours, minutes, seconds data. Include constructors and a function to add two time objects and display in 00:00:00 format. 2. Modify the program to overload the + operator to add two time objects instead of using an add function. 3. Further modify to overload the - operator to subtract two time objects.

Uploaded by

DaanyalSiingh
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/ 7

Object Oriented Programming EE-123L

DHA SUFFA UNIVERSITY


Department of Electrical Engineering - Semester III

EE-123L Object Oriented Programming


Lab Manual Fall-2016

Lab #08

Name : _________________________________________

Roll No. : _________________________________________

Semester :_______________ Section: __________________

Date : _________________________________________

Remarks : _________________________________________

Signature : _________________________________________
Object Oriented Programming EE-123L
Object Oriented Programming EE-123L

Code/Notes

The following example overloads the ++ operator, to allow object to increment

Program #1
Class counter
{
private:
unsigned int count;
public:
counter():count(0)
{}
unsigned int get_count()
{return count;}
void operator ++()
{
++count;
}
};
int main()
{
counter c1,c2;
cout<<”\nc1=”<<c1.get_count();
cout<<”\nc2=”<<c2.get_count();
++c1;
++c2;
cout<<”\nc1=”<<c1.get_count();
cout<<”\nc2=”<<c2.get_count();
return 0;
}
Object Oriented Programming EE-123L

Program #2
//binary operator overloading
class number{
private:
int real;
int imag;
public:
number(){}
number(int x, int y)
{
real =x;
imag =y;
}

void operator + (number num)


{
real = real+ num.real;
imag = imag+ num.imag;
}
void display()
{
cout<<real<<"+j"<<imag<<endl;
}
};
int main()
{
number n1(2,4);
number n2(4,7);
n1+n2;
n1.display();
return 0;
}
Object Oriented Programming EE-123L

Operator Return Value

Program #1
Class counter
{
private:
unsigned int count;
public:
counter():count(0)
{}
unsigned int get_count()
{return count;}
counter operator ++()
{
++count;
counter temp;
temp.count=count;
return temp;
}
int main()
{
counter c1,c2;
cout<<”c1 =”<<c1.get_count();
cout<<”c1 =”<<c2.get_count();

++c1;
c2=++c1;
cout<<”c1 =”<<c1.get_count();
cout<<”c1 =”<<c2.get_count()<<endl;
return 0;
}
Object Oriented Programming EE-123L

Program #2
//Binary operator with return value
class number {
private:
int real;
int imag;
public:
number():real(0),imag(0.0)
{}
number(int x, int y)
{
real = x;
imag =y;
}

number operator + (number num)


{
number temp;
temp.real = real + num.real;
temp.imag = imag + num.imag;
return temp;
}
void display()
{
cout<<real<<"+j"<<imag<<endl;
}
};
int main()
{
number n1(6,6);
number n2(8,3);
number n3;
n3 = n1 + n2;
n3.display();
return 0;}
Object Oriented Programming EE-123L

Lab Task:

1. Create a class called “your name” (place your name over here) has separate int member
data for hours, minutes, and seconds. One constructor initialize this data to zero, and
another should initialize it to fix values. One member function should add two objects of
type “your name” passed as argument. Another member function should display it, in
00:00:00 format.
Write a main() program to create and initialized two objects of type (“your name”) and
one that isn’t initialized. Then it should add two initialized values together and assign the
result in third “your name” variable. Finally it should display the value of third variable.

2. Modify the above program so that instead of add function it uses the overloaded + operator
to add two variable types of “your name”.

3. Modify the above program so that instead of add function it uses the overloaded – operator
to subtract two variable types of “your name.

Home Task:

You might also like