This homework is loosely based on Programming Challenge #12 in Chapter 7. 1. Imp
ID: 3628765 • Letter: T
Question
This homework is loosely based on Programming Challenge #12 in Chapter 7.
1. Implement a class named Auto that holds data about an automobile. The class should have the following
private member variables:
make - a string that holds the name of the manufacturor of the automobile
year - an int that holds the year that the automobile was made
speed - an int that hold's the automobile's current speed
a) The class should have at least one constructor.
b) Include mutator functions to assign values to the private member variables and accessor
functions that return the values of each of the member variables.
c) When assigning the current speed, do not allow a negative value. If the user tries to assign a
negative speed, set the speed to zero and write out an informative message to standard output.
d) The year the automobile was made must be between 1900 and 2011 (inclusive.)
Use the class in a source code that creates two Auto objects. The code whould request values from the user
and assign the values to the member variables using the mutator functions. The code should then use the
accessor functions to print out the assigned values.
For this homework, the class definition and the function definitions can be in the same file as the main function.
Hand in:
1. A listing of the code.
2. Display the output when (a) the user enters valid values for all the member variables
and (b) when the user enters either a negative speed or an incorrect year.
Assessment Concerns:
1. Is the program well-doccumented?
2. Is the code easy to read, i.e. well-structured?
3. Is there input validation within the class?
4. Are there any easily detected logic errors?
Explanation / Answer
please rate-thanks
#include <string>
#include <iostream>
using namespace std;
class Auto
{
public:
Auto();
void setSpeed(int);
void setYear(int);
void setMake(string);
int getSpeed();
string getMake();
int getYear();
private:
string make;
int year;
int speed;
};
Auto::Auto()
{
}
void Auto::setSpeed(int s)
{if(s<0)
{cout<<"invalid speed must be >=0-default of 0 being used ";
speed=0;
}
else
speed=s;
}
void Auto::setMake(string m)
{
make=m;
}
void Auto::setYear(int y)
{if(y<1900||y>2011)
{cout<<"invalid year-using default 2010 ";
year=2010;
}
else
year=y;
}
int Auto::getSpeed()
{
return speed;
}
string Auto::getMake()
{
return make;
}
int Auto::getYear()
{
return year;
}
int main()
{int year, speed;
string make;
Auto a,b;
cout<<"for car 1 ";
cout<<"enter make: ";
cin>>make;
a.setMake(make);
cout<<"enter year: ";
cin>>year;
a.setYear(year);
cout<<"enter speed: ";
cin>>speed;
a.setSpeed(speed);
cout<<"for car 2 ";
cout<<"enter make: ";
cin>>make;
b.setMake(make);
cout<<"enter year: ";
cin>>year;
b.setYear(year);
cout<<"enter speed: ";
cin>>speed;
b.setSpeed(speed);
cout<<"car 1: ";
cout<<"make: "<<a.getMake()<<endl;
cout<<"year: "<<a.getYear()<<endl;
cout<<"speed: "<<a.getSpeed()<<endl<<endl;
cout<<"car 2: ";
cout<<"make: "<<b.getMake()<<endl;
cout<<"year: "<<b.getYear()<<endl;
cout<<"speed: "<<b.getSpeed()<<endl<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.