C++ experiment---driver and car

Driver and car

Description

We know that my country’s current driver’s license can be roughly divided into three categories: A, B, and C. The C license can only be used for small passenger cars (truck), the B license can be used for medium and small passenger cars (truck), and the A license has no restrictions. Now please define the following classes:

Automobile: Abstract class, with data member double speed, pure virtual function virtual void run() const = 0.

There are six models, namely small cars Benz and Buick; medium cars Zhongba and Beiqi; and large cars Dayu and Jianghuai. They are all subclasses of Automobile.

The Driver class has two data members: string name and char type. The former is the name of the driver, and the latter is the type of driver's license (A, B, or C) held by the driver. Provides the Drive (Automobile *) method to determine whether the driver can drive the specified car according to the type of driver's license.

Input

Enter multiple lines. The first line is an integer M>0, indicating that there are M test cases afterwards.

Each test case includes four parts: driver's name (without blanks), driver's license type (A, B, or C), and vehicle type (the letters a~f are used to indicate the six vehicle types, and the corresponding vehicle types can be viewed from main() Out) and the driving speed of the car (a positive number in the range of the double type).

Output

There are a total of M lines of output, and each test case corresponds to one line of input. For the specific format, see the sample.

Sample Input

4
zhangsan C a 100.33
lisi C d 100.33
wangwu B f 100.33
Tom A e 300.00

Sample Output

Driver zhangsan can drive Benz at speed of 100.33km/h.
A Benz is erased!
An automobile is erased!
Driver lisi cannot drive bus.
A Beiqi is erased!
An automobile is erased!
Driver wangwu cannot drive large bus.
A Jianghuai is erased!
An automobile is erased!
Driver Tom can drive Dayu at speed of 300.00km/h.
A Dayu is erased!
An automobile is erased!

HINT

1. Use typeid to determine the type of object actually pointed to by a base class pointer.
2. Note: A method of the Driver class is already given in append.cc, do not repeat the definition in the Driver class.

Title given code

void Driver::Drive(Automobile *automobile)
{
    
    
    switch (type)
    {
    
    
    case 'A':
        cout<<"Driver "<<name<<" can drive ";
        automobile->run();
        break;
    case 'B':
        if (typeid(*automobile) == typeid(Dayu) || typeid(*automobile) == typeid(Jianghuai))
            cout<<"Driver "<<name<<" cannot drive large bus."<<endl;
        else
        {
    
    
            cout<<"Driver "<<name<<" can drive ";
            automobile->run();
        }
        break;
    case 'C':
        if (typeid(*automobile) != typeid(Benz) && typeid(*automobile) != typeid(Buick))
            cout<<"Driver "<<name<<" cannot drive bus."<<endl;
        else
        {
    
    
            cout<<"Driver "<<name<<" can drive ";
            automobile->run();
        }
        break;
    }
}
int main()
{
    
    
    string name;
    char type;
    double speed;
    char automobileType;
    int cases;
    Automobile *automobile;
 
 
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
    
    
        cin>>name>>type>>automobileType>>speed;
        Driver driver(name, type);
        switch (automobileType)
        {
    
    
        case 'a':
            automobile = new Benz(speed);
            break;
        case 'b':
            automobile = new Buick(speed);
            break;
        case 'c':
            automobile = new Zhongba(speed);
            break;
        case 'd':
            automobile = new Beiqi(speed);
            break;
        case 'e':
            automobile = new Dayu(speed);
            break;
        case 'f':
            automobile = new Jianghuai(speed);
            break;
        }
        driver.Drive(automobile);
        delete automobile;
    }
    return 0;
}

code:

#include<iostream>
#include<typeinfo>
#include<iomanip>
using namespace std;

class Automobile{
    
    
protected:
	double speed;
public:
	Automobile(double s):speed(s){
    
    }
	
	virtual void run()=0;
		
	virtual ~Automobile(){
    
    
		cout<<"An automobile is erased!"<<endl;
	}
};

class Benz:virtual public Automobile{
    
    
public:
	Benz(double s):Automobile(s){
    
    }
	void run(){
    
    
		cout<<fixed<<setprecision(2)<<"Benz at speed of "<<speed<<"km/h."<<endl;
	}
	~Benz(){
    
    
		cout<<"A Benz is erased!"<<endl;
	}
};

