C ++ - (如果语句)总是会显示

时间:2014-10-05 06:50:54

标签: c++ inheritance if-statement

所以我有我的这个基类。

class Info{
private:
    string name, sex;
    int year, month, day, age;

public:

    void setInfo(){

    string n, s;

        cout<<"Enter your full name: ";
        cin>>n;
        cout<<"Gender: ";
        cin>>s;

        name=n;
        sex=s;

        cout<<endl;
    }

    void setBirthdate(){

        int y, m, d, a;

        cout<<"Birthdate in numerical type"<<endl;
        cout<<"Year: ";
        cin>>y;
        cout<<"Month: ";
        cin>>m;
        cout<<"Day: ";
        cin>>d;

        a=2014-y;

        year=y;
        month=m;
        day=d;
        age=a;

        cout<<endl;
    }

    int getYear(){
        return year;        
    }

    int getMon(){
        return month;
    }

    int getDay(){
        return day;
    }

    int getAge(){
        return age;
    }

};

和派生类

class Fortunes:public Info{

private:
    string zodiacs;

public:

Info fo1;

    string getZodiac(){

        if((fo1.getMon()<=4) && (fo1.getMon()>=3))
        {
        cout<<"Aries";
        }

        else
            cout<<"aww";

    }




};

主要班级

int main(){
Fortunes f;

f.setInfo();
f.setBirthdate();
f.getZodiac();

cout<<endl;

system("pause>nul");
}

我想制作一个程序,根据您输入的信息告诉您的星座。 所以我只测试了我的这个小代码,总是会显示出来。 我猜我的经营者错了?请帮帮我:(

1 个答案:

答案 0 :(得分:1)

您创建了两个Info对象实例,一个作为f的子对象(声明为Fortunes f;),另一个声明为Info fo1;在Fortune对象中,但fo1未初始化。更正后的计划如下:

    class Fortunes:public Info{
    public:
        string getZodiac(){

            if((this->getMon()<=4) && (this->getMon()>=3))
            {
            cout<<"Aries";
            }

            else
                cout<<"aww";

        }

};
相关问题