Dynamic objects from files

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 Truck:public 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 carSmall:public 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 carBig:public 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);
}
 
J

Johan Borkhuis

emagutu said:
I have been geiven the following Task.

Good for you.
Class, inheritance, polymorphism, file handling, arrays or linked list.
JIKES!

A C++ program is required to handle three types of objects:

Then why are you asking this question here? This is a C newsgroup.

Kind regards,
Johan

--
o o o o o o o . . . _____J_o_h_a_n___B_o_r_k_h_u_i_s___
o _____ || http://www.borkhuis.com |
.][__n_n_|DD[ ====_____ | (e-mail address removed) |
>(________|__|_[_________]_|________________________________|
_/oo OOOOO oo` ooo ooo 'o!o!o o!o!o`
== VxWorks FAQ: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ==
 
M

Martin Ambuhl

emagutu said:
Hi,
I have been geiven the following Task.
Class, inheritance, polymorphism, file handling, arrays or linked list.

Class, inheritance, and polymorphism are not language features in C.
Object-oriented programming can be done in C, to be sure, but those are
not 'features' of C. C++ is designed for that, but questions about that
*different* language are to be asked in comp.lang.c++. Before you rush
off to do so, do what you anti-socially refused to do before posting to
comp.lang.c: follow a week or so of posts to the newsgroup and check the
FAQ for prior art.
A C++ program is required to handle three types of objects:

C++ programs are not require to handle your three types of objects
(Trucks, ars with engine capacity greater than 1200cc, Cars with engine
capacity smaller than or equal to 1200cc).

If you need C++, why did you post here? I remember: it's because you
didn't bother to follow the newsgroup or check its FAQ before posting.

[...]
I have done the following so far but it looks very messy and has a few
bugs.

It sure does look messy. It looks like the obfuscated crap C++ is good
at encouraging people to write. The people over in comp.lang.c++ like
obfuscated messes, though. Go ask them.
 
M

Malcolm

emagutu said:
I have been geiven the following Task.
Class, inheritance, polymorphism, file handling, arrays or linked list.

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.
Universities often try to teach object orientation far too early. You can't
possible make a good job tackling the complexities if you don't even know
the difference between C and C++.

However I'm not your tutor. As a hint, decide what methods any vehicle must
support, what methods any car must support, and what methods your two types
of car and truck must support which general vehicles / cars don't support.
 
D

Dave Thompson

emagutu wrote:

C++ programs are not require to handle your three types of objects
(Trucks, ars with engine capacity greater than 1200cc, Cars with engine
capacity smaller than or equal to 1200cc).
"A foo is required to bar" can mean universal quantification: for any
x that is a foo, x is required to bar. But it can also mean there is
need of some foo, call it x, which does bar. In an assignment of work,
like this homework, the latter is more common.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top