类继承问题c++
Class inheritance issues c++
所以我为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()
{
...
}
...
}
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题