Code Reduction for Multiple Inheritance

I wrote a small little program in which you type a boy or a girl, and prints an expression. My main question is that from my code there is an easier way to write for women next to copy and paste from the base class. Here is my code

#include <iostream>
#include <string>


class Man{
protected:
    std::string name;
public:
    void getInfo(std::string hName){
        name = hName;
    }
    void showInfo(){
        std::cout << "Your name is: " << name << std::endl;
        std::cout << "And you are a MAN" << std::endl;
    }
};

class Women{ //is there an easier way to write this         
protected:
    std::string fem_name;
public:
    void getfemInfo(std::string fhName){
        fem_name = fhName;
    }
    void showfemaleInfo(){
        std::cout << "Your name is: " << fem_name << std::endl;
        std::cout << "And you are a Women" << std::endl;    
    }
};

class Human:public Man, public Women{
public:
    Human(){}
};

int main(){
    //local variables
    std::string choice;
    std::string tName;
    //declaring objects 
    Human person;
    //user interface
    std::cout << "Please enter you name: ";
    std::cin >> tName;
    std::cout << "Are you a [boy/girl]: ";
    std::cin >> choice;
    //if handler
    if (choice == "boy"){
        person.getInfo(tName);
        person.showInfo();
    }else if(choice == "girl"){
        person.getfemInfo(tName);
        person.showfemaleInfo();
    }
    system("pause");
    return 0;
}

When I try to get a class Womanfrom a class Man, it makes person.getInfo(tName)it person.showInfo()ambiguous. Why is this? And how can I make this code smaller (for women).

+4
source share
2 answers

- IS-A. , , " , . . " ". (, , ) .

- . ( , ). , Human . - :

class Human
{
protected:
  std::string name;

  virtual std::string getGenderString() const = 0;

public:
  virtual ~Human() {}  //dtor must be virtual to be usable as a polymorphic base class

  void getInfo(std::string hName)
  { name = hName; }

  void showInfo() const
  {
    std::cout << "Your name is: " << name << '\n';
    std::cout << "And you are a " << getGenderString() << std::endl;
  }
};


class Man : public Human
{
protected:
  virtual std::string getGenderString() const
  { return "MAN"; }
};


class Woman : public Human
{
protected:
  virtual std::string getGenderString() const
  { return "WOMAN"; }
};


int main(){
    //local variables
    std::string choice;
    std::string tName;
    //declaring objects 
    Human *person = NULL;
    //user interface
    std::cout << "Please enter you name: ";
    std::cin >> tName;
    std::cout << "Are you a [boy/girl]: ";
    std::cin >> choice;
    //if handler
    if (choice == "boy"){
        person = new Man();
    }else if(choice == "girl"){
        person = new Woman();
    }
    person->getInfo(tName);
    person->showInfo();
    system("pause");
    delete person;
    return 0;
}

( ) . , .

, , ​​ , . , ++ 11, . () - ++.

+3

, . : , , "/", : : - . .

, , , , , . , :

class Human
{
  std::string name;
  virtual std::string GetGender() const = 0;
public:
    // virtual d-tor

    Human(const std::string& humanName)
      : name(humanName) {}

    void showInfo() const 
    {
        std::cout << "Your name is: " << name << std::endl;
        std::cout << "And you are a " << GetGender() << std::endl;
    }
};

class Man : public Human{
  /*virtual*/ std::string GetGender() const { return "MAN"; }
public:
  Man(const std::string& name)
    : Human(name) {}
};

class Woman : public Human{
  /*virtual*/ std::string GetGender() const { return "WOMAN"; }
public:
  Woman(const std::string& name)
    : Human(name) {}
};

, :

humanObject->showInfo();
0

Source: https://habr.com/ru/post/1525971/


All Articles