多重继承问题 C++

multiple inheritance issue c++

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

我在多重继承方面遇到了麻烦。

#pragma once
#include <iostream>
#include <list>
class Data {
protected:
    int year;
    int month;
    int day;
public:
    Data();
    Data(int, int, int);
    void show();
};
class Person : virtual public Data {
protected:
    std::string Name;
    std::string Sur_name;
public:
    Person();
    Person(std::string, std::string, int, int, int);
    void show();
};
class Waiter : public Person {
protected:
    int category;
public:
    Waiter();
    Waiter(std::string, std::string, int d, int m, int y, int c);
    void show();
};
class TypeOfDish {
protected:
    std::string typeDish;
public:
    TypeOfDish();
    TypeOfDish(std::string);
    void show();
};
class Course : public TypeOfDish {
protected:
    double price;
    std::string NameofCourse;
public:
    Course();
    Course(std::string,  double, std::string);
    void show();
};
class Order : public Course, public Waiter,  virtual public Data{
public:
    Order();
    void show();
};
//

/

#include "stdafx.h"
#include "Class.h"
#include <string>
Data::Data() {
    day = 21;
    month = 10;
    year = 2017;
}
Data::Data(int d, int m, int y) {
    this->day = d;
    this->month = m;
    this->year = y;
}
void Data::show() {
    std::cout << "Hello from Data.Date of birth " << day << " " << month << " " << year << std::endl;
}
Person::Person() : Data() {
    day = 22;
    month = 10;
    year = 2017;
    Name = "Ivan";
    Sur_name = "Petrov";
}
Person::Person(std::string n, std::string s, int d, int m, int y) : Data(d,m,y) {
    this->Name = n;
    this->Sur_name = s;
}
void Person::show() {
    std::cout << "Hello from Person " << Name << " " << Sur_name << std::endl;
    std::cout << "Hello from Person.Date of birth " << day << " " << month << " " << year << std::endl;
}
Waiter::Waiter() : Person() {
    category = 3;
}
Waiter::Waiter(std::string n, std::string s, int d, int m, int y, int c) : Person(n,s,d,m,y) {
    this->category = c;
}
void Waiter::show() {
    std::cout << "Hello from Waiter " << Name << " " << Sur_name << std::endl;
    std::cout << "Hello from Waiter.Date of birth " << day << " " << month << " " << year << std::endl;
    std::cout << "Hello from Waiter.Category " << category << std::endl;
}
TypeOfDish::TypeOfDish() {
    typeDish = "";
}
TypeOfDish::TypeOfDish(std::string type) {
    this->typeDish = type;
}
void TypeOfDish::show() {
    std::cout << "The type of dish is " << typeDish << std::endl;
}
Course::Course() : TypeOfDish() {
    price = 12.32;
    NameofCourse = "Borsh";
}
Course::Course(std::string name, double pri, std::string type) : TypeOfDish(type) {
    this->NameofCourse = name;
    this->price = pri;
}
void Course::show() {
    std::cout << typeDish << ":" << NameofCourse << std::endl;
}
Order::Order() : Data(), Course(), Waiter() {
   std::cout << ".";
}
void Order::show() {
    std::cout << "Hello from Order " << Name << " " << Sur_name << std::endl;
    std::cout << "Hello from Order.Date of birth " << Waiter::day << " " << Waiter::month << " " << Waiter::year << std::endl;
    std::cout << "Hello from Order.Category " << category << std::endl;
    std::cout << "Hello from Order.Date of order " << day << " " << month << " " << year << std::endl;
    std::cout << "Hello from Order" << typeDish << ":" << NameofCourse << std::endl;
}

我有 3 个基本课程 - 服务员、课程和数据以及衍生订单。我想在屏幕上查看服务员、课程和数据中的所有字段。我的意思是名字,姓氏,类别,服务员的出生日期,课程中的所有字段以及最重要的订单数据。当我运行程序时,一切都很好,但它没有按照我想要的方式工作。出生日期和订购日期的数据相同。

所以,我想知道如何解决这个问题。

任何解决方案将不胜感激。谢谢。

由于您使用的是虚拟继承,因此无论您是否使用范围解析,Order 实例中都只有一组日-月-年数据。

所以在 Order::show 中,"Waiter::d ay"和"day"指的是同一条数据。

如果要维护此结构,请删除虚拟继承,Order 的每个实例将包含单独的数据实例;但请确保还使用范围解析来区分它们。

但是,我认为您的继承计划并不那么好。我不认为订单"是"课程或服务员;这应该是一个"HASA"关系,即订单应该包含这些类型的成员变量,而不是从课程或服务员继承。