Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
所以我有这个基础班。

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");
}


我想制作一个程序,根据您输入的信息告诉您的黄道十二宫。
所以我只是测试了我的这个小代码,否则总是显示出来。
我猜我的操作员错了吗?请帮我 :(

最佳答案

您创建了两个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";

        }

};

关于c++ - C++-(如果语句)其他总是显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26200335/

10-10 03:08