Question

CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle cla...

CIS247C Week 3 Project Overview

The objective of this week is to enhance last week's Vehicle class by making the following changes:
• Create a static variable called numVehicles that holds an int and initialize it to zero. This will allow us to count all the Vehicle objects created in the main class.
• Add the copy constructor
• Increment numVehicles in all of the constructors
• Decrement numVehicle in destructor
• Add overloaded versions of setYear and setMpg that accept strings. This way, we will have two "set" methods; one that accepts a string, and one that accepts its default data type.

In the Vehicle class complete the following steps:
1. Create a static numVehicles variable and initialize it to zero.
2. Create a copy constructor.
3. Update the destructor to decrement numVehicles by one.
4. Increment numVehicles by one in each of the constructors.
5. Create an overloaded setYear method and this time make the parameter a string.
6. Create an overloaded setMpg method and this time make the parameter a string.
**Remember that you will have to convert the string in the above two "set" methods to the data type of the attribute.**
7. Make the getNumVehicles a static method. This way, you can call it with the class name instead of an object name.

In the main program create code statements that perform the following operations Note that several of the steps below were accomplished in last week's assignment. New steps are in bold.
1. Using your code from Week 1, display a divider that contains the string "Start Program".
2. Update and call the DisplayApplicationInformation function.
3. Create a Vehicle object using the default constructor.
4. Prompt for and then set year and mpg using the new overloaded setters. Consider using your getInput method from Week 1 to obtain data from the user for this step as well as Step 3.
5. Using your code from Week 1, display a divider that contains the string "Vehicle 1 Information".
6. Display the Vehicle Information.
7. Create a second Vehicle object using the multi-arg constructor, setting each of the attributes with the following values: "Cadillac", 2017, 26.5
8. Create a third object by copying the second object.
9. Delete the third object by calling the destructor method.
10. Display the number of vehicles created using getNumVehicles. Remember to access getNumVehicles using the class name, not the Vehicle object.
11. Update and call the TerminateApplication function.



Source.cpp

//Programmer: Dr. Natalie Waksmanski

//CIS247C Week 2 Project

//Program Description:

#include "Vehicle.h"

#include

#include

using namespace std;

void DisplayApplicationInformation();

void DisplayDivider(string);

string GetInput(string);

void TerminateApplication();

int main()

{

       Vehicle car1;

       Vehicle car2("Cadillac", 2020, 19);

       DisplayApplicationInformation();

       DisplayDivider("Get Car Make ");

       car1.setMake(GetInput("Make "));

       DisplayDivider("Get Year");

       car1.setYear(atoi(GetInput("Year ").c_str()));

       DisplayDivider("Get Mileage");

       car1.setMileage(atof(GetInput("Mileage ").c_str()));

       DisplayDivider("Vehicle Information");

       car1.displayVehicle();

       DisplayDivider("Vehicle Information");

       car2.displayVehicle();

       TerminateApplication();

       system("pause");

       return 0;

      

}

void DisplayApplicationInformation()

{

       cout << "Welcome to CIS247C Project";

       cout << "This program uses the Vehicle class to prompt the user for vehicle information, gather information about the attributes, and display the results";

       return;

}

void DisplayDivider(string message)

{

       cout << "\n***************" << message << "********" << endl;

}

string GetInput(string message)

{

       string input;

       cout << "Enter the " << message;

       getline(cin, input);

       return input;

}

void TerminateApplication()

{

       cout << "End of CIS247C Project Application";

       return;

}

Vehicle.h

#pragma once

#include

using namespace std;

class Vehicle

{

private:

       string make;

       int year;

       double mpg;

public:

       Vehicle();

       ~Vehicle();

       Vehicle(string, int, double);

       void setYear(int);

       int getYear();

       void setMake(string);

       string getMake();

       void setMileage(double);

       double getMileage();

       void displayVehicle();

};

Vehicle.cpp

#include "Vehicle.h"

#include

#include

using namespace std;

Vehicle::Vehicle()

{

       make = "unknown";

       year = 0;

       mpg = 0;

}

Vehicle::Vehicle(string m, int y, double mg)

{

       make = m;

       year = y;

       mpg = mg;

}

Vehicle::~Vehicle()

{

}

int Vehicle::getYear()

{

       return year;

}

void Vehicle::setYear(int y)

{

       year = y;

}

void Vehicle::setMake(string m)

{

       make = m;

}

string Vehicle::getMake()

{

       return make;

}

void Vehicle::setMileage(double mg)

{

       mpg = mg;

}

double Vehicle::getMileage()

{

       return mpg;

}

void Vehicle::displayVehicle()

{

       cout << "Vehicle make: " << make << endl;

       cout << "Vehicle year: " << year << endl;

       cout << "Vehicle mpg: " << mpg << endl;

}

THIS IS ALL THE INFORMATION I HAVE, i DONT KNWO WHAT ELSE YOU WANT. THE PRIOR WEEK CODE IS INCLUDED AND THE ASSIGNGMENT IS IN BOLD ITALIC. THIS IS ALL I HAVE TO GO WITH

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

Vehicle.h

#pragma once

#include<bits/stdc++.h>

using namespace std;

class Vehicle

{

private:

       string make;

       int year;

       double mpg;

                

                 static int numVehicles;

public:

       Vehicle();

       ~Vehicle();

       Vehicle(string, int, double);

                

                 // Copy constructor

                 Vehicle(const Vehicle &v);

       void setYear(int);

                

                 void setYear(string);

       int getYear();

       void setMake(string);

       string getMake();

       void setMileage(double);

                

                 void setMileage(string);

       double getMileage();

       void displayVehicle();

                

                 static int getNumVehicles()

                 {

                                return numVehicles;

                 }

};

Vehicle.cpp

