Question

Question 3 [30 Marks In architectural drawings, the distances are measured in feet and inches according to the English system

code in c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Summary :

As part of the solution following are provided (1) Implementation Notes (ii) Code and Sample Output.

Notes :

Distance class is defined with 2 member variables of type int to hold the feet and inches and the third member which is special , is a static int member used for keeping track of how many instances are created of Class Distance.

The static member requires to be dealt specially , hence its declared again the cpp file and initialized to value 0 so that everytime a new member is created, the counstructor methods keep incrementing this member variable .

Further while additions are being performed as part of + or += operator overloading , inches are converted to feet if the count is > 12 .

And for the ++ operator ( ++Distance ) , the increment is only done for the feet , left code for incrementing the inches as well commented.

########## Code ################

>>>>> Distance.h <<<<<

#ifndef DISTANCE_H_
#define DISTANCE_H_

#include <iostream>

class Distance
{
    static int cnt;
private:
    int feet;
    int inches;


protected:
    void cntObject()
    {
        cnt++;
    }
public:
    //static int cnt;
    Distance();
    Distance(int feet, int inches);
    void getDistance();
    void setFeet(int ft);
    void setInches(int in);
    void showDistance() const;
    int getFeet() const;
    int getInches() const;
    static int getcnt() { return cnt; };
    void operator+=(const Distance& other);
    Distance& operator++();
    bool operator < (const Distance& other);
    friend Distance operator+(const Distance& d1, const Distance& d2);
    friend std::ostream& operator<< ( std::ostream& out, const Distance& d1);

};


#endif /* DISTANCE_H_ */

>>>>>>>>>>> Distance.cpp <<<<<<<<<

#include <iostream>
#include "Distance.h"

using namespace std;

int Distance::cnt = 0;

// Default constructor
Distance::Distance()
{
    cntObject();
    this->feet = 0;
    this->inches = 0;
}

// overloaded constructor which accepts distance in feet and inches
Distance::Distance(int ft, int in)
{
    cntObject();
    this->feet = ft;
    this->inches = in;

}

// Returns the distance in feet
int Distance::getFeet() const
{
    return this->feet;
}

// Returns the inches portion of distance
int Distance::getInches() const
{
    return this->inches;
}

// sets the feet values of the Distance object
void Distance::setFeet(int ft)
{
    this->feet = ft;
}

// sets the inches portion of the distance object
void Distance::setInches(int in)
{
    // incase the value is > 12 , it converts to feet and addits to feet membe
    this->inches = (in % 12);
    this->feet = this->feet + (in / 12);
}

// Reads the value of Distance in feet and inches
void Distance::getDistance()
{
std::cout << " Enter Distance Feet followed by Inches (eg 5 9 ) : ";
std::cin >> this->feet;
std::cin >> this->inches;
std::cout << " Read " << this->feet << " " << this->inches << "\n";
}

// Prints the Distance in ft'-inches" format to standard output
void Distance::showDistance() const
{
cout << " " << feet << "'-" << inches <<'"' << "\n";
}


// Overloads the + operator to add two distance objects and retuns the result object
Distance operator+(const Distance& d1, const Distance& d2)
{

    int in = (d1.getInches() + d2.getInches());
    int ft = d1.getFeet() + d2.getFeet() + in / 12;

    in = in % 12;
    return Distance(ft, in);

}


// Overload the += operator and adds the given distance object to the lhs object
void Distance::operator+=(const Distance& rhs)
{
    int in = (this->inches + rhs.getInches());
    this->feet = this->feet + rhs.getFeet() + in / 12;
    this->inches = in % 12;

}

// Increments the rhs object by 1 feet
Distance& Distance::operator++()
{

    //int in = this->inches + 1;

    //this->inches = in % 12 ;
    this->feet = this->feet + 1;

    return *this;
}

// Implements the < operator and returns true if the lhs is < rhs Distance
bool Distance::operator < (const Distance& other)
{
    if (this->feet == other.getFeet())
    {
        return (this->inches < other.getInches());
    }
    else
        return (this->feet < other.getFeet());
}


// Overloads the << operator to print the given Distance object in ft'-in" format
ostream& operator<< (ostream& out, const Distance& d1)
{
    out << " " << d1.getFeet() << "'-" << d1.getInches() << '"' << "\n";
    return out;

}


int main()
{
    Distance d1;

    d1.setFeet(1);
    d1.setInches(2);

    cout << " d1 : " << d1 << "\n";

    Distance d11;

    d11.getDistance();


    d11 += d1;
    cout << " and d11 += d1 = " << d11 << "\n";

    cout << "Total Objects created so far : " << Distance::getcnt() << "\n";

    Distance d2(5,11);

    Distance d3 = d1 + d2;

    cout << "d1 : " << d1 << " d2 : " << d2 << " and d3 = (d1 + d2 ) " << d3 << "\n";
  
    cout << " d2 : " << d2 ;
    ++d2;

     cout << " and ++d2 = " << d2 << "\n";

     cout << " d1 < d2 : " << (d1 < d2) << "\n";
     cout << " d3 < d2 : " << (d3 < d2) << "\n";

     cout << "Total Objects : " << Distance::getcnt();
}

############ Sample Output ###############

ca Microsoft Visual Studio Debug Console di : 1-2 Enter Distance Feet followed by Inches (eg 5 9 ) : 5 11 Read 5 11 and di1

Add a comment
Know the answer?
Add Answer to:
code in c++ Question 3 [30 Marks In architectural drawings, the distances are measured in feet...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT