a19870502 发表于 2008-4-22 09:26

[求助]关于类嵌套

#include<iostream>
using namespace std;
class date
{
public:
        date();

        date(date &d);

        void setdate();

        void showdate();

private:
        int year,month,day;
};



date::date(){
        year=0;
        month=0;
        day=0;
}

date::date(date & d){
        year=d.year;
        month=d.month;
        day=d.day;
}

void date::setdate(){
        cout<<"依次输入年,月,日"<<endl;
    cin>>year;
        cin>>month;
        cin>>day;
       
}

void date::showdate(){
        cout<<year<<"-"<<month<<"-"<<day<<endl;
}

class personnel
{
public:
        personnel();

        personnel(personnel & p);

        void setdata_per();

        void showdata_per();
private:
        int name;
        int number;
        char sex;
        date birthday;
        int numofindentity;
};


personnel::personnel(){

        name=0;

        number=0;

        sex=0;

        numofindentity=0;


}

personnel::personnel(personnel &p){
        birthday=p.birthday;
        number=p.number;
        sex=p.sex;
        numofindentity=p.numofindentity;
}

void personnel::setdata_per(){
       
        cout<<"input name"<<endl;
        cin>>name;

        cout<<"input number"<<endl;
        cin>>number;

        cout<<"input sex"<<endl;
        cin>>sex;

        cout<<"input birthday"<<endl;
        birthday.setdate();

        cout<<"input number of indentity"<<endl;
        cin>>numofindentity;
}

void personnel::showdata_per(){
       
        cout<<name<<"        ";
       
        cout<<number<<"                ";
       
        cout<<sex<<"        ";
       
        birthday.showdate();
       
        cout<<"          ";
       
        cout<<numofindentity<<"                "<<endl;
}

int main(){
        personnel person1;

        person1.setdata_per();
        person1.showdata_per();
       
        cout<<endl;
       
        personnel person2(person1);
        person2.showdata_per();

        return 0;
}
创建完person1之后,执行setdata_per()函数,但是在输入完姓名的之后,setdata_per()中后面的语句都没有执行,这是什么问题啊?
如果觉得我讲得不清楚,请大家编译一下就知道了,麻烦了.

sunkaidong 发表于 2008-4-22 09:58

一, string name;
二,personnel::personnel():birthday(){

    name="0";

    number=0;

    sex=0;

    numofindentity=0;


}

a19870502 发表于 2008-4-22 14:50

十分感谢,问题解决了.
但是我不明白为什么会这样?是数据类型的影响吗?
麻烦了.

sunkaidong 发表于 2008-4-22 14:52

名字是字符串的....一般情况用变量之前初始化..而很大部分情况的错误是由于变量的初始化引起的

a19870502 发表于 2008-4-22 21:16

再次感谢!

页: [1]

编程论坛