0% found this document useful (0 votes)
3 views15 pages

Output 24p 3024

The document outlines an assignment for an Object-Oriented Programming course, submitted by Atta Ul Mustafa, which includes a detailed implementation of a RoomDistance class in C++. The class manages distances in feet and inches, includes various constructors, operator overloads, and functions for input/output and normalization of distances. The main function demonstrates the usage of the RoomDistance class and its functionalities, including object creation and operator overloading for arithmetic and comparison operations.

Uploaded by

p243024
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)
3 views15 pages

Output 24p 3024

The document outlines an assignment for an Object-Oriented Programming course, submitted by Atta Ul Mustafa, which includes a detailed implementation of a RoomDistance class in C++. The class manages distances in feet and inches, includes various constructors, operator overloads, and functions for input/output and normalization of distances. The main function demonstrates the usage of the RoomDistance class and its functionalities, including object creation and operator overloading for arithmetic and comparison operations.

Uploaded by

p243024
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/ 15

Assignment #3

Course: OOP
Submitted by Name: Atta Ul Mustafa
Teacher: M Usman Malik
T.A: M Raqeeb

Roll no: 24P-3024


Section: B
Dated: 14-04-2025
1|Page
Q no 1:
Source code: (Output on page no 15 )
/*
strategy:
1: first of all i'll Define a class RoomDistance to store and manaage
distances in feet and inches
as required in the probem.
2: than ill also use a static varaible to track how many objdects are
created so far
i'll also declare it in class
3: Than i'll create constructors mainly default, parameteriezed also
copy as
it is required to create objects.
4: I'll try to create a function that automatically normalizes inches
into
feet when inches = 12 or negative according to standard.
5: ill also cretae other function which allows user input and output
namely getdistance() to get and showdistance() to display
6: overloded functions will also be created as required in question
overloads + and += operators adds distances
7: similarly < func to compare which distance is smaller.
8: i'll also create ++ and -- to increase or decrease inches by one
manualy
2|Page
9: << and >> operators will also be overloaded using overloadedto
simplify i/o
operations with cin and cout
10: also a fun to display total no of objects created
11: i'll try to keep the code clean
*/

#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;

// this is simple function which converts inches into standard form


void normalizing_inches(){

// it checks the condition if true than loop runs

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:

//this is simple default constructor


RoomDistance(){
feet=0;
inches=0;
totalobj++;

4|Page
//this is parametrized constructor it takes input
RoomDistance(int inputfeet, float inputinches){
feet=inputfeet;
inches=inputinches;

normalizing_inches();
totalobj++;
}

//copy constructor as required in problen simply copies

RoomDistance(const RoomDistance &Real){


feet=Real.feet;
inches=Real.inches;

totalobj++;

//simple destructor

5|Page
~RoomDistance(){

totalobj--;

//this function takes distance input from user


void getdistance() {
cout<<"Please enter the feets : ";
cin>>feet;
cout<<"Please enter the inches:";
cin>>inches;

normalizing_inches();
}

//this function displays the distance


void showdistance()const{
cout<<feet<<"'-"<<inches<<"\""<<endl;
}

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;

//here i have created overloading += operator fun to add and assign


simply
void operator+=(const RoomDistance &second){
feet=feet+second.feet;
inches=inches+second.inches;

normalizing_inches();
}

//here i have created overloading < operator fun to compare two


distances
// it returns true if greateer else returns false
7|Page
// using const as value cant be changed
bool operator<(const RoomDistance &other) const{

if(feet<other.feet){

return 1;

else if( feet==other.feet && inches<other.inches ){

return 1;
}

else{
return 0;

}
}

void operator++(){

8|Page
inches=inches+1;
normalizing_inches();

void operator--(){
inches=inches-1;

normalizing_inches(); }

// this is very important overloading << operator to print object


// It allows us to print a RoomDistance object using cout in the format
feet-inches as
//required in the problem
// used friend so it can access
friend ostream& operator<<(ostream &output, const RoomDistance
&d){
output<<d.feet<<"'-"<<d.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; }

//here i have created static function to show total objects


// it doesnt depend on the obj also can be called without obj
static void showtotalobj(){
cout<<"Total objects created: "<<totalobj<<endl;
}
};

//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);

// now getting distance of room 1 via methods


cout<<"Please enter distance for room1 "<<endl;
room1.getdistance();
cout<<endl;
// now showing via methods
cout<<"The distance of room1 ";
room1.showdistance();

// similar done here


cout<<endl;
cout<<"Distance of room2: ";
room2.showdistance();

// testing opertaor overloading +

11 | P a g e
RoomDistance total=room1+room2;
cout<<endl;
cout<<"Total length of room1 + room2 = ";
total.showdistance();

// testing opertaor overloading +=


room1+=room2;
cout<<endl;
cout<<"After room1 += room2: ";
room1.showdistance();

// testing opertaor overloading <


cout<<endl;
cout<<"Checking whether room1<room2 or not "<<endl;

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();

// testing opertaor overloading --


--room2;
cout<<endl;
cout<<"after decreasing room2 by 1 inch now distance is ";
room2.showdistance();

// testing opertaor overloading <<


cout<<endl;
cout<<"Using << operator to print details of room no 1
"<<room1<<endl;

// testing opertaor overloading >>

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy