NEW RELEASE!!
C PLUS PLUS (C++)
BY PROF SAM
QUESTIONS AND ANSWERS
Q1. Date
Design a class called Date that has integer data members to
store month, day, and year. The
class should have a three-parameter default constructor that
allows the date to be set at the
time a new Date object is created. If the user creates a
Date object without passing any
arguments, or if any of the values passed are invalid, the
default values of 1, 1, 2001 (i.e.,
January 1, 2001) should be used. The class should have
member functions to print the date
in the following formats:
3/15/13
March 15, 2013
15 March 2013
Demonstrate the class by writing a program that uses it. Be
sure your program only accepts
reasonable values for month and day. The month should be
between 1 and 12. The day
should be between 1 and the number of days in the selected
month.
ANSWER.
#include <iostream>
#include <cstring>
using namespace std;
class Date
{
int
month;
int
day;
int
year;
public:
int date(int month = 1, int day = 1, int
year = 2001)
{
Date::month = month;
Date::day = day;
Date::year = year;
}
void Date:: showdate();
~Date(){
}
};
void Date::showDate(){
cout<<"/"<<day<<"/"<<year<<endl;
}
int main()
{
int
month, day, year;
string
monthName[12] = {"january", "february", "march",
"april", "may", "june", "july",
"august", "september", "october",
"november", "december"};
cout<<"enter
month (between 1 and12)"<<endl;
cin>>month;
if(month>12
|| month<1){
month
= 1;
}
}
cout<<"enter day (between 1 and
31)"<<endl;
cin>>day;
if(day>31 || day <1){
day =
1;
}
cout<<"enter year (between 1900 and
2016)"<<endl;
cin>>year;
if(year >2016 || year<1900){
year =
2015;
}
date newDate(month, day, year);
newDate.showDate();
cout<<monthName[month-1]<<""<<day<<","<<year<<endl;
cout<<day<<""<<monthName[monyh-1]<<""<<year<<endl;
return 0;
}
Q2. Car Class
Write a class named Car that has the following member
variables:
• year. An int that holds the car’s model year.
• make. A string object that holds the make of the car.
• speed. An int that holds the car’s current speed.
In addition, the class should have the following member
functions.
• Constructor. The constructor should accept the car’s year
and make as arguments
and assign these values to the object’s year and make member
variables. The
constructor should initialize the speed member variable to
0.
• Accessors. Appropriate accessor functions should be
created to allow values to
be retrieved from an object’s year, make, and speed member
variables.
• accelerate. The accelerate function should add 5 to the
speed member
variable each time it is called.
• brake. The brake function should subtract 5 from the speed
member variable
each time it is called.
Demonstrate the class in a program that creates a Car
object, and then calls the accelerate
function five times. After each call to the accelerate
function, get the current speed of the
car and display it. Then, call the brake function five
times. After each call to the brake
function, get the current speed of the car and display it.
ANSWER.
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
class Car
{
private:
int
YearModel;
int
Speed;
string
Make;
public:
Car(int,
string, int);
string
getMake();
int
getModel();
int
getSpeed();
void
Accelerate();
void
Brake();
};
//int YearModel; string Makeby; int Speed;
void Car:: Accelerate()
{
int
YearModel; string Makeby; int Speed, YearOfModel, Spd;
YearModel
= YearOfModel;
Make=Makeby;
Speed
=Spd;
}
string Car::getMake()
{
return
Make;
}
int Car::getModel()
{
return
YearModel;
}
int Car::getSpeed()
{
return
Speed;
}
void Car::Accelerate()
{
Speed =
Speed + 5;
}
void Car::Brake()
{
Speed =
Speed - 5;
}
int main()
{
int
Speed =0;
int
index;
int
total = 0;
cout<<"Your V8 speed is
"<<Speed<<"mph"<<endl;
Car first(2008, "Pontiac", Speed);
total = Speed;
for(index = 0; index<6; index++)
{
first.Accelerate();
cout<<"The
car is accelerating at "<<Speed<<"mph"<<endl;
}
cout<<"The
cars current speed is "<<Speed<<"mph"<<endl;
for(index
= 0; index < 6; index++)
{
first.Brake();
cout<<"The
car is breaking. the speed now is
"<<Speed<<"mph"<<endl;
}
cout<<"the
cars speed is now "<<Speed<<"mph"<<endl;
return
0;
}
Q3. Population
In a population, the birth rate and death rate are
calculated as follows:
Birth Rate = Number of Births ÷ Population
Death Rate = Number of Deaths ÷ Population
For example, in a population of 100,000 that has 8,000
births and 6,000 deaths per year,
Birth Rate = 8,000 ÷ 100,000 = 0.08
Death Rate = 6,000 ÷ 100,000 = 0.06
Design a Population class that stores a current population,
annual number of births, and
annual number of deaths for some geographic area. The class
should allow these three
values to be set in either of two ways: by passing arguments
to a three-parameter
constructor when a new Population object is created or by
calling the setPopulation,
setBirths, and setDeaths class member functions. In either
case, if a population figure
less than 2 is passed to the class, use a default value of
2. If a birth or death figure less than
0 is passed in, use a default value of 0. The class should
also have getBirthRate and
getDeathRate functions that compute and return the birth and
death rates. Write a short
program that uses the Population class and illustrates its
capabilities.
ANSWER.
#include <iostream>
#include <iomanip>
using namespace std;
class Pop
//decllare a class
{
private:
int
population; //member variables
int
births;
int
deaths;
public:
void
setPopulation(int); //function
prototypes
void setBirths(int);
void setDeaths(int);
int getPopulation();
double getBirthRate();
double getDeathRate();
Pop():
population(0),
births(0),
deaths(0) {
} };
void Pop::setPopulation(int p) {population = p;
}
void Pop::setBirths(int b)
{births = b;}
void Pop::setDeaths(int d) {deaths = d;
}
int Pop::getPopulation(){return population;
}
double
Pop:: getBirthRate()
{return
births/static_cast <double>(population);
}
double
Pop::getDeathRate()
{
return
deaths/static_cast <double>(population);
}
int
main()
{
Pop
myTown;
int
numPeople;
int
numBirths;
int
numDeaths;
cout<<"Enter
the total population\n\n";
cin>>numPeople;
while(numPeople
< 1)
{
cout<<"value
must be greater than 0, please re-enter\n\n";
cin>>numPeople;
}
myTown.setPopulation(numPeople);
cout<<"Enter
annual number of births\n\n";
cin>>numBirths;
while(numBirths
< 0)
{
cout<<"value
cannot be negative, please re-enter \n\n";
cin>>numBirths;
}
myTown.setBirths(numBirths);
cout<<"Enter
annual number of deaths\n\n";
cin>>numDeaths;
while(numDeaths
< 0)
{
cout<<"value
cannot be negative, please re-enter\n\n";
cin>>numDeaths;
}
myTown.setDeaths(numDeaths);
cout<<"population
statistics ";
cout<<fixed<<showpoint<<setprecision(3);
cout<<"\n\tPopulation:
" <<setw(7)<<myTown.getPopulation();
cout<<"\n\tBirth
rate: "<<setw(7)<<myTown.getBirthRate();
cout<<"\n\tDeath
Rate:
"<<setw(7)<<myTown.getDeathRate()<<endl;
return
0;
}
-
Comments