1. Given the following C++ class class vehicle { static enum e_terrain(water, la
ID: 3722654 • Letter: 1
Question
1. Given the following C++ class class vehicle { static enum e_terrain(water, land, air); int topSpeed(){return 0}; e_terrain getTerrain() {return land}; } Define derived classes for an airplane, a boat and a car. You can make up values for speed. Note: you may have to make changes to the base class.
2. Write a simple C++ class that represents a bank account. It should have methods to withdraw and deposit money and to get the balance. (An exception should be thrown if any of these tasks cannot be completed). The program that calls your account class is multi-threaded, so you must ensure that the data in your class cannot be corrupted. Assume you have two library methods available – startCriticalSection() and endCriticalSection().
Explanation / Answer
1)
vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
//Vehicle Class
class Vehicle {
protected:
Vehicle myVehicle[9];
string make; //make
string model; // model
string color; // color
int year; // year
int mileage; // miles on car
string type; //Type of vehicle
public:
//Constructor that will set information for a new car
void New_vehicle (string a, string b, string c, int d, int e)
{make = a; model = b; color = c; year = d; mileage = e;}
Vehicle(); //Default constructor
Vehicle(string, string, string, int, int, string);
//mutator and accessor functions
void setMake(string);
void setModel(string);
void setColor(string);
void setYear(int);
void setMileage(int);
void setType(string);
string getMake();
string getModel();
string getColor();
int getYear();
int getMileage();
string getType();
//Check mileage to see if valid
void valid_mileage(int);
//virtual function
virtual void details() {
}
};
//Sets to default values
Vehicle::Vehicle() {
make = " ";
model = " ";
color = " ";
year = 0;
mileage = 0;
type = " ";
}
Vehicle::Vehicle(string make, string model, string color, int year, int mileage, string type) {
Vehicle::make = make;
Vehicle::model = model;
Vehicle::color = color;
Vehicle::year = year;
valid_mileage(mileage);
Vehicle::type = type;
}
void Vehicle::setMake(string make) {
Vehicle::make = make;
}
void Vehicle::setModel(string model) {
Vehicle::model = model;
}
void Vehicle::setColor(string color) {
Vehicle::color = color;
}
void Vehicle::setYear(int year) {
Vehicle::year = year;
}
void Vehicle::setMileage(int mileage) {
valid_mileage(mileage);
}
void Vehicle::setType(string type) {
Vehicle::type = type;
}
string Vehicle::getMake() {
return make;
}
string Vehicle::getModel() {
return model;
}
string Vehicle::getColor() {
return color;
}
int Vehicle::getYear() {
return year;
}
int Vehicle::getMileage() {
return mileage;
}
string Vehicle::getType() {
return type;
}
void Vehicle::valid_mileage(int mileage) {
if (mileage>=0)
Vehicle::mileage=mileage;
else {
Vehicle::mileage=0;
cout << "WARNING! You have entered invalid mileage! ";
}
Vehicle& getVehicle(int n) {
return myVehicle[n];
}
};
#endif
Car.h
#ifndef CAR_H
#define CAR_H
#include "Vehicle.h"
//Car class that inherits from the vehicle class
class Car:public Vehicle
{
private:
//There are no new attributes
public:
void details() {
cout << "The current car is a " << year << ' ' << color << ' '
<< make << ' ' << model << " with " << mileage << " miles. ";
}
};
#endif
Vehicle.cpp file
#include "Car.h"
using namespace std;
int main() {
int tempInt;
string tempString;
Vehicle myVehicle;
for (int i = 0; i<9; i++) {
cout << "Enter the Make of the Vehicle: ";
cin >> tempString;
myVehicle.getVehicle(i).setMake(tempString);
cout << "Enter the Model of the Vehicle: ";
cin >> tempString;
myVehicle.getVehicle(i).setModel(tempString);
cout << "Enter the Color of the Vehicle: ";
cin >> tempString;
myVehicle.getVehicle(i).setColor(tempString);
cout << "Enter the Year of the Vehicle: ";
cin >> tempInt;
myVehicle.getVehicle(i).setYear(tempInt);
cout << "Enter the Mileage of Vehicle: ";
cin >> tempInt;
myVehicle.getVehicle(i).setMileage(tempInt);
cout << "Enter the Type of Vehicle: ";
cin >> tempString;
myVehicle.getVehicle(i).setType(tempString);
cout << "Enter the Bed Size of Truck(if entered as truck): ";
cin >> tempInt;
myVehicle.getVehicle(i).setBedSize(tempInt);
cout << "Enter the Bike Type(if entered as bike): ";
cin >> tempString;
myVehicle.getVehicle(i).setBikeType(tempString);
cout << "__________________________________________________________ "<< endl;
}
2) Program to represent a bank account (implemented as a Class)
# include<iostream.h>
# include<conio.h>
# include<iomanip.h>
class bank
{
char name[20];
int acno;
char actype[4];
float balance;
public:
void init();
void deposit();
void withdraw();
void disp_det();
};
//member functions of bank class
void bank :: init()
{
cout<<"
New Account
";
cout<<"
Enter the Name of the depositor : ";
cin.get(name,19,'
');
cout<<"
Enter the Account Number : ";
cin>>acno;
cout<<"
Enter the Account Type : (CURR/SAVG/FD/RD/DMAT) ";
cin>>actype;
cout<<"
Enter the Amount to Deposit : ";
cin >>balance;
}
void bank :: deposit()
{
float more;
cout <<"
Depositing
";
cout<<"
Enter the amount to deposit : ";
cin>>more;
balance+=more;
}
void bank :: withdraw()
{
float amt;
cout<<"
Withdrwal
";
cout<<"
Enter the amount to withdraw : ";
cin>>amt;
balance-=amt;
}
void bank :: disp_det()
{
cout<<"
Account Details
";
cout<<"Name of the depositor : "<<name<<endl;
cout<<"Account Number : "<<acno<<endl;
cout<<"Account Type : "<<actype<<endl;
cout<<"Balance : $"<<balance<<endl;
}
// main function , exectution starts here
void main(void)
{
clrscr();
bank obj;
int choice =1;
while (choice != 0 )
{
cout<<"
Enter 0 to exit
1. Initialize a new acc.
2. Deposit
3.Withdraw
4.See A/c Status";
cin>>choice;
switch(choice)
{
case 0 :obj.disp_det();
cout<<"
EXITING PROGRAM.";
break;
case 1 : obj.init();
break;
case 2: obj.deposit();
break;
case 3 : obj.withdraw();
break;
case 4: obj.disp_det();
break;
default: cout<<"
Illegal Option"<<endl;
}
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.