类继承问题c++

Class inheritance issues c++

本文关键字:c++ 问题 继承      更新时间:2023-10-16

所以我为Uni中的一个赋值创建了这个。

它必须使用一个以"Vehicle"为主要类的类结构,以及从中继承一些功能的两个类汽车和卡车。

所以我已经成功地创建了我的类结构(我认为),但我需要为此创建一个基本的UI,记住它是一个控制台应用程序,我只是使用开关创建一个基础菜单。

然而,在我的"getDetails"部分中,它只获取了"Vehicle::getDetails",而它本应获取"Car/lock::getDetails"以及车辆详细信息。

你知道是什么原因造成的吗?

第一个帖子在这里,所以很抱歉,如果我的帖子不好:(.

谢谢!

#include <iostream> 
#include <string>
#include <fstream>
using namespace std;

class vehicle {    
public:     
string manufacturer;
int year;
string regnum;
void getDetails() { 
    cout << "Please enter the details for your vehicle"<< endl;
    cout << "Please enter the manufacturer of your vehicle: "<< endl;
    cin >> manufacturer;
    cout << "Please enter the year of your vehicle's manufacture: "<< endl;
    cin >> year;
    cout << "Please enter your vehicle's registration number:  "<< endl;
    cin >> regnum;
}

void printDetails() {
    cout << "Your vehicle's details are as follows: " << endl;
    cout << "Your Vehicle's manufacturer is " << manufacturer << endl;
    cout << "Your Vehicle's year of manufacture is " << year << endl;
    cout << "Your Vehicle's registration number is " << regnum << endl;
}
void saveDetails() { 
    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "***Your Vehicle's Details***" << endl;
    vehiclefile << "Manufacturer:" << manufacturer << endl;
    vehiclefile << "Year of Manufacture:" << year << endl;
    vehiclefile << "Registration Number: " << regnum << endl;
    vehiclefile.close();
}
void openVehicleDetails() { 
}
};

class car : public vehicle{
public:
int numpassengers;
string cartype;
void getDetails() { 
    vehicle::getDetails();
    cout << "Please enter the number of maximum passengers your car can hold: "<< endl;
    cin >> numpassengers;
    cout << "Please enter the car body type: "<< endl;
    cin >> cartype;
    cout << "Thank your for your details"<< endl;
}
void printDetails() {
    vehicle::printDetails();
    cout << "Your car's maximum passengers is: " << numpassengers << endl;
    cout << "The body type of your car is: " << cartype << endl;
}
void saveDetails() { 
    vehicle::saveDetails();
    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "Car or Lorry: Car" << endl;
    vehiclefile << "Number of passengers: " << numpassengers << endl;
    vehiclefile << "Type of car: " << cartype << endl;
    vehiclefile.close();
}
};

class lorry : public vehicle{
public:
double tonnage;
string bodtype;
void getDetails() {
    vehicle::getDetails();
    cout << "Please enter the gross weight of your Lorry: "<< endl;
    cin >> tonnage;
    cout << "Please enter the body type of your Lorry: "<< endl;
    cin >> bodtype;
    cout << "Thank your for your details"<< endl;
}
void printDetails() {
    vehicle::printDetails();
    cout << "Your lorry's details are as follows: " << endl;
    cout << "Your lorry's maximum weight is: " << tonnage << endl;
    cout << "The body type of your lorry is: " << bodtype << endl;
}
void saveDetails() { 
    vehicle::saveDetails();
    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "Car or Lorry: Lorry" << endl;
    vehiclefile << "Maximum weight: " << tonnage << endl;
    vehiclefile << "Body type: " << bodtype << endl;
    vehiclefile.close();
}
};

int main () {
int flag = 0;
char choice;
int ifchoice;
vehicle*v;

while (flag == 0){
    cout << "***Main Menu***" << endl;      //Menu to allow ease of access within the program.
    cout << "Select by letter:" << endl;     
    cout << "1 - Add new entry" << endl;
    cout << "2 - Show entry" << endl;
    cout << "3 - Save entry" << endl;
    cout << "4 - Open saved document" << endl;
    cout << "5 - Delete entry" << endl;
    cin >> choice;          
    switch(choice) {       
    case '1':
        cout << "Is your vehicle a Car or a Lorry? " << endl;
        cout << "Press '1' for Car " << endl;
        cout << "Press '2' for Lorry " << endl;
        cin >> ifchoice;
            if (ifchoice == 1)
            {
                v = new car();
            }
            if (ifchoice == 2)
            {
                v = new lorry();
            }
            v->getDetails();
        break;
    case '2':
        v -> printDetails();
        break;
    case '3': 
        v -> saveDetails();
        break;


   }    
}
}

练习的重点是在基本vehicle类中制作void getDetails()方法virtual

(我也会在vehicle类中定义一个virtual析构函数,作为良好的编码实践。)

class vehicle {    
public:         
    string manufacturer;
    int year;
    string regnum;
    // Make this virtual
    virtual void getDetails() { 
         ...

在派生类中,您可能还需要使用新的C++11 override说明符。

在c++中,如果希望一个函数被派生类重写,则将其标记为virtual,即:

class vehicle {
    ...
    virtual void getDetails()
    {
        ...
    }
    ...
}