1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
class Car{
protected:
int max_speed,
horsepower,
year;
public:
virtual void GetInfo(){
cout<<"Car with "<<max_speed<<" and "<<horsepower<<" made in "<<year<<std::endl;
}
Car(int ms, int hp, int y){
max_speed=ms;
horsepower=hp;
year=y;
}
};
class Bmw: public Car{
float number;//Altho I dunno what this is for, I have made it BMW specific
public:
void GetInfo(){
cout<<"BMW with "<<number<<" and "<<max_speed<<" and "<<horsepower<<" made in "<<year<<std::endl;
}
Bmw(int ms, int hp, int y, float n):Car(ms, hp, y){
number=n ;
}
};
class Mercedez: public Car{
int rear_lights;//Mercedez specific variable
public:
void GetInfo(){
cout<<"Mercedez with "<<rear_lights<<" rear lights and "<<max_speed<<" and "<<horsepower<<" made in "<<year<<std::endl;
}
Mercedez(int ms, int hp, int y, int rl): Car(ms, hp, y){
rear_lights=rl;
}
};
int main(){
vector<Car*> database;
//Create instances
database.push_back(new Car(1,2,3));
database.push_back(new Bmw(4,5,6,7.8));
database.push_back(new Mercedez(9,10,11,12));
for(int i=0; i<database.size(); i++){//Call the function and delete car
database[i]->GetInfo();
delete database[i];
}
std::cin.get();
return 0;
}
|