简体   繁体   English

C ++中的继承(父类和子类)

[英]Inheritance in C++ (Parent and Child class)

I have a parent class called Athlete and there is a child class called TennisPlayer. 我有一个名为“运动员”的家长班,还有一个名为“ TennisPlayer”的孩子班。 There are two attributes in my parent class which are name and annual salary. 我的父母班级有两个属性,分别是姓名和年薪。 There is also a read method which take in user input for both classes. 还有一个读取方法,可以为两个类都接受用户输入。 Here is how my parent looks like: 这是我父母的样子:

class Athlete
{
public:
    Athlete();
    Athlete(string name, float annual_salary);
    virtual void read();
    virtual void display();
    string get_name() const;
    float get_annual_salary();
    void set_name(string name);
    void set_annual_salary(float annual_salary);
private:
    string name;
    float annual_salary;
};

class TennisPlayer : public Athlete
{
public:
    TennisPlayer();
    TennisPlayer(string name, float annual_salary, int current_world_ranking);
    int get_current_world_ranking();
    void set_current_world_ranking(int current_world_ranking);
    virtual void read();
    virtual void display();
private:
    int current_world_ranking;
};

The read() method for athlete class works perfectly. 适用于运动员课程的read()方法非常有效。 But when it comes to tennisPlayer which is the child class, something went wrong, it won't take in the user input, instead it straight away ask me for the input of current world ranking. 但是当涉及子类的TennisPlayer时,出现了问题,它不会接受用户输入,而是直接要求我输入当前世界排名。 Here is my .cpp for TennisPlayer class: 这是我的Tennisclayer类的.cpp文件:

void TennisPlayer::read()
{
    cout << "Enter name for tennis player: " ;
    getline(cin, get_name());
    cout << "Enter annual salary for tennis player: ";
        cin >> get_annual_salary();
    cout << "Enter current world ranking: ";
    cin >> current_world_ranking;
}

Conctructor for tennisPlayer class: 网球建设者球员类别:

TennisPlayer::TennisPlayer(string name, float annual_salary ,int current_world_ranking)
: Athlete(name , annual_salary)
{
    this->current_world_ranking = current_world_ranking;
}

Also there's a problem when reading the annual salary for tennisPlayer. 在阅读TennisPlayer的年薪时也有问题。 I am not quite familiar with inheritance in C++. 我对C ++中的继承不是很熟悉。 Thanks in advance. 提前致谢。

At a guess I would say inheritance is not your problem, but getline . 大概我会说继承不是您的问题,而是getline Often the 'enter' character is left in the input buffer which means that the next input line seems to be skipped because it just reads this character. 通常,'enter'字符会留在输入缓冲区中,这意味着下一条输入行似乎被跳过了,因为它只是读取了该字符。 Try putting cin.get(); 尝试放入cin.get(); before your call to getline() to flush out this extra character. 在调用getline()清除此多余字符之前。

EDIT: let me refactor some of your code: 编辑:让我重构您的一些代码:

void TennisPlayer::read()
{
    string tennisName;
    float annualSalary;

    cout << "Enter name for tennis player: " ;
    cin.get();  <--read any leftover characters in the input buffer
    getline(cin, tennisName);  <--read in the value
    set_name(tennisName);   <--use parent's mutator to set the value

    cout << "Enter annual salary for tennis player: ";
    cin >> annualSalary;
    set_annual_salary(annualSalary);

}

ADDITIONAL: The child has access to public and protected aspects of the parent class. 其他:孩子可以访问父类的公共和受保护方面。 So an alternative way to do this would be to make the member variables protected so the child class(es) can access them, but they are still hidden from external classes. 因此,执行此操作的另一种方法是使成员变量受保护,以便子类可以访问它们,但它们仍对外部类隐藏。

Child classes can call public member variables and functions from the parent class without scope resolution ie no need to specify TennisPlayer::set_name , just set_name will suffice. 子类可以从父类中调用公共成员变量和函数,而无需范围解析,即无需指定TennisPlayer::set_name ,只需set_name就足够了。

Try this 尝试这个

void TennisPlayer::read()
{
    cout << "Enter name for tennis player: " ;
    string sName;
    getline(cin, sName);
    set_name( sName );

    cout << "Enter annual salary for tennis player: ";
    float salary;
    cin >> salary;
    set_annual_salary(salary);

    cout << "Enter current world ranking: ";
    int ranking;
    cin >> ranking;
    set_current_world_ranking(ranking);
}

The getter function simply return a value. getter函数只是返回一个值。 Changing this value will not do anything to the object from which the value came. 更改此值不会对产生该值的对象产生任何影响。 You need to call the setter functions in order to actually change the TennisPlayer object. 您需要调用setter函数才能实际更改TennisPlayer对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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