Object Oriented Programming EE-123L: Dha Suffa University Department of Electrical Engineering - Semester III
Object Oriented Programming EE-123L: Dha Suffa University Department of Electrical Engineering - Semester III
Lab #08
Name : _________________________________________
Date : _________________________________________
Remarks : _________________________________________
Signature : _________________________________________
Object Oriented Programming EE-123L
Object Oriented Programming EE-123L
Code/Notes
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;
}
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;
}
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: