KEMBAR78
Oop assignment 02 | PDF
Assignment No. 02
Object Oriented Programming
Student Name Nabeel Asghar
Registration Number FA20-BEE-012
Lecturer: Dr. Ajmal Khan
COMSATS UNIVERSITY ISLAMABAD
ATTOCK CAMPUS
Task#01: To the Distance class in the ENGLPLUS program in chapter 8, add an overloaded -
operator that subtracts two distances. It should allow statements like dist3=dist1-dist2; Assume
that the operator will never be used to subtract a larger number from a smaller one (that is, negative
distances are not allowed).
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
//Distance class
class Distance{
private:
int feet;
int inches;
public:
//Constructor
Distance(int x=0,int y=0){
feet=x;
inches=y;
}
//Setter Function
void set_distance(int x,int y){
while(y>11){
x++;
y=y-12;
}
feet=x;
inches=y;
}
//Display Method
void display() const{
cout<<feet<<"'"<<inches<<endl;
}
//Overloading -
Distance operator -(Distance d1){
Distance d2;
if(feet>d1.feet){
d2.feet=feet-d1.feet;
d2.inches=inches-d1.inches;
while(d2.inches<0){
d2.feet--;
d2.inches=abs(d2.inches);
}
}
else{
cout << "Subtraction not possible"<<endl;
}
return d2;
}
};
//Main Function
int main(){
int x,y;
Distance distance[3];
//Setting values
for(int i=0;i<2;i++){
cout << "Enter Feet of Distance " << i+1<<":";
cin >> x;
cout << "Enter Inches of Distance " << i+1<<":";
cin >> y;
distance[i].set_distance(x,y);
cout << endl;
}
system("cls");
//Displaying the distances
distance[0].display();
distance[1].display();
distance[2]=distance[0]-distance[1];
distance[2].display();
}
Task#02: Modify the time class from Exercise 3 in Chapter 6 so that instead of a function
add_time() it uses the overloaded + operator to add two times. Write a program to test this class.
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
//Time Class
class Time{
private:
int hours;
int minutes;
int seconds;
public:
//Constuctor
Time(){
hours=0;
minutes=0;
seconds=0;
}
//Setter Function
void set_time(int h,int m,int s){
hours=h;
minutes=m;
seconds=s;
}
//Sum Function
Time operator +(Time t2){
Time t3;
t3.hours=hours+t2.hours;
t3.minutes=minutes+t2.minutes;
t3.seconds=seconds+t2.seconds;
if(t3.seconds>59){
t3.minutes++;
t3.seconds=t3.seconds-60;
}
if(t3.minutes>59){
t3.minutes=t3.minutes-60;
t3.hours+=1;
}
if(t3.hours>23){
t3.hours=t3.hours-23;
}
return t3;
}
//Display Function
void display() const{
if(hours>=12){
hours-=12;
cout <<hours<<":"<<minutes<<":"<<seconds<<" PM"<<endl;
hours+=12;
}
else if(hours<12){
cout <<hours<<":"<<minutes<<":"<<seconds<<" AM"<<endl;
}
}
};
//Main Function
int main(){
Time t1,t2,t3;
t1.set_time(22,28,30);
t2.set_time(11,25,27);
cout<<"Time T1: ";
t1.display();
cout<<"Time T2: ";
t2.display();
t3=t1+t2;
cout<<"Time T3: ";
t3.display();
}
Task#03: Create a class Int based on Exercise 1 in Chapter 6. Overload four integer arithmetic
operators (+, -, *, and /) so that they operate on objects of type Int.Write a program to test this
class.
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
//Int Class
class Int{
private:
int x;
public:
//Constructor
Int(int y=0){
x=y;
}
//Setter Method
void set_data(int y){
x=y;
}
//Display Method
void display() const{
cout << x << endl;
}
//Overloading +
Int operator +(Int y){
Int temp;
temp.x=x+y.x;
return temp;
}
//Overloading -
Int operator -(Int y){
Int temp;
temp.x=x-y.x;
return temp;
}
//Overloading /
Int operator /(Int y){
Int temp;
temp.x=x/y.x;
return temp;
}
//Overloading *
Int operator *(Int y){
Int temp;
temp.x=x*y.x;
return temp;
}
};
//Main Function
int main(){
Int sum,sub,mul,div;
Int A,B;
int x;
//Initializing objects
cout<<"Enter value for A:";
cin>>x;
A.set_data(x);
cout<<"Enter value for B:";
cin>>x;
B.set_data(x);
system("cls");
cout << "A= ";
A.display();
cout << "B= ";
B.display();
//Using operators
sum=A+B;
sub=A-B;
mul=A*B;
div=A/B;
//Displaying results
cout << "Sum= ";
sum.display();
cout << "Subraction= ";
sub.display();
cout << "Multiplication= ";
mul.display();
cout << "Division= ";
div.display();
}
Task#04: Augment the time class referred to in Exercise 2 to include overloaded increment (++)
and decrement (--) operators that operate in both prefix and postfix notation and return values. Add
statements to main() to test these operators.
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
//Time Class
class Time{
private:
int hours;
int minutes;
int seconds;
public:
//Constructor
Time(){
hours=0;
minutes=0;
seconds=0;
}
//Setter Function
void set_time(int h,int m,int s){
hours=h;
minutes=m;
seconds=s;
}
//Error Correcting Method
void fix_time(Time t3){
while(t3.seconds>59){
t3.minutes++;
t3.seconds=t3.seconds-60;
}
while(t3.minutes>59){
t3.minutes=t3.minutes-60;
t3.hours+=1;
}
while(t3.hours>23){
t3.hours=t3.hours-24;
}
}
//Overloading Time++
Time operator ++(int){
Time t3;
t3.hours=hours++;
t3.minutes=minutes++;
t3.seconds=seconds++;
fix_time(t3);
return t3;
}
//Overloading ++Time
Time operator ++(){
Time t3;
t3.hours=++hours;
t3.minutes=++minutes;
t3.seconds=++seconds;
fix_time(t3);
return t3;
}
//Display Function
void display(){
if(hours>=12){
hours-=12;
cout <<hours<<":"<<minutes<<":"<<seconds<<" PM"<<endl;
hours+=12;
}
else if(hours<12){
cout <<hours<<":"<<minutes<<":"<<seconds<<" AM"<<endl;
}
}
};
//Main Function
int main(){
Time t1,t2,t3,t4;
t1.set_time(5,28,30);
t2.set_time(8,25,27);
cout<<"Time T1: ";
t1.display();
t2=t1++; //incremented
cout<<"Time T2: ";
t2.display();
t2=t1++; //incremented
cout<<"Time T2: ";
t2.display();
t2=++t1; //incremented
cout<<"Time T2: ";
t2.display();
}
Task#05: Add to the time class of Exercise 4 the ability to subtract two time values
using the overloaded (-) operator, and to multiply a time value by a number of type
float, using the overloaded (*) operator.
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
//Time Class
class Time{
private:
int hours;
int minutes;
int seconds;
public:
//Constructor
Time(){
hours=0;
minutes=0;
seconds=0;
}
//Setter Function
void set_time(int h,int m,int s){
hours=h;
minutes=m;
seconds=s;
}
//Error Correcting Method
void fix_time(Time t3){
while(t3.seconds>59){
t3.minutes++;
t3.seconds=t3.seconds-60;
}
while(t3.minutes>59){
t3.minutes=t3.minutes-60;
t3.hours+=1;
}
while(t3.hours>23){
t3.hours=t3.hours-24;
}
}
//Overloading -
Time operator -(Time t2){
Time t3;
t3.hours=hours-t2.hours;
t3.minutes=minutes-t2.minutes;
t3.seconds=seconds-t2.seconds;
fix_time(t3);
return t3;
}
//Overloading *
Time operator *(float num){
Time t3;
t3.hours=hours*num;
t3.minutes=minutes*num;
t3.seconds=seconds*num;
fix_time(t3);
return t3;
}
//Display Function
void display(){
if(hours>=12){
hours-=12;
cout <<hours<<":"<<minutes<<":"<<seconds<<" PM"<<endl;
hours+=12;
}
else if(hours<12){
cout <<hours<<":"<<minutes<<":"<<seconds<<" AM"<<endl;
}
}
};
//Main Function
int main(){
Time t1,t2,t3,t4;
t1.set_time(22,44,37);
t2.set_time(13,34,12);
cout<<"Time T1: ";
t1.display();
cout<<"Time T2: ";
t2.display();
cout<<endl;
t3=t1-t2;
cout<<"Time T1-T2: ";
t3.display();
t4=t1*89;
cout<<"Time T1*3: ";
t4.display();
}
Task#06: Design a class called NumDays . The class’s purpose is to store a value that represents
a number of work hours and convert it to a number of days. For example, 8 hours would be
converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to
2.25 days. The class should have a constructor that accepts a number of hours, as well as member
functions for storing and retrieving the hours and days.
The class should also have the following overloaded operators:
• + Addition operator.
• - Subtraction operator.
• ++ Prefix and postfix increment operators.
• -- Prefix and postfix decrement operators.
Code:
#include<iostream>
using namespace std;
//NumDays Class
class NumDays{
private:
int hours;
float days;
public:
//Constructor
NumDays(int x=0){
hours=x;
days=(float)hours/8;
}
//Setter Function
void set_hours(int x){
hours=x;
days=(float)hours/8;
}
//Retrieving Hours
int get_hours() const{
return hours;
}
//Retrieving Days
float get_days() const{
return days;
}
//Overloading +
NumDays operator +(NumDays x){
NumDays temp;
temp.hours=hours+x.hours;
temp.days=(float)temp.hours/8;
return temp;
}
//Overloading -
NumDays operator -(NumDays x){
NumDays temp;
temp.hours=hours-x.hours;
temp.days=(float)temp.hours/8;
return temp;
}
//Overloading ++Obj
NumDays operator ++(){
++hours;
days=(float)hours/8;
}
//Overloading Obj++
NumDays operator ++(int){
hours++;
days=(float)hours/8;
}
//Overloading --Obj
NumDays operator --(){
--hours;
days=(float)hours/8;
}
//Overloading Obj--
NumDays operator --(int){
hours--;
days=(float)hours/8;
}
//Display Function
void display() const{
cout<<"Hours="<<hours<<endl;
cout<<"Days="<<days<<endl<<endl;
}
};
//Main Function
int main(){
NumDays worker1(20),worker2(12),worker3;
//displaying original values
cout<<"Original Values"<<endl;
worker1.display();
worker2.display();
//pre and post increments
cout<<"Incremented Values"<<endl;
worker1++;
++worker2;
worker1.display();
worker2.display();
//pre and post decrements
cout<<"Decremented Values"<<endl;
worker1--;
--worker2;
worker1.display();
worker2.display();
//using + and - operators
cout<<"+ and - Operators"<<endl;
worker3=worker1+worker2;
worker3.display();
worker3=worker1-worker2;
worker3.display();
}

