简体   繁体   中英

multiple inheritance issue c++

I have a trouble with multiple 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;
}

I have 3 basic classes - Waiter, Course and Data and derivative Order. I want to see all fields on screen from Waiter, Course and Data. I mean Name, Surname, category, date of birth from Waiter, all fields from Course too and the most important the data of order. When i run the program everything is ok, but it doesn't work the way i want. The same data for date of birth and for date of order.

So, i'd like to know how can i solve the problem.

Any solutions would be much appreciated. Thanks.

Because you are using virtual inheritance, you only have one set of day-month-year data in an instance of Order, regardless of whether you use scope resolution or not.

So in Order::show, "Waiter::day" and "day" are referring to the same piece of data.

If you want to maintain this structure, remove the virtual inheritance and each instance of Order will contain separate instances of Data; but make sure you also use scope resolution to distinguish between them.

However, I think that your inheritance scheme is not that good. I don't think that an Order "isa" Course or a Waiter; this should be a "hasa" relationship, ie instead of inheriting from Course or Waiter, Order should contain member variables of those types instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM