E
emagutu
Hi,
I have been geiven the following Task.
Class, inheritance, polymorphism, file handling, arrays or linked list.
A C++ program is required to handle three types of objects:
- Trucks
- Cars with engine capacity greater than 1200cc
- Cars with engine capacity smaller than or equal to 1200cc
The annual road tax for a truck is $3000. The annual road tax for a car
with engine capacity greater than 1200cc is $300 and for a car with
engine capacity less than or equal to 1200cc is $180. The information
is recorded for each type of object as shown:
Truck
-Registration number
-Engine capacity
-Annual road tax
-Tonnage
Cars with engine capacity > 1200cc
-Registration number
-Engine capacity
-Annual road tax
-Number of passengers
Cars with engine capacity <= 1200cc
-Registration number
-Engine capacity
-Annual road tax
A text file holds the information on all the vehicles within the
company as shown:
T W123MGB 5200 12 (T stands for a truck object)
L K670KVC 2200 7 (L stands for a car with engine capacity > 1200cc)
S Y317JNB 1100 (S stands for a car with engine capacity <= 1200cc)
The requirement for the program is to:
a. Read all the records from the file and to create an object for each
type of vehicle
b. To provide a display method which, given a registration number,
prints the details of that particular vehicle.
c. As an additional measure, the program should be able to calculate
the total amount of road tax paid in a year.
Develop the program to implement the specification. The program should
use inheritance or polymorphism if appropriate. The number of records
in the file does not exceed 100.
I have done the following so far but it looks very messy and has a few
bugs.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>//chararacter manipulations
#include <sstream>
using namespace std;
class vehicle
{
public:
vehicle(){}
vehicle ( string r, double e, float rt,int y):regNo(r),
engineCap(e), roadTax(rt), years(y){}
~vehicle () {}
virtual double compute_tax(void)
{return 0.0;}
int years;
//functions
//private:
double engineCap;
float roadTax;
string regNo;
};
//Truck
class Truckublic vehicle
{
public:
Truck(){}
Truck(float ton, string r, double e, float rt, int y):vehicle(r,
e, rt, y),t(ton){}
~Truck(){}
double compute_tax(void)
{return years * 3000; }
//private:
float t;
};
//car with engine a capacity less than or equal to 1200cc
class carSmallublic vehicle
{
public:
carSmall(){}
carSmall(int p, string r, double e, float rt, int y):vehicle(r,
e, rt, y),noOfPass(p){}
~carSmall(){}
double compute_tax(void)
{return years * 180; }
//private:
int noOfPass;
};
//car with engine capacity greater than 1200cc
class carBigublic vehicle
{
public:
carBig(){}
carBig(int p2, string r, double e, float rt, int y):vehicle(r,
e, rt, y),noOfPass(p2){}
~carBig(){}
double compute_tax(void)
{return years * 300; }
//private:
int noOfPass;
};
void main()
{
int a = 0;
do{
Truck truck1[100];
carBig carBig1[100];
carSmall carSmall1[100];
int count = 0, nt = 0, bt = 0, st = 0;
ifstream infile("vehicles.txt", ios::in);
//if(!infile.is_open())
//{
// cout<<"Opening vehicles file failed\n"<<endl;
//}
//else {cout<<"Vehicles file opened\n"<<endl;}
int d = 0;
string line1, str1;
char t = 'T', s = 'S', b = 'L';
string search ;
cout<<"**************MENU***************"<<endl;
cout<<"*****What do you want to do? ****\n"<<endl;
cout<<"1. Search for a record and display"<<endl;
cout<<"2. Create objects, display no of each type of vehicles \n and
calculate tax per year"<<endl;
cout<<"3. Exit\n\n";
cin>>a;
switch(a){
case 1:
cout<<"What is the registration no?"<<endl;
cin>>search;
while(getline(infile, line1)){
if (line1.substr(2,7) == search){
cout<<"The details are\n"<<"In RegNo Ton/No of
pass\n\n"<<line1<<"\n";
}
/*if(d == 0){
cout<<"No such vehicle with the RegNo "<<search<<"
exists"<<endl;
d++;
}*/
}
break;
case 2:
while(getline(infile, line1)) //getline takes two arguments
the file and a string object.
{
if (line1.at(0) == b){
//cout<<"This is a Big car\n"<<endl;
stringstream ss(line1.substr(10,4));
stringstream s3(line1.substr(15,2));
double eng = 0.0;
float pas = 0.0;
s3>>pas;
ss>>eng;
//create object of type truck
int x = 0;
carBig1[count].years = x;
carBig1[count].engineCap = eng;
carBig1[count].regNo = line1.substr(2,7);
carBig1[count].roadTax = 300;
carBig1[count].noOfPass = pas;
/*cout<<"regNo = "<<carBig1[count].regNo<<endl;
cout<<"Engine capacity = "<<carBig1[count].engineCap<<endl;
cout<<"Tonnage = "<<carBig1[count].noOfPass<<"\n"<<endl;*/
bt ++;
}
else if (line1.at(0) == s){
//cout<<"This is a Small car\n"<<endl;
stringstream ss(line1.substr(10,4));
stringstream s3(line1.substr(15,2));
double eng = 0.0;
float pass = 0.0;
s3>>pass;
ss>>eng;
//create object of type truck
int x = 0;
carSmall1[count].years = x;
carSmall1[count].engineCap = eng;
carSmall1[count].regNo = line1.substr(2,7);
carSmall1[count].roadTax = 180;
carSmall1[count].noOfPass = pass;
/*cout<<"regNo = "<<carSmall1[count].regNo<<endl;
cout<<"Engine capacity = "<<carSmall1[count].engineCap<<endl;
cout<<"Tonnage = "<<carSmall1[count].noOfPass<<"\n"<<endl;*/
st++;
}
else if(line1.at(0) == t ){
//cout<<"This is a Truck\n"<<endl; //cout<<str1<<endl;
stringstream ss(line1.substr(10,4));
stringstream s3(line1.substr(15,2));
double eng = 0.0;
float tonn = 0.0;
s3>>tonn;
ss>>eng;
//create object of type truck
int x = 0;
int count = 0;
truck1[count].years = x;
truck1[count].engineCap = eng;
truck1[count].regNo = line1.substr(2,7);
truck1[count].roadTax = 3000;
truck1[count].t = tonn;
/*cout<<"regNo = "<<truck1[count].regNo<<endl;
cout<<"Engine capacity = "<<truck1[count].engineCap<<endl;
cout<<"Tonnage = "<<truck1[count].t<<"\n"<<endl;*/
nt++;
//cout<<"Record 4"<<truck1[].regNo<<"\n";
}
else
cout<<"Wrong record"<<endl;
count++;
line1.erase(line1.length());
}
cout<<"\nNo of Truck = "<<nt<<endl;
cout<<"No of Small cars = "<<st<<endl;
cout<<"No of Big cars = "<<bt<<"\n";
cout<<"\nTotal road tax = $"<<(nt * 3000) + (st * 180) + (bt *
300)<<"\n"<<endl;
break;
}
}while(a != 3);
}
I have been geiven the following Task.
Class, inheritance, polymorphism, file handling, arrays or linked list.
A C++ program is required to handle three types of objects:
- Trucks
- Cars with engine capacity greater than 1200cc
- Cars with engine capacity smaller than or equal to 1200cc
The annual road tax for a truck is $3000. The annual road tax for a car
with engine capacity greater than 1200cc is $300 and for a car with
engine capacity less than or equal to 1200cc is $180. The information
is recorded for each type of object as shown:
Truck
-Registration number
-Engine capacity
-Annual road tax
-Tonnage
Cars with engine capacity > 1200cc
-Registration number
-Engine capacity
-Annual road tax
-Number of passengers
Cars with engine capacity <= 1200cc
-Registration number
-Engine capacity
-Annual road tax
A text file holds the information on all the vehicles within the
company as shown:
T W123MGB 5200 12 (T stands for a truck object)
L K670KVC 2200 7 (L stands for a car with engine capacity > 1200cc)
S Y317JNB 1100 (S stands for a car with engine capacity <= 1200cc)
The requirement for the program is to:
a. Read all the records from the file and to create an object for each
type of vehicle
b. To provide a display method which, given a registration number,
prints the details of that particular vehicle.
c. As an additional measure, the program should be able to calculate
the total amount of road tax paid in a year.
Develop the program to implement the specification. The program should
use inheritance or polymorphism if appropriate. The number of records
in the file does not exceed 100.
I have done the following so far but it looks very messy and has a few
bugs.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>//chararacter manipulations
#include <sstream>
using namespace std;
class vehicle
{
public:
vehicle(){}
vehicle ( string r, double e, float rt,int y):regNo(r),
engineCap(e), roadTax(rt), years(y){}
~vehicle () {}
virtual double compute_tax(void)
{return 0.0;}
int years;
//functions
//private:
double engineCap;
float roadTax;
string regNo;
};
//Truck
class Truckublic vehicle
{
public:
Truck(){}
Truck(float ton, string r, double e, float rt, int y):vehicle(r,
e, rt, y),t(ton){}
~Truck(){}
double compute_tax(void)
{return years * 3000; }
//private:
float t;
};
//car with engine a capacity less than or equal to 1200cc
class carSmallublic vehicle
{
public:
carSmall(){}
carSmall(int p, string r, double e, float rt, int y):vehicle(r,
e, rt, y),noOfPass(p){}
~carSmall(){}
double compute_tax(void)
{return years * 180; }
//private:
int noOfPass;
};
//car with engine capacity greater than 1200cc
class carBigublic vehicle
{
public:
carBig(){}
carBig(int p2, string r, double e, float rt, int y):vehicle(r,
e, rt, y),noOfPass(p2){}
~carBig(){}
double compute_tax(void)
{return years * 300; }
//private:
int noOfPass;
};
void main()
{
int a = 0;
do{
Truck truck1[100];
carBig carBig1[100];
carSmall carSmall1[100];
int count = 0, nt = 0, bt = 0, st = 0;
ifstream infile("vehicles.txt", ios::in);
//if(!infile.is_open())
//{
// cout<<"Opening vehicles file failed\n"<<endl;
//}
//else {cout<<"Vehicles file opened\n"<<endl;}
int d = 0;
string line1, str1;
char t = 'T', s = 'S', b = 'L';
string search ;
cout<<"**************MENU***************"<<endl;
cout<<"*****What do you want to do? ****\n"<<endl;
cout<<"1. Search for a record and display"<<endl;
cout<<"2. Create objects, display no of each type of vehicles \n and
calculate tax per year"<<endl;
cout<<"3. Exit\n\n";
cin>>a;
switch(a){
case 1:
cout<<"What is the registration no?"<<endl;
cin>>search;
while(getline(infile, line1)){
if (line1.substr(2,7) == search){
cout<<"The details are\n"<<"In RegNo Ton/No of
pass\n\n"<<line1<<"\n";
}
/*if(d == 0){
cout<<"No such vehicle with the RegNo "<<search<<"
exists"<<endl;
d++;
}*/
}
break;
case 2:
while(getline(infile, line1)) //getline takes two arguments
the file and a string object.
{
if (line1.at(0) == b){
//cout<<"This is a Big car\n"<<endl;
stringstream ss(line1.substr(10,4));
stringstream s3(line1.substr(15,2));
double eng = 0.0;
float pas = 0.0;
s3>>pas;
ss>>eng;
//create object of type truck
int x = 0;
carBig1[count].years = x;
carBig1[count].engineCap = eng;
carBig1[count].regNo = line1.substr(2,7);
carBig1[count].roadTax = 300;
carBig1[count].noOfPass = pas;
/*cout<<"regNo = "<<carBig1[count].regNo<<endl;
cout<<"Engine capacity = "<<carBig1[count].engineCap<<endl;
cout<<"Tonnage = "<<carBig1[count].noOfPass<<"\n"<<endl;*/
bt ++;
}
else if (line1.at(0) == s){
//cout<<"This is a Small car\n"<<endl;
stringstream ss(line1.substr(10,4));
stringstream s3(line1.substr(15,2));
double eng = 0.0;
float pass = 0.0;
s3>>pass;
ss>>eng;
//create object of type truck
int x = 0;
carSmall1[count].years = x;
carSmall1[count].engineCap = eng;
carSmall1[count].regNo = line1.substr(2,7);
carSmall1[count].roadTax = 180;
carSmall1[count].noOfPass = pass;
/*cout<<"regNo = "<<carSmall1[count].regNo<<endl;
cout<<"Engine capacity = "<<carSmall1[count].engineCap<<endl;
cout<<"Tonnage = "<<carSmall1[count].noOfPass<<"\n"<<endl;*/
st++;
}
else if(line1.at(0) == t ){
//cout<<"This is a Truck\n"<<endl; //cout<<str1<<endl;
stringstream ss(line1.substr(10,4));
stringstream s3(line1.substr(15,2));
double eng = 0.0;
float tonn = 0.0;
s3>>tonn;
ss>>eng;
//create object of type truck
int x = 0;
int count = 0;
truck1[count].years = x;
truck1[count].engineCap = eng;
truck1[count].regNo = line1.substr(2,7);
truck1[count].roadTax = 3000;
truck1[count].t = tonn;
/*cout<<"regNo = "<<truck1[count].regNo<<endl;
cout<<"Engine capacity = "<<truck1[count].engineCap<<endl;
cout<<"Tonnage = "<<truck1[count].t<<"\n"<<endl;*/
nt++;
//cout<<"Record 4"<<truck1[].regNo<<"\n";
}
else
cout<<"Wrong record"<<endl;
count++;
line1.erase(line1.length());
}
cout<<"\nNo of Truck = "<<nt<<endl;
cout<<"No of Small cars = "<<st<<endl;
cout<<"No of Big cars = "<<bt<<"\n";
cout<<"\nTotal road tax = $"<<(nt * 3000) + (st * 180) + (bt *
300)<<"\n"<<endl;
break;
}
}while(a != 3);
}