CSI 250L Lab 10 Solution, Spring 2012

American University of Science and Technology · Programming II Lab · 14 pages · Dr. Saeed Raheel · with solution

All CSI 250L files
CSI 250L Lab 10 Solution, Spring 2012, page 1

Stuck on a question? A tutor can walk you through it.

CSI 250L Lab 10 Solution, Spring 2012, page 2 CSI 250L Lab 10 Solution, Spring 2012, page 3 CSI 250L Lab 10 Solution, Spring 2012, page 4 CSI 250L Lab 10 Solution, Spring 2012, page 5 CSI 250L Lab 10 Solution, Spring 2012, page 6 CSI 250L Lab 10 Solution, Spring 2012, page 7 CSI 250L Lab 10 Solution, Spring 2012, page 8 CSI 250L Lab 10 Solution, Spring 2012, page 9 CSI 250L Lab 10 Solution, Spring 2012, page 10

Showing the first 10 of 14 pages. Download the PDF to see all of it.

More CSI 250L files

Show the text of this lab
#include "Car.h" #include <iostream> using namespace std; int Car::count = 0; Car::Car(int y, string p, int t, string b, string c, int m, double ct):Registered_Vehicle(y, p, t) { setBrand(b); setColor(c); setModel(m); setCost(ct); ++count; } Car::~Car() { --count; } double Car::calculate_price() { if(getType() == 1) return (cost+300); else return (cost+200); } int Car::getCount() { return count; } void Car::setBrand(string b) { brand = b; } void Car::setColor(string c) { color = c; } void Car::setModel(int m) { model = m; } void Car::setCost(double c) { cost = c; } string Car::getBrand() { return brand; } string Car::getColor() { return color; } int Car::getModel() { return model; } double Car::getCost() { return cost; } void Car::Print() { Registered_Vehicle::Print(); cout << "Brand : " << getBrand() << ", Color : " << getColor() << "Model : " << getModel() << ", Price : " << calculate_price() << endl ; } #include "Registered_Vehicle.h" #include <string> using std::string; class Car : public Registered_Vehicle { private: string brand; string color; int model; double cost; static int count; public: Car(int=0, string="", int=1, string="", string="", int=0, double=0.0); ~Car(); double calculate_price(); static int getCount(); void Print(); void setBrand(string); void setColor(string); void setModel(int); void setCost(double); string getBrand(); string getColor(); int getModel(); double getCost(); }; #include "Car.h" #include <iostream> using namespace std; int main() { int N; int yr; string plnum; int tp; string bd; string cr; int ml; double ct; cout << "Enter the number of cars to create : "; cin >> N; cout << "Reading Car Information...\n"; Car *c = new Car[N]; for (int i=0;i<N;i++) { cout << "Car No : " << i+1 << endl; cout << "Enter the Year: "; cin >> yr; cout << "Enter the Plate number: "; cin >> plnum; cout << "Enter the Car's type: "; cin >> tp; cout << "Enter the Brand: "; cin >> bd; cout << "Enter the Color: "; cin >> cr; cout << "Enter the Model Number: "; cin >> ml; cout << "Enter the Cost: "; cin >> ct; c[i].setYear(yr); c[i].setPlnum(plnum); c[i].setType(tp); c[i].setBrand(bd); c[i].setColor(cr); c[i].setModel(ml); c[i].setCost(ct); } for (int i=0;i<N;++i) { cout << "Car No (" << (i+1) << ") : " << endl; c[i].Print(); } system("pause"); return 0; } #include "Registered_Vehicle.h" #include <iostream> using namespace std; Registered_Vehicle::Registered_Vehicle(int y, string p, int t) { setYear(y); plnum=p; setType(t); } void Registered_Vehicle::setPlnum(string p) { plnum = p; } void Registered_Vehicle::setType(int tp) { if(tp==1 || tp==2) type=tp; else { type=1; cout << "Invalid entry,will be reset to 1 "; } } void Registered_Vehicle::setYear(int y) { year = y; } void Registered_Vehicle::Print() { cout << "Year : " << getYear() << ", Plate No : " << getPlnum() << ", Type : " << getType() << endl; } int Registered_Vehicle::getType() { return type; } int Registered_Vehicle::getYear() { return year; } string Registered_Vehicle::getPlnum() { return plnum; } #include <string> using std::string; class Registered_Vehicle { private: int year; string plnum; int type; public: Registered_Vehicle(int, string, int); void Print(); void setYear(int); void setPlnum(string); void setType(int); int getYear(); string getPlnum(); int getType(); }; #include "Car_Insurance.h" #include <string.h> car_insurance::car_insurance(int Policy_Number, int type, int year, char *c):Insurance_Policy(Policy_Number) { Kind = (type < 0 || type > 2) ? 1 : type; Car_year = year; Car_num = new char [strlen(c) + 1]; strcpy(Car_num, c); acc_p += Calculate_price(); } float car_insurance::Calculate_price() { switch (Kind) { case 1: if (Car_year < 2000) return (250 + (250 * 15 / 100)); else return 250; break; case 2: if (Car_year < 2000) return (300 + (300 * 15 / 100)); else return 300; break; default: return 123; break; } } #include "Insurance_Policy.h" #ifndef CAR_INSURANCE_H #define CAR_INSURANCE_H class car_insurance:public Insurance_Policy { public: car_insurance(int = 0, int = 0, int = 0, char [] = ""); ~car_insurance() { delete Car_num; } char* Get_name() { return "Car Insurance"; } float Calculate_price(); private: int Kind, Car_year; char *Car_num; }; #endif #include "Insurance_Policy.h" float Insurance_Policy::acc_p = 0; #include <string.h> #ifndef INSURANCE_POLICY_H #define INSURANCE_POLICY_H class Insurance_Policy { public: Insurance_Policy(int Policy_Number = 0) {policy_num = Policy_Number; } virtual char* Get_name () = 0; virtual float Calculate_price () = 0; int Get_policy_num() { return policy_num; } protected: int policy_num; static float acc_p; }; #endif #include "Insurance_Policy.h" #include "Medical-Insurance.h" #include <string.h> MedicalInsurance::MedicalInsurance(int Policy_Number, int Deg, int a, char *Person_name):Insurance_Policy(Policy_Number) { degree = (Deg < 0 || Deg > 3) ? 1 : Deg; age = a; name = new char [strlen(Person_name) + 1]; strcpy(name, Person_name); acc_p += Calculate_price(); } float MedicalInsurance::Calculate_price() { switch (degree) { case 1: if (age > 40) return (200 + (200 * 20 / 100)); else return 200; break; case 2: if (age > 40) return (150 + (250 * 20 / 100)); else return 250; break; case 3: if (age > 40) return (300 + (300 * 20 / 100)); else return 300; break; default: return 123; break; } } #include "Insurance_Policy.h" #ifndef MEDICALINSURANCE_H #define MEDICALINSURANCE_H class MedicalInsurance:public Insurance_Policy { public: MedicalInsurance(int = 0, int = 0, int = 0, char * = ""); ~MedicalInsurance() { delete name; } char* Get_name() { return "Medical-Insurance"; } float Calculate_price(); private: int degree, age; char *name; }; #endif #include "Insurance_Policy.h" #include "Medical-Insurance.h" #include "Car_Insurance.h" #include <iostream> using namespace std; int main () { int Type, Policy_number, Degree, Age, year, car_type; char name[50], car_number[50]; Insurance_Policy *ptr[5]; for (int i = 0; i < 5; i++) { cout << "Enter Policy #: [1 for medical, 2 for car]"; cin >> Type; switch (Type) { case 1: cout << "Enter the attrb. for Medical Insurance "; cin >> Policy_number >> Degree >> Age >> name; ptr[i] = new MedicalInsurance(Policy_number, Degree, Age, name); break; case 2: cout << "Enter the attrb. for car Insurance "; cin >> Policy_number >> car_type >> year; cin >> car_number; cout << "English" << car_number << endl; ptr[i] = new car_insurance(Policy_number, car_type, year, car_number); cout << "The pointer" << endl; break; default: cout << "Error found" << endl; break; } cout << "The price of this class is " << ptr[i]->Calculate_price() << " The class name is " << ptr[i]->Get_name() << endl; } return 0; }