多继承问题c ++

时间:2017-10-21 17:21:40

标签: c++ inheritance

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

#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个基本课程 - 服务员,课程和数据以及衍生订单。 我希望看到来自Waiter,Course和Data的屏幕上的所有字段。 我的意思是名字,姓氏,类别,服务员的出生日期,课程中的所有字段以及最重要的订单数据。 当我运行该程序时一切正常,但它并没有按照我想要的方式工作。 出生日期和订单日期的相同数据。

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

任何解决方案都将非常感激。感谢。

1 个答案:

答案 0 :(得分:1)

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

所以在Order :: show,&#34; Waiter :: day&#34;和&#34; day&#34;指的是同一条数据。

如果要维护此结构,请删除虚拟继承,Order的每个实例都将包含单独的Data实例;但请确保您也使用范围分辨率来区分它们。

但是,我认为你的继承方案不是那么好。我不认为订单&#34; isa&#34;课程或服务员;这应该是一个&#34; hasa&#34;关系,即不是继承自课程或服务员,而是应该包含这些类型的成员变量。