Question

#include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
/**
   The FeetInches class holds distances measured in
   feet and inches.
*/

class FeetInches
{
  private:
   int feet;      // The number of feet
   int inches;    // The number of inches
   
   /**
   The simplify method adjusts the values
   in feet and inches to conform to a 
   standard measurement.
   */
   void simplify()
   {
      if (inches > 11)
      {
         feet = feet + (inches / 12);
         inches = inches % 12;
      }
   }

   /**
      This constructor assigns 0 to the feet
      and inches fields.
   */

 public:
   FeetInches()
   { 
      feet = 0;
      inches = 0;
   }

   /**
      This constructor accepts two arguments which
      are assigned to the feet and inches fields.
      The simplify method is then called.
      @param f The value to assign to feet.
      @param i The value to assign to inches.
   */
     
   FeetInches(int f, int i)
   { 
      feet = f;
      inches = i;
      simplify();
   }


   /**
      The following is a copy constructor. It accepts a
      reference to another FeetInches object. The feet
      and inches fields are set to the same values as
      those in the argument object.
      @param object2 The object to copy.
   */

   FeetInches (const FeetInches& object2)
   {
      feet = object2.feet;
      inches = object2.inches;
   }

   /**
      The setFeet method assigns a value to 
      the feet field.
      @param f The value to assign to feet.
   */

   void setFeet(int f)
   {
      feet = f; 
   }

   /**
      The setInches method assigns a value to 
      the inches field.
      @param i The value to assign to inches.
   */

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

   /**
      getFeet method
      @return The value in the feet field.
   */

   int getFeet()
   {
      return feet;
   }

   /**
      getInches method
      @return The value in the inches field.
   */

   int getInches()
   {
      return inches;
   }

   /**
      print method
      prints the distance as feet/inches
   */  
   void print()
   {
    cout << feet << " feet  " << inches << " inches";
   }
  
   /**
      The add method returns a FeetInches object
      that holds the sum of this object and another
      FeetInches object.
      @param object2 The other FeetInches object.
      @return A reference to a FeetInches object.
   */

   FeetInches add(const FeetInches& object2)
   {
      int totalFeet,    // To hold the sum of feet
          totalInches;  // To hold the sum of inches

      totalFeet = feet + object2.feet;
      totalInches = inches + object2.inches;
      return FeetInches(totalFeet, totalInches);
   }
   
   FeetInches scale(int s)
   {
      return FeetInches(s*feet, s*inches);
   }

   /**
      The equals method compares this object to the
      argument object. If both have the same values, 
      the method returns true.
      @return true if the objects are equal, false
      otherwise.
   */

   bool equals(FeetInches object2)
   {
      return feet == object2.feet && inches == object2.inches;
   }

};

int main()
{
        FeetInches f(0, 30), g;
        int ft, in;
        cout << "Length 1 = "; f.print(); cout << endl;
        cout << "\nEnter length 2 feet: "; cin >> ft;
        cout << "Enter length 2 inches: "; cin >> in;
        g.setFeet(ft);
        g.setInches(in);
        
        cout << "\nLength 2 = "; g.print(); cout << endl;
        if (f.equals(g))
                cout << "\nThe lengths are the same\n\n";
        else
                cout << "\nThe lengths are not the same\n\n";
        FeetInches h = f.add(g);
        cout << "Sum of length 1 and 2 = "; h.print(); cout << endl;
        cout << "2 * length 1 = "; f.scale(2).print(); cout << endl;
        cout << "3 * length 2 = "; g.scale(3).print(); cout << endl;
        getchar();
        return 0;
}

Programming Project 6

Change the implementation of the FeetInches class to use only one private data member named all_inches to store the length. A length that would have been stores in the current class as feet = 2, inches = 6 will now be stored as all_inches = 30.

All class member methods must work the same as before. The main program should give identical results using either the old or new versions of the class.

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

Please find the updated code below::::

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
/**
The FeetInches class holds distances measured in
feet and inches.
*/

class FeetInches
{
private:
   int all_inches; // The number of feet

/* no need ot simplify function now
   *
The simplify method adjusts the values
in feet and inches to conform to a
standard measurement.

   void simplify()
   {
       if (inches > 11)
       {
           feet = feet + (inches / 12);
           inches = inches % 12;
       }
   }
*/

   /**
This constructor assigns 0 to the feet
and inches fields.
   */

public:
   FeetInches()
   {
       all_inches = 0;
   }

   /**
This constructor accepts two arguments which
are assigned to the feet and inches fields.
The simplify method is then called.
@param f The value to assign to feet.
@param i The value to assign to inches.
   */

   FeetInches(int f, int i)
   {
       all_inches = f*12 + i;
   }


   FeetInches( int i)
   {
       all_inches = + i;
   }

   /**
The following is a copy constructor. It accepts a
reference to another FeetInches object. The feet
and inches fields are set to the same values as
those in the argument object.
@param object2 The object to copy.
   */

   FeetInches (const FeetInches& object2)
   {
       all_inches = object2.all_inches;
   }

/* The setFeet method assigns a value to
the feet field.
@param f The value to assign to feet.*/


   void setFeet(int f)
   {
       all_inches += f*12;
   }

   /**
The setInches method assigns a value to
the inches field.
@param i The value to assign to inches.
   */

   void setInches(int i)
   {
       all_inches += i;
   }

/*
   *
getFeet method
@return The value in the feet field.


   int getFeet()
   {
       return feet;
   }
*/

   /**
getInches method
@return The value in the inches field.
   */

   int getInches()
   {
       return all_inches;
   }

   /**
print method
prints the distance as feet/inches
   */
   void print()
   {
       int feet = all_inches/12;
       int inches = all_inches%12;
       cout << feet << " feet " << inches << " inches";
   }

   /**
The add method returns a FeetInches object
that holds the sum of this object and another
FeetInches object.
@param object2 The other FeetInches object.
@return A reference to a FeetInches object.
   */

   FeetInches add(const FeetInches& object2)
   {
       int totalInches = all_inches + object2.all_inches;
       return FeetInches(totalInches);
   }

   FeetInches scale(int s)
   {
       return FeetInches(s*all_inches);
   }

   /**
The equals method compares this object to the
argument object. If both have the same values,
the method returns true.
@return true if the objects are equal, false
otherwise.
   */

   bool equals(FeetInches object2)
   {
       return all_inches == object2.all_inches;
   }

};

int main()
{
   FeetInches f(0, 30), g;
   int ft, in;
   cout << "Length 1 = "; f.print(); cout << endl;
   cout << "\nEnter length 2 feet: "; cin >> ft;
   cout << "Enter length 2 inches: "; cin >> in;
   g.setFeet(ft);
   g.setInches(in);

   cout << "\nLength 2 = "; g.print(); cout << endl;
   if (f.equals(g))
       cout << "\nThe lengths are the same\n\n";
   else
       cout << "\nThe lengths are not the same\n\n";
   FeetInches h = f.add(g);
   cout << "Sum of length 1 and 2 = "; h.print(); cout << endl;
   cout << "2 * length 1 = "; f.scale(2).print(); cout << endl;
   cout << "3 * length 2 = "; g.scale(3).print(); cout << endl;
   getchar();
   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...
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
  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class...

    Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class Product { private:    // private variables    int id_number;    char pDescription[40];    int mId;    double productPrice;    double productMarkUp;    int qty; public:    // constructor    Product() {        id_number = 0;        strcpy_s(pDescription, "");        mId = 0;        productPrice = 0.0;        productMarkUp = 0.0;        qty = 0;    }   ...

  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

  • Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person {...

    Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...

  • #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...

    #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

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