因此,我正在为Uni中的作业创建此作品。
它必须使用以'Vehicle'作为主类的类结构,以及从中继承一些函数的两个类; 'Car'和'Lorry'。
所以我已经成功创建了我的类结构(我认为)但我需要为此创建一个基本的UI,因为它认为它是一个控制台应用程序,我只是使用开关创建一个基本菜单。
然而,在我的“getDetails”部分中,它只是抓住'Vehicle :: getDetails',它还应该获得'Car / lorry :: 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;
}
}
}
答案 0 :(得分:2)
练习的重点是在基础void getDetails()
课程中制作virtual
方法 vehicle
。
(我也在virtual
类中定义了一个vehicle
析构函数,作为良好的编码实践。)
class vehicle {
public:
string manufacturer;
int year;
string regnum;
// Make this virtual
virtual void getDetails() {
...
在派生类中,您可能也想使用the new C++11 override
specifier。
答案 1 :(得分:0)
在c ++中,如果希望某个函数可以被派生类覆盖,则将其标记为virtual
,即:
class vehicle {
...
virtual void getDetails()
{
...
}
...
}