Thursday, January 4, 2018

Programming Challenge 14.12 - LandTract Class

Example Files: LandTractClass.7z

FeetInches.h


#ifndef FEET_INCHES_H_
#define FEET_INCHES_H_

#include <iostream>
using std::istream;
using std::ostream;

class FeetInches
{
    private:
        int feet;                        // To hold a number of feet
        int inches;                        // To hold a number of inches

        void simplify();                // Defined in FeetInches.cpp

    public:
        FeetInches(int f = 0, int i = 0)
        {
            feet = f;
            inches = i;
            simplify();
        }

        // Copy Constructor
        FeetInches(const FeetInches &obj)
        {
            feet = obj.feet;
            inches = obj.inches;
        }

        // Mutator functions
        void setFeet(int f)
        {
            feet = f;
        }

        void setInches(int i)
        {
            inches = i;
             simplify();
        }

        // Accessor functions
        int getFeet() const
        { return feet; }

        int getInches() const
        { return inches; }

        // Friends
        friend ostream &operator << (ostream &, const FeetInches &);
        friend istream &operator >> (istream &, FeetInches &);
};

#endif

FeetInches.cpp


#include "FeetInches.h"

#include <cstdlib>

#include <iostream>
using std::cout;

/* **********************************************************
            FeetInches::simplify()
    This function checks for values in the inches member
    greater than twelve or less than zero. If such a value is
    found, the numbers in feet and inches are adjusted to
    conform to a standard feet & inches expression. For
    example:

    3 feet 14 inches would be adjusted to 4 feet 2 inches and
    5 feet -2 inches would be adjusted to 4 feet 10 inches.

    The standard library function abs() is used to get the
    absolute value of the inches member. The abs() function
    requires to #include <cstdlib>.
   ********************************************************** */

void FeetInches::simplify()
{
    if (inches >= 12)
    {
        feet += (inches / 12);
        inches = inches % 12;
    }
    else if (inches < 0)
    {
        feet -= ((abs(inches) / 12) + 1);
        inches = 12 - (abs(inches) % 12);
    }
}

/* **********************************************************
            Overloaded << operator
    Gives cout the ability to directly display FeetInches
    objects.
   ********************************************************** */

ostream &operator << (ostream &strm, const FeetInches &obj)
{
    strm << obj.feet << " feet, " << obj.inches << " inches.";

    return strm;
}

/* **********************************************************
            Overloaded >> operator
    Gives cin the ability to directly store user input into
    FeetInches objects.
   ********************************************************** */

istream &operator >> (istream &strm, FeetInches &obj)
{
    // Prompt the user for the feet
    cout << "Feet: ";
    strm >> obj.feet;

    // Prompt the user for the inches
    cout << "Inches: ";
    strm >> obj.inches;

    // Normalize the values
    obj.simplify();

    return strm;
}

LandTract.h


#ifndef LAND_TRACT_H_
#define LAND_TRACT_H_

#include "FeetInches.h"

class LandTract
{
    private:
        int area;

        FeetInches width;
        FeetInches length;

    public:
        LandTract(FeetInches w, FeetInches l) : width(w), length(l)
        {
            area = 0;
            calcArea();
        }

        void calcArea();

        int getArea() const
        { return area; }

        // Overloaded operator
        bool operator == (const LandTract &);                    // Overloaded ==
};

#endif

LandTract.cpp


#include "LandTract.h"

/* **********************************************************
            LandTract::calcArea()
    Calculates the area of two tracts of land.
   ********************************************************** */

void LandTract::calcArea()
{
    int w = width.getFeet() * 12 + width.getInches();
    int len = length.getFeet() * 12 + length.getInches();
   
    area = (w * len);
}

/* **********************************************************
            Overloaded == operator
    Returns true if the current object is set to a value equal
    to that of right.
   ********************************************************** */

bool LandTract::operator == (const LandTract &right)
{
    bool status;

    if (area == right.area)
    {
        status = true;
    }
    else
    {
        status = false;
    }

    return status;
}

LandTractDm.cpp


#include "LandTract.h"

#include <iomanip>
using std::setprecision;

#include <iostream>
using std::fixed;
using std::cin;
using std::cout;

int main()
{
    const double TO_SQR_FT = 144.0;

    FeetInches w, l;

    cout << "Land Tract Area Calculator\n\n";
    cout << "This program lets you enter the dimensions for two tracts of land.\n"
          << "It will calculate the area for each, and output the result in square feet.\n"
          << "In addition it will tell you, whether the tracts are of same size.\n\n";

    cout << "Enter the width of tract #1:\n\n";
    cin >> w;
    cout << "\nEnter the length of tract #1:\n\n";
    cin >> l;

    LandTract lot(w, l);

    cout << "\nThe area of your lot is: ";
    cout << setprecision(2) << fixed;
    cout << (lot.getArea() / TO_SQR_FT) << " sq. ft.\n\n";

    cout << "Enter the width of tract #2:\n\n";
    cin >> w;
    cout << "\nEnter the length of tract #2:\n\n";
    cin >> l;

    LandTract garden(w, l);

    cout << "\nThe area of your garden is: ";
    cout << (garden.getArea() / TO_SQR_FT) << " sq. ft.\n\n";

    if (lot == garden)
    {
        cout << "Both tracts of land are of equal size.";
    }
    else
    {
        cout << "The tracts of land are of different size.";
    }

    cin.get();
    cin.ignore();
    return 0;
}

Example Output:





No comments:

Post a Comment