class Buick:virtual public Automobile{
    
    
public:
	Buick(double s):Automobile(s){
    
    
	}
	void run(){
    
    
		cout<<fixed<<setprecision(2)<<"Benz at speed of "<<speed<<"km/h."<<endl;
	}
	~Buick(){
    
    
		cout<<"A Buick is erased!"<<endl;
	}
};

class Zhongba:virtual public Automobile{
    
    
public:
	Zhongba(double s):Automobile(s){
    
    }
	void run(){
    
    
		cout<<fixed<<setprecision(2)<<"Zhongba at speed of "<<speed<<"km/h."<<endl;
	}
	~Zhongba(){
    
    
		cout<<"A Zhongba is erased!"<<endl;
	}
};

class Beiqi:virtual public Automobile{
    
    
public:
	Beiqi(double s):Automobile(s){
    
    }
	void run(){
    
    
		cout<<fixed<<setprecision(2)<<"Beiqi at speed of "<<speed<<"km/h."<<endl;
	}
	~Beiqi(){
    
    
		cout<<"A Beiqi is erased!"<<endl;
	}
};

class Dayu:virtual public Automobile{
    
    
public:
	Dayu(double s):Automobile(s){
    
    }
	void run(){
    
    
		cout<<fixed<<setprecision(2)<<"Dayu at speed of "<<speed<<"km/h."<<endl;
	}
	~Dayu(){
    
    
		cout<<"A Dayu is erased!"<<endl;
	}
};

class Jianghuai:virtual public Automobile{
    
    
public:
	Jianghuai(double s):Automobile(s){
    
    }
	void run(){
    
    
		cout<<fixed<<setprecision(2)<<"Jianghuai at speed of "<<speed<<"km/h."<<endl;
	}
	
	~Jianghuai(){
    
    
		cout<<"A Jianghuai is erased!"<<endl;
	}
};

class Driver{
    
    
	string name;
	char type;
public:
	Driver(string N,char C){
    
    
		name=N;
		type=C;
	}
	
	void Drive(Automobile *automobile);
	
};

void Driver::Drive(Automobile *automobile)
{
    
    
    switch (type)
    {
    
    
    case 'A':
        cout<<"Driver "<<name<<" can drive ";
        automobile->run();
        break;
    case 'B':
        if (typeid(*automobile) == typeid(Dayu) || typeid(*automobile) == typeid(Jianghuai))
            cout<<"Driver "<<name<<" cannot drive large bus."<<endl;
        else
        {
    
    
            cout<<"Driver "<<name<<" can drive ";
            automobile->run();
        }
        break;
    case 'C':
        if (typeid(*automobile) != typeid(Benz) && typeid(*automobile) != typeid(Buick))
            cout<<"Driver "<<name<<" cannot drive bus."<<endl;
        else
        {
    
    
            cout<<"Driver "<<name<<" can drive ";
            automobile->run();
        }
        break;
    }
}
int main()
{
    
    
    string name;
    char type;
    double speed;
    char automobileType;
    int cases;
    Automobile *automobile;
 
 
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
    
    
        cin>>name>>type>>automobileType>>speed;
        Driver driver(name, type);
        switch (automobileType)
        {
    
    
        case 'a':
            automobile = new Benz(speed);
            break;
        case 'b':
            automobile = new Buick(speed);
            break;
        case 'c':
            automobile = new Zhongba(speed);
            break;
        case 'd':
            automobile = new Beiqi(speed);
            break;
        case 'e':
            automobile = new Dayu(speed);
            break;
        case 'f':
            automobile = new Jianghuai(speed);
            break;
        }
        driver.Drive(automobile);
        delete automobile;
    }
    return 0;
}

Ideas:
1) Pay attention to the usage of fixed and setprecision, in the header file:
inside;
usage:

cout<<fixed<<setprecision(2)<<....

2) Pay attention to the usage of the keyword typeid(): it is
used to determine whether an object is an instantiation of a class!

typeid(*对象名)==typeid(子类名)

Pay attention to the declaration header file:
3) To execute the destructor of the abstract class, the destructor of the parent class must not be declared virtual
4) Pay attention to the general abstract function definition method

virtual 返回类型 函数名(参数列表)=0

5) Execute the constructor of the parent class before executing the constructor of the subclass! ! !

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115144828
car