#include "Vehicle.h"

using namespace std;

Vehicle::Vehicle()

{

                 numVehicles++;

       make = "unknown";

       year = 0;

       mpg = 0;

}

Vehicle::Vehicle(string m, int y, double mg)

{

                 numVehicles++;

                

       make = m;

       year = y;

       mpg = mg;

}

int Vehicle::numVehicles = 0;

Vehicle::Vehicle(const Vehicle &v)

{

              numVehicles++;

             

              make = v.make;

    year = v.year;

    mpg = v.mpg;

             

}

Vehicle::~Vehicle()

{

              numVehicles--;

}

int Vehicle::getYear()

{

       return year;

}

void Vehicle::setYear(int y)

{

       year = y;

}

void Vehicle::setYear(string y)

{

       year = atoi(y.c_str());

}

void Vehicle::setMake(string m)

{

       make = m;

}

string Vehicle::getMake()

{

       return make;

}

void Vehicle::setMileage(double mg)

{

       mpg = mg;

}

void Vehicle::setMileage(string mg)

{

       mpg = atof(mg.c_str());

}

double Vehicle::getMileage()

{

       return mpg;

}

void Vehicle::displayVehicle()

{

       cout << "Vehicle make: " << make << endl;

       cout << "Vehicle year: " << year << endl;

       cout << "Vehicle mpg: " << mpg << endl;

}

Source.cpp

//Programmer: Dr. Natalie Waksmanski

//CIS247C Week 2 Project

//Program Description:

#include "Vehicle.h"

using namespace std;

void DisplayApplicationInformation();

void DisplayDivider(string);

string GetInput(string);

void TerminateApplication();

int main()

{

                 DisplayDivider("Start Program");

                

                 DisplayApplicationInformation();

                

                

                 Vehicle car1;

                

                 DisplayDivider("Get Year");

       car1.setYear(GetInput("Year "));

       DisplayDivider("Get Mileage");

       car1.setMileage(GetInput("Mileage "));

                

                

                 DisplayDivider("Vehicle Information");

       car1.displayVehicle();

                

       Vehicle car2("Cadillac", 2017, 26.5);

                

                 Vehicle car3(car2);

                

                 car3.~Vehicle();

                

                

                 DisplayDivider("Number of Vehicles: ");

                 cout<<Vehicle::getNumVehicles()<<endl;

                

       TerminateApplication();

       system("pause");

       return 0;

     

}

void DisplayApplicationInformation()

{

       cout << "Welcome to CIS247C Project";

       cout << "This program uses the Vehicle class to prompt the user for vehicle information, gather information about the attributes, and display the results";

       return;

}

void DisplayDivider(string message)

{

       cout << "\n***************" << message << "********" << endl;

}

string GetInput(string message)

{

       string input;

       cout << "Enter the " << message;

       getline(cin, input);

       return input;

}

void TerminateApplication()

{

       cout << "End of CIS247C Project Application";

       return;

}

Comments:

This runtime error is observed at the end:

*** Error in `./a.out': double free or corruption (fasttop): 0x000000000102cc50 ***

This is because this object is created using copy constructor and we are doing deep copy. This is error expected. Since we are freeing two times, one time calling the destructor and another time during exit of the program.

There are various ways to prevent this:

  1. Move constructor
  2. Shallow copy

Since this behavior is not mentioned in the steps asked, I have not done it. It is not necessary as per the given question.

Thanks.

Please comment if you need further help.

Screenshot of the output:

**Start Program Welcome to CIS247C ProjectThis program uses the Vehicle class to prompt the user for vehicle information, gat

Add a comment
Know the answer?
Add Answer to:
CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle cla...
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
  • //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 =...

  • In Source file: Insert comments describing set of statements as indicated. Check format of output. ====================================================================================================...

    In Source file: Insert comments describing set of statements as indicated. Check format of output. ==================================================================================================== #include <iostream> #include "Vehicle.h" #include "Warranty.h" #include "Hybrid.h" using namespace std; //insert comment describing these set of statements here void DisplayDivider(string); string GetInput(string); void DisplayApplicationInformation(); void TerminateApplication(); int main() {    DisplayDivider("Start Program");    DisplayApplicationInformation();    //insert comment describing these set of statements here    DisplayDivider("Vehicle 1");    vehicle vehicle1;    vehicle1.setMake(GetInput("vehicle make"));    vehicle1.setYear(GetInput("vehicle year"));    vehicle1.setMileage(GetInput("vehicle MPG"));    warranty warranty1;    warranty1.setNumYears(atoi(GetInput("number...

  • Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year :...

    Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year : int ----------------------- + Car() + setMake(m: string) : void + getMake() : string + setYear(y: int) : void + getYear() : int ---------------------- */ #ifndef CAR_H #define CAR_H #include <iostream> using namespace std; class Car { private: string make; int year; public: Car(); Car(string); Car(int); Car(string, int); void setMake (string); string getMake() {return make;} void setYear (int); int getYear() {return year;} }; #endif #include...

  • Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

    Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...

  • Please Help. C++. Need 3 attributes and 3 methods added to the code below. Need the...

    Please Help. C++. Need 3 attributes and 3 methods added to the code below. Need the prototype added to the .h file......and the implementation in the .cpp file....thanks! edited: I just need the above and then I was told to use the methods in the main program; for the vehicle. Any 3 attributes and any three methofds. source.cpp file // Header Comment #include #include #include #include "Automobile.h" using namespace std; struct Address{ string street; string city; string state; string zip;...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE...

    HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE PROGRAM RUNS. Task 1: Enforcing const-ness throughout Your first job will be to go through all of the code and decide which functions should be declared const. You should find several places throughout the program where this makes sense. We will also make the id data member in the Customer class const , as once a customer has been created their ID will never...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

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