Oop assignment 02

  • 1.
    Assignment No. 02 ObjectOriented Programming Student Name Nabeel Asghar Registration Number FA20-BEE-012 Lecturer: Dr. Ajmal Khan COMSATS UNIVERSITY ISLAMABAD ATTOCK CAMPUS
  • 2.
    Task#01: To theDistance class in the ENGLPLUS program in chapter 8, add an overloaded - operator that subtracts two distances. It should allow statements like dist3=dist1-dist2; Assume that the operator will never be used to subtract a larger number from a smaller one (that is, negative distances are not allowed). Code: #include<iostream> #include<stdlib.h> using namespace std; //Distance class class Distance{ private: int feet; int inches; public: //Constructor Distance(int x=0,int y=0){ feet=x; inches=y; } //Setter Function void set_distance(int x,int y){ while(y>11){ x++; y=y-12; } feet=x; inches=y; }
  • 3.
    //Display Method void display()const{ cout<<feet<<"'"<<inches<<endl; } //Overloading - Distance operator -(Distance d1){ Distance d2; if(feet>d1.feet){ d2.feet=feet-d1.feet; d2.inches=inches-d1.inches; while(d2.inches<0){ d2.feet--; d2.inches=abs(d2.inches); } } else{ cout << "Subtraction not possible"<<endl; } return d2; } }; //Main Function int main(){ int x,y; Distance distance[3]; //Setting values for(int i=0;i<2;i++){
  • 4.
    cout << "EnterFeet of Distance " << i+1<<":"; cin >> x; cout << "Enter Inches of Distance " << i+1<<":"; cin >> y; distance[i].set_distance(x,y); cout << endl; } system("cls"); //Displaying the distances distance[0].display(); distance[1].display(); distance[2]=distance[0]-distance[1]; distance[2].display(); } Task#02: Modify the time class from Exercise 3 in Chapter 6 so that instead of a function add_time() it uses the overloaded + operator to add two times. Write a program to test this class. Code: #include<iostream> #include<stdlib.h> using namespace std; //Time Class class Time{ private: int hours; int minutes;
  • 5.
    int seconds; public: //Constuctor Time(){ hours=0; minutes=0; seconds=0; } //Setter Function voidset_time(int h,int m,int s){ hours=h; minutes=m; seconds=s; } //Sum Function Time operator +(Time t2){ Time t3; t3.hours=hours+t2.hours; t3.minutes=minutes+t2.minutes; t3.seconds=seconds+t2.seconds; if(t3.seconds>59){ t3.minutes++; t3.seconds=t3.seconds-60; } if(t3.minutes>59){ t3.minutes=t3.minutes-60; t3.hours+=1; }
  • 6.
    if(t3.hours>23){ t3.hours=t3.hours-23; } return t3; } //Display Function voiddisplay() const{ if(hours>=12){ hours-=12; cout <<hours<<":"<<minutes<<":"<<seconds<<" PM"<<endl; hours+=12; } else if(hours<12){ cout <<hours<<":"<<minutes<<":"<<seconds<<" AM"<<endl; } } }; //Main Function int main(){ Time t1,t2,t3; t1.set_time(22,28,30); t2.set_time(11,25,27); cout<<"Time T1: "; t1.display(); cout<<"Time T2: "; t2.display(); t3=t1+t2;
  • 7.
    cout<<"Time T3: "; t3.display(); } Task#03:Create a class Int based on Exercise 1 in Chapter 6. Overload four integer arithmetic operators (+, -, *, and /) so that they operate on objects of type Int.Write a program to test this class. Code: #include<iostream> #include<stdlib.h> using namespace std; //Int Class class Int{ private: int x; public: //Constructor Int(int y=0){ x=y; } //Setter Method void set_data(int y){ x=y; } //Display Method void display() const{ cout << x << endl; }
  • 8.
    //Overloading + Int operator+(Int y){ Int temp; temp.x=x+y.x; return temp; } //Overloading - Int operator -(Int y){ Int temp; temp.x=x-y.x; return temp; } //Overloading / Int operator /(Int y){ Int temp; temp.x=x/y.x; return temp; } //Overloading * Int operator *(Int y){ Int temp; temp.x=x*y.x; return temp; } };
  • 9.
    //Main Function int main(){ Intsum,sub,mul,div; Int A,B; int x; //Initializing objects cout<<"Enter value for A:"; cin>>x; A.set_data(x); cout<<"Enter value for B:"; cin>>x; B.set_data(x); system("cls"); cout << "A= "; A.display(); cout << "B= "; B.display(); //Using operators sum=A+B; sub=A-B; mul=A*B; div=A/B; //Displaying results cout << "Sum= ";
  • 10.
    sum.display(); cout << "Subraction="; sub.display(); cout << "Multiplication= "; mul.display(); cout << "Division= "; div.display(); } Task#04: Augment the time class referred to in Exercise 2 to include overloaded increment (++) and decrement (--) operators that operate in both prefix and postfix notation and return values. Add statements to main() to test these operators. Code: #include<iostream> #include<stdlib.h> using namespace std; //Time Class class Time{ private: int hours; int minutes; int seconds; public: //Constructor Time(){ hours=0; minutes=0;
  • 11.
    seconds=0; } //Setter Function void set_time(inth,int m,int s){ hours=h; minutes=m; seconds=s; } //Error Correcting Method void fix_time(Time t3){ while(t3.seconds>59){ t3.minutes++; t3.seconds=t3.seconds-60; } while(t3.minutes>59){ t3.minutes=t3.minutes-60; t3.hours+=1; } while(t3.hours>23){ t3.hours=t3.hours-24; } } //Overloading Time++ Time operator ++(int){ Time t3;
  • 12.
    t3.hours=hours++; t3.minutes=minutes++; t3.seconds=seconds++; fix_time(t3); return t3; } //Overloading ++Time Timeoperator ++(){ Time t3; t3.hours=++hours; t3.minutes=++minutes; t3.seconds=++seconds; fix_time(t3); return t3; } //Display Function void display(){ if(hours>=12){ hours-=12; cout <<hours<<":"<<minutes<<":"<<seconds<<" PM"<<endl; hours+=12; } else if(hours<12){ cout <<hours<<":"<<minutes<<":"<<seconds<<" AM"<<endl; } } };
  • 13.
    //Main Function int main(){ Timet1,t2,t3,t4; t1.set_time(5,28,30); t2.set_time(8,25,27); cout<<"Time T1: "; t1.display(); t2=t1++; //incremented cout<<"Time T2: "; t2.display(); t2=t1++; //incremented cout<<"Time T2: "; t2.display(); t2=++t1; //incremented cout<<"Time T2: "; t2.display(); } Task#05: Add to the time class of Exercise 4 the ability to subtract two time values using the overloaded (-) operator, and to multiply a time value by a number of type float, using the overloaded (*) operator. Code: #include<iostream> #include<stdlib.h> using namespace std; //Time Class class Time{
  • 14.
    private: int hours; int minutes; intseconds; public: //Constructor Time(){ hours=0; minutes=0; seconds=0; } //Setter Function void set_time(int h,int m,int s){ hours=h; minutes=m; seconds=s; } //Error Correcting Method void fix_time(Time t3){ while(t3.seconds>59){ t3.minutes++; t3.seconds=t3.seconds-60; }
  • 15.
    while(t3.minutes>59){ t3.minutes=t3.minutes-60; t3.hours+=1; } while(t3.hours>23){ t3.hours=t3.hours-24; } } //Overloading - Time operator-(Time t2){ Time t3; t3.hours=hours-t2.hours; t3.minutes=minutes-t2.minutes; t3.seconds=seconds-t2.seconds; fix_time(t3); return t3; } //Overloading * Time operator *(float num){ Time t3; t3.hours=hours*num; t3.minutes=minutes*num; t3.seconds=seconds*num;
  • 16.
    fix_time(t3); return t3; } //Display Function voiddisplay(){ if(hours>=12){ hours-=12; cout <<hours<<":"<<minutes<<":"<<seconds<<" PM"<<endl; hours+=12; } else if(hours<12){ cout <<hours<<":"<<minutes<<":"<<seconds<<" AM"<<endl; } } }; //Main Function int main(){ Time t1,t2,t3,t4; t1.set_time(22,44,37); t2.set_time(13,34,12); cout<<"Time T1: "; t1.display(); cout<<"Time T2: "; t2.display(); cout<<endl;
  • 17.
    t3=t1-t2; cout<<"Time T1-T2: "; t3.display(); t4=t1*89; cout<<"TimeT1*3: "; t4.display(); } Task#06: Design a class called NumDays . The class’s purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing and retrieving the hours and days. The class should also have the following overloaded operators: • + Addition operator. • - Subtraction operator. • ++ Prefix and postfix increment operators. • -- Prefix and postfix decrement operators. Code: #include<iostream> using namespace std; //NumDays Class class NumDays{ private: int hours; float days;
  • 18.
    public: //Constructor NumDays(int x=0){ hours=x; days=(float)hours/8; } //Setter Function voidset_hours(int x){ hours=x; days=(float)hours/8; } //Retrieving Hours int get_hours() const{ return hours; } //Retrieving Days float get_days() const{ return days; } //Overloading + NumDays operator +(NumDays x){ NumDays temp; temp.hours=hours+x.hours;
  • 19.
    temp.days=(float)temp.hours/8; return temp; } //Overloading - NumDaysoperator -(NumDays x){ NumDays temp; temp.hours=hours-x.hours; temp.days=(float)temp.hours/8; return temp; } //Overloading ++Obj NumDays operator ++(){ ++hours; days=(float)hours/8; } //Overloading Obj++ NumDays operator ++(int){ hours++; days=(float)hours/8; } //Overloading --Obj NumDays operator --(){ --hours;
  • 20.
    days=(float)hours/8; } //Overloading Obj-- NumDays operator--(int){ hours--; days=(float)hours/8; } //Display Function void display() const{ cout<<"Hours="<<hours<<endl; cout<<"Days="<<days<<endl<<endl; } }; //Main Function int main(){ NumDays worker1(20),worker2(12),worker3; //displaying original values cout<<"Original Values"<<endl; worker1.display(); worker2.display();
  • 21.
    //pre and postincrements cout<<"Incremented Values"<<endl; worker1++; ++worker2; worker1.display(); worker2.display(); //pre and post decrements cout<<"Decremented Values"<<endl; worker1--; --worker2; worker1.display(); worker2.display(); //using + and - operators cout<<"+ and - Operators"<<endl; worker3=worker1+worker2; worker3.display(); worker3=worker1-worker2; worker3.display(); }