关于C++继承与组合如何修改对象成员的数据。。。
#include"iostream"#include"string"usingnamespacestd;classteacher{public:teacher(intn,...
#include "iostream"
#include "string"
using namespace std;
class teacher
{
public:
teacher(int n,string nam,char s):num(n),name(nam),sex(s){}
show();
private:
int num;
string name;
char sex;
};
teacher::show()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
class Birthdate
{
public:
Birthdate(int y,int m,int d):year(y),month(m),day(d){}
output();
change();
private:
int year;
int month;
int day;
};
Birthdate::output()
{
cout<<"birthday is:"<<year<<" "<<month<<" "<<day<<endl;
}
Birthdate::change()
{
month=3;
day=12;
}
class professor:public teacher
{
public:
professor(int n,string nam,char s,Birthdate b):teacher(n,nam,s),birthday(b){}
output(Birthdate *a);
private:
Birthdate birthday;
};
professor::output(Birthdate *a)
{
a->change();
show();
birthday.output();
}
void main()
{
Birthdate b(1995,4,21);
professor prof1(1001,"wenshuang",'f',b);
Birthdate *str=&b;
prof1.output(str);
}
这就是全部的代码了,在VC++6.0编译通过,但是我试图想修改对象成员birthday的月和日这两个成员,但是编译显示的仍然木有改,还是4 21.。。请问是为什么啊,如何改呢?另外,我试过把指针换成引用,但还是不行诶。。。。求大神~~ 展开
#include "string"
using namespace std;
class teacher
{
public:
teacher(int n,string nam,char s):num(n),name(nam),sex(s){}
show();
private:
int num;
string name;
char sex;
};
teacher::show()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
class Birthdate
{
public:
Birthdate(int y,int m,int d):year(y),month(m),day(d){}
output();
change();
private:
int year;
int month;
int day;
};
Birthdate::output()
{
cout<<"birthday is:"<<year<<" "<<month<<" "<<day<<endl;
}
Birthdate::change()
{
month=3;
day=12;
}
class professor:public teacher
{
public:
professor(int n,string nam,char s,Birthdate b):teacher(n,nam,s),birthday(b){}
output(Birthdate *a);
private:
Birthdate birthday;
};
professor::output(Birthdate *a)
{
a->change();
show();
birthday.output();
}
void main()
{
Birthdate b(1995,4,21);
professor prof1(1001,"wenshuang",'f',b);
Birthdate *str=&b;
prof1.output(str);
}
这就是全部的代码了,在VC++6.0编译通过,但是我试图想修改对象成员birthday的月和日这两个成员,但是编译显示的仍然木有改,还是4 21.。。请问是为什么啊,如何改呢?另外,我试过把指针换成引用,但还是不行诶。。。。求大神~~ 展开
展开全部
professor::output(Birthdate *a)
{
a->change();
show();
birthday.output();
}
这里你既然有Birthdate birthday;的类成员,你修改了a->change();但是没有修改Birthdate birthday;这个成员啊,你用这个成员Birthdate birthday来输出当然是显示的 4 21.
output();
professor::output()
{
birthday.hange();
show();
birthday.output();
}
那个参数a根本就没有必要出现。
{
a->change();
show();
birthday.output();
}
这里你既然有Birthdate birthday;的类成员,你修改了a->change();但是没有修改Birthdate birthday;这个成员啊,你用这个成员Birthdate birthday来输出当然是显示的 4 21.
output();
professor::output()
{
birthday.hange();
show();
birthday.output();
}
那个参数a根本就没有必要出现。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void professor::output(Birthdate *a)
{
a->change();
show();
a->output(); // 这里应该改成a,不是birthday
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询