SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 313 Weeks Ago, 6 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 09 Jan 2018 My Price 10.00

inheritance to two new classes,inheri class Car

need help modifying program 5.1 included into problem 5.2

  • /*
    James Adams
    Winter 2017
    Lab 5
    Problem 5.1
    Description of problem:
    modify problem D1
    -Use inheritance to create two new classes, both of which will inherit from the class Car
    */


    /* *************************************************Beginning of Global Area*************************************************** */


    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cstring>
    #include <cstdlib>
    #include <fstream>
    using namespace std;

    enum Kind {business,maintenance,other,box,tank,flat,otherFreight,chair,sleeper,otherPassenger};
    const int num_kind = 10;

    const string KIND_ARRAY[num_kind] = { "business", "maintenance", "other", "box",
            "tank", "flat", "otherFreight", "chair", "sleeper", "otherPassenger" };


    /* *************** START of Class Car ****************************** */
    class Car
    {
        protected:
            static const int FIELD_WIDTH = 22;
            string reportingMark;
            int carNumber;
            Kind kind;
            bool loaded;
            string destination;

        public:
            /* ******* default constructor ***** */
            Car()
            {
                reportingMark = "";
                carNumber = 0;
                kind = other;
                loaded = 0;
                destination = "NONE";
            }


            /* *** copy constructor ************ */
            Car(Car &type1)
            {
                reportingMark = type1.reportingMark;
                carNumber = type1.carNumber;
                kind = type1.kind;
                loaded = type1.loaded;
                destination = type1.destination;
            }


            /* *** input constructor *********** */
            Car(string reporting_mark, int car_number, string kind_of_car, bool loaded_or_not, string place_of_destination)
            {
                setUp(reporting_mark, car_number, kind_of_car, loaded_or_not,place_of_destination);
            }


        /* *** Destructor *********** */
        ~Car() {}


        /* ******************** setKind ********************
        sets the correct Kind into the Car class
        */
        void setKind(const string& kind)
        {
            if (kind == "business")
                {
                    this->kind = business;
                }
            else if (kind == "maintenance")
                {
                    this->kind = maintenance;
                }
            else
                {
                    this->kind = other;
                }
        }


        /* ************** Output *****************
        -Prints Data to the console by calling
         the object
        */
        void output()
        {
        std::cout << std::setw(FIELD_WIDTH) << std::left << "Reporting Mark: " << std::setw(FIELD_WIDTH) << reportingMark << std::endl;
        std::cout << std::setw(FIELD_WIDTH) << "Car Number: "<< std::setw(FIELD_WIDTH) << carNumber<< std::endl;
        std::cout << std::setw(FIELD_WIDTH) << "Kind: "<< std::setw(FIELD_WIDTH) << KIND_ARRAY[kind]<< std::endl;
        std::cout << std::setw(FIELD_WIDTH) << "Loaded: "<< std::setw(FIELD_WIDTH);
            if(loaded)
            {
                std::cout << "true" << std::endl;
            }
            else
            {
                std::cout << "false" << std::endl;
            }
        std::cout << std::setw(FIELD_WIDTH) << "Destination: "<< std::setw(FIELD_WIDTH) << destination<< std::endl;
        std::cout << std::endl;
        }


        /* ******************** setUp ********************
        stores the data that is entered into the object
        */
        void setUp(string reporting_mark, int car_number, string kind_of_car, bool loaded_or_not, string place_of_destination)
        {
            reportingMark = reporting_mark;
            carNumber = car_number;
            setKind(kind_of_car);
            loaded = loaded_or_not;
            destination = place_of_destination;
        }


        /* ******************** operator= ********************
        returns the left hand through reference
        */
        void operator =(const Car & carB)
        {
            reportingMark = carB.reportingMark;
            carNumber = carB.carNumber;
            kind = carB.kind;
            loaded = carB.loaded;
            destination = carB.destination;
        }


        friend bool operator ==(const Car &left, const Car &right)
        {
            if (left.reportingMark == right.reportingMark
                    && left.carNumber == right.carNumber)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    };
    /* *************** END of Class Car ********************************* */


    /* *************** START of Class StringOfCars ********************** */
    class StringOfCars
    {
        private:
            Car *ptr;
            static const int ARRAY_MAX_SIZE = 10;
            int arrCars;
        public:
            /* ******* default constructor ***** */
            StringOfCars()
            {
                ptr = new Car[ARRAY_MAX_SIZE];
                arrCars = 0;
            }


            /* *** copy constructor ************ */
            StringOfCars(const StringOfCars &)
            {
                ptr = new Car[ARRAY_MAX_SIZE];
            }

            /* ********* Destructor ***********/
            ~StringOfCars()
            {
                delete[] ptr;
            };


        /* ******************** output ********************
        prints the cars inside the string of cars along with its heading.
        */
        void output()
        {
            if (arrCars == 0)
                {
                    cout << "NO Cars" << endl;
                }
            else
                {
                    for (int i = 0; i < arrCars; i++)
                        {
                            ptr[i].output();
                        }
                }
        }


        /* ******************** push ********************
        adds a car to the string of cars
        */
        void push(Car &carsPushed)
        {
            if (arrCars < ARRAY_MAX_SIZE)
                {
                    ptr[arrCars] = carsPushed;
                    arrCars++;
                }
            else
                {
                    cout << "The array is full." << endl;
                    exit(-1);
                }
        }


        /* ******************** pop ********************
        removes a car from the string of cars, Last in First Out.
        */
        void pop(Car &carPopped)
        {
            Car temp;
            arrCars--;
            carPopped = ptr[arrCars];
        }
    };
    /* *************** END of Class StringOfCars ********************************* */


    /* *************** START of Derived Class FreightCar **************** */
    class FreightCar: public Car
    {
        public:

            FreightCar(string reporting_mark, int car_number, string kind_of_car,
                bool loaded_or_not, string place_of_destination):Car(reporting_mark,car_number,kind_of_car,loaded_or_not,place_of_destination)
            {
                setKind(kind_of_car);
            }


        /* ************** FreightCar::setKind *******
        -Takes in data and assigns to variable kind
        */
        void setKind(string constant_string)
        {
            if (constant_string == "box")
                {
                    this->kind = box;
                }
            else if (constant_string == "tank")
                {
                    this->kind = tank;
                }
            else if (constant_string == "flat")
                {
                    this->kind = flat;
                }
            else
                {
                    this->kind = otherFreight;
                }
        }
    };
    /* *************** END of Derived Class FreightCar ******************** */


    /* *************** START of Derived Class PassengerCar **************** */
    class PassengerCar: public Car
    {
    public:

        PassengerCar(string reporting_mark, int car_number, string kind_of_car, bool loaded_or_not, string place_of_destination)
            :Car(reporting_mark,car_number,kind_of_car,loaded_or_not,place_of_destination)
        {
            setKind(kind_of_car);
        }


        /* ************** PassengerCar::setKind *******
        -Takes in data and assigns to variable kind
        */
        void setKind(string constant_string)
        {
            if (constant_string == "chair")
                {
                    this->kind = chair;
                }
            else if (constant_string == "sleeper")
                {
                    this->kind = sleeper;
                }
            else
                {
                    this->kind = otherPassenger;
                }
        }
    };
    /* *************** END of Derived Class PassengerCar **************** */


    /* ************************* START Function Prototypes *********************** */
    void input(StringOfCars &string1);
    void buildCar(string &reporting_mark, int &car_number, string &kind_of_car, bool &loaded_or_not, string &place_of_destination);
    void buildFreightCar(string &reporting_mark, int &car_number, string &kind_of_car, bool &loaded_or_not, string &place_of_destination);
    void buildPassengerCar(string &reporting_mark, int &car_number, string &kind_of_car, bool &loaded_or_not, string &place_of_destination);
    /* ************************* END Function Prototypes ************************ */


    /* ******************** input ********************
    -prompts user to read data from a text file into the structure
    */
    void input(StringOfCars &string1)
    {
        string reporting_mark;
        int car_number;
        string kind_of_car;
        bool loaded_or_not;
        string place_of_destination;

        string type;

        ifstream inputFile("cardata51.txt");

        if (inputFile.fail())
            {
                std::cerr << "Error opening the file." << endl;
                exit(-1);
            }

        int count = 1;
        string order;
        while (inputFile >> type >> order >>reporting_mark >> car_number >> kind_of_car >> boolalpha>>loaded_or_not)
            {
            getline(inputFile, place_of_destination);
            cout << "CAR " << count++ << endl;
            if (type == "Car")
                {
                    buildCar(reporting_mark, car_number, kind_of_car, loaded_or_not,
                        place_of_destination);
                }
            if (type == "FreightCar")
                {
                    buildFreightCar(reporting_mark, car_number, kind_of_car,
                        loaded_or_not, place_of_destination);
                }
            if (type == "PassengerCar")
                {
                    buildPassengerCar(reporting_mark, car_number, kind_of_car,
                        loaded_or_not, place_of_destination);
                }

            }
        inputFile.close();
    }

    /* ******************** buildCar ********************
    -sets the values in the car object and calls the output function
    */
    void buildCar(string &reporting_mark, int &car_number, string &kind_of_car,
            bool &loaded_or_not, string &place_of_destination)
    {
        Car carA(reporting_mark,car_number,kind_of_car,loaded_or_not,place_of_destination);
        carA.output();
        cout << endl;
    }


    /* ******************** buildFreightCar ********************
    -sets the values in the FreightCar object and calls the output function
    */
    void buildFreightCar(string &reporting_mark, int &car_number, string &kind_of_car,
            bool &loaded_or_not, string &place_of_destination)
    {
        FreightCar fCar(reporting_mark,car_number,kind_of_car,loaded_or_not,place_of_destination);
        fCar.output();
        cout << endl;
    }

    /* ******************** buildPassengerCar ********************
     sets the values in the PassengerCar object and calls the output function
     */
    void buildPassengerCar(string &reporting_mark, int &car_number, string &kind_of_car,
            bool &loaded_or_not, string &place_of_destination)
    {
        PassengerCar pCar(reporting_mark,car_number,kind_of_car,loaded_or_not,place_of_destination);
        pCar.output();
        cout << endl;
    }
    /* *****************************************************End of Global Area******************************************************** */


    /* **************** Main ******************* */
    int main()
    {
        StringOfCars cars;
        input(cars);
        return 0;
    }

Attachments:

Answers

(5)
Status NEW Posted 09 Jan 2018 12:01 PM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)