Output 24p 3024
Output 24p 3024
Course: OOP
Submitted by Name: Atta Ul Mustafa
Teacher: M Usman Malik
T.A: M Raqeeb
#include<iostream>
using namespace std;
//here i have created main class containing distance in feet and inches
class RoomDistance{
private:
int feet;
float inches;
//here im using static varaible for counting total obj
static int totalobj;
3|Page
while(inches>=12){
feet=feet+1;
inches=inches-12;
}
// im using same logic here
while(inches<0){
feet=feet-1;
inches=inches+12;
}
}
public:
4|Page
//this is parametrized constructor it takes input
RoomDistance(int inputfeet, float inputinches){
feet=inputfeet;
inches=inputinches;
normalizing_inches();
totalobj++;
}
totalobj++;
//simple destructor
5|Page
~RoomDistance(){
totalobj--;
normalizing_inches();
}
6|Page
//here i have created overloading + operatorfun itadds two distances
RoomDistance operator+(const RoomDistance &second)const{
RoomDistance temp;
temp.feet=feet+second.feet;
temp.inches=inches+second.inches;
temp.normalizing_inches();
return temp;
normalizing_inches();
}
if(feet<other.feet){
return 1;
return 1;
}
else{
return 0;
}
}
void operator++(){
8|Page
inches=inches+1;
normalizing_inches();
void operator--(){
inches=inches-1;
normalizing_inches(); }
return output;
}
9|Page
//overloading >> operator similar logic
friend istream& operator>>(istream &input, RoomDistance &d){
cout<<"Please enter feet: ";
input>>d.feet;
cout<<"Please enter inches: ";
input>>d.inches;
d.normalizing_inches();
return input; }
//initalization done
int RoomDistance::totalobj=0;
10 | P a g e
int main(){
// now testing phase
// here creating two rooms
RoomDistance room1;
RoomDistance room2(10,10);
11 | P a g e
RoomDistance total=room1+room2;
cout<<endl;
cout<<"Total length of room1 + room2 = ";
total.showdistance();
if(room1<room2){
cout<<endl;
cout<<"room1 is smaller "<<endl;
}
else{
cout<<endl;
12 | P a g e
cout<<"room1 is not smaller"<<endl;
}
// testing opertaor overloading ++
++room1;
cout<<endl;
cout<<"after increasing room1 by 1 inch now distance is ";
room1.showdistance();
13 | P a g e
RoomDistance room3;
cout<<endl;
cout<<"Enter demo data for room 3"<<endl;
cin>>room3;
cout<<endl;
cout<<" entered room3 details = "<<room3<<endl;
cout<<endl;
// finally prinitng all objects which are created
RoomDistance::showtotalobj();
return 0;
}
14 | P a g e
OUTPUT
15 | P a g e