派生类中的运算符重载无法工作... [英] operator overloading in the derived class could not work...

查看:85
本文介绍了派生类中的运算符重载无法工作...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


在刷新我对C ++的理解的同时,我发现过载的

运算符无法正常工作。为什么我的以下代码会继续打印

学生b的数据是一个人的?我输了什么吗?感谢

你的帮助!


人a(John,30);

学生b(玛丽 ;,20," 93406001");

person * p [2] = {& a,& b};

for(int j = 0; j< 2; j ++)

cout<< (* p [j] +3)<< endl;


下面列出了人和学生的定义。

------------------ --------------

班级人士

{

私人:

字符串名称;

int age;

public:

person(string na,int ag):name(na),age(ag ){}

person operator +(int n)const

{

返回人(姓名,年龄+ n);

}

虚拟空白打印(ostream& os)const

{

os<<名称<< " " <<年龄;

}

};


ostream&运营商LT;< (ostream& os,const person& aP)

{

aP.print(os);

返回os;

}

-----------------------------------

班级学生:公共人员

{

私人:

string studentNo;

public :

学生(字符串na,int ag,字符串sno):person(na,ag),studentNo(sno)

{}

void print(ostream& os)const

{

os<< studentNo<< " " ;;

person :: print(os);

}

};

解决方案

< wf ***** @ libra.seed.net.tw> schrieb im Newsbeitrag新闻:11 ********************* @ e56g2000cwe.googlegro ups.com ...

你好,
操作符无法正常工作。为什么我的以下代码会继续打印学生b的数据作为一个人的?我输了什么吗?感谢您的帮助!

人a(John,30);
学生b(Mary,20," 93406001"); cout< (* p [j] +3)<< endl;



....


程序打印什么,你期待什么?


Heinz


显然我希望看到相应的打印功能是

。由于p [0]指的是人物对象,而p [1],则是学生

对象。结果应该合理地如下:

John 33

93406001 Mary 23


然而,它非常沮丧。打印出来的结果是

John 33

Mary 23


所以我的问题是如何制作运营商+ "像我想要的那样工作?


< wf ***** @ libra.seed.net.tw> schrieb im Newsbeitrag新闻:11 ********************** @ u72g2000cwu.googlegr oups.com ...

显然我想要发布相应的打印功能。由于p [0]指的是人物,而p [1]是指学生的对象。结果应该合理地如下:
John 33
93406001 Mary 23

然而,它非常沮丧。打印出来的结果是John 33
Mary 23

所以我的问题是如何制作运营商+。像我想要的那样工作?


[来自原始帖子的代码]人a(John,30);
学生b(Mary,20,93406001); < (* j = 0; j <2; j ++)
cout<< (* p [j] +3)<< endl;

下面列出了人和学生的定义。
-------------------------- ------
上课的人
{
私人:
字符串名称;
int age;
公众:
人(字符串na,int ag):name(na),age(ag){}
person operator +(int n)const
{
返回者(姓名,年龄+ n);
}




operator +创建一个person类型的新对象。它被调用的对象是派生类型并不重要。您的代码告诉编译器返回一个人,而不是学生,这样您就可以获得所要求的内容。如果你希望operator +对于学生类型的对象表现不同,你也必须为班级学生实现它。没有它,编译器不会知道如何向学生添加int。它只知道学生也是人,以及如何为一个人添加一个int,它知道这样一个操作的结果是一个人,而不是一个学生。实际上,在编写班级人员时,它甚至不知道有学生这样的事情。


HTH

Heinz


Hello,

while refreshing my understanding of C++, I found the overloaded
operators could not work properly. Why my following codes keep printing
the student b''s data as a person''s? Did I lost anything? Thanks for
your help!

person a("John", 30);
student b("Mary", 20, "93406001");
person *p[2] = {&a, &b};
for (int j=0; j<2; j++)
cout << (*p[j]+3) << endl;

The definitions of person and student are listed below.
--------------------------------
class person
{
private:
string name;
int age;
public:
person(string na, int ag):name(na), age(ag) {}
person operator+(int n) const
{
return person(name, age+n);
}
virtual void print(ostream& os) const
{
os << name << " " << age;
}
};

ostream& operator<< (ostream& os, const person &aP)
{
aP.print(os);
return os;
}
-----------------------------------
class student:public person
{
private:
string studentNo;
public:
student(string na, int ag, string sno):person(na, ag), studentNo(sno)
{}
void print(ostream &os) const
{
os << studentNo << " ";
person::print(os);
}
};

解决方案

<wf*****@libra.seed.net.tw> schrieb im Newsbeitrag news:11*********************@e56g2000cwe.googlegro ups.com...

Hello,

while refreshing my understanding of C++, I found the overloaded
operators could not work properly. Why my following codes keep printing
the student b''s data as a person''s? Did I lost anything? Thanks for
your help!

person a("John", 30);
student b("Mary", 20, "93406001");
person *p[2] = {&a, &b};
for (int j=0; j<2; j++)
cout << (*p[j]+3) << endl;


....

What does the program print and what do you expect?

Heinz


Obviously I would like to see the corresponding print function be
issued. Since p[0] refers to a person object, while p[1], a student
object. The result should reasonably be as follows:
John 33
93406001 Mary 23

However, it is quite frustrated. The result it printed out is
John 33
Mary 23

So my question is how to make "the operator+" work as what I want?


<wf*****@libra.seed.net.tw> schrieb im Newsbeitrag news:11**********************@u72g2000cwu.googlegr oups.com...

Obviously I would like to see the corresponding print function be
issued. Since p[0] refers to a person object, while p[1], a student
object. The result should reasonably be as follows:
John 33
93406001 Mary 23

However, it is quite frustrated. The result it printed out is
John 33
Mary 23

So my question is how to make "the operator+" work as what I want?
[Code from original post] person a("John", 30);
student b("Mary", 20, "93406001");
person *p[2] = {&a, &b};
for (int j=0; j<2; j++)
cout << (*p[j]+3) << endl;

The definitions of person and student are listed below.
--------------------------------
class person
{
private:
string name;
int age;
public:
person(string na, int ag):name(na), age(ag) {}
person operator+(int n) const
{
return person(name, age+n);
}



operator+ creates a new object of type person. It doesn''t matter that the object for which it has been called is of a derived type. Your code tells the compiler to return a person, not a student, so you get what you asked for. If you want operator+ to behave differently for objects of type student, you have to implement it for class student, too. Without it the compiler does not lnow how to add an int to a student. It only knows that students are persons, too, and how to add an int to a person, and it knows that the result of such an operation is a person, but not a student. Actually, when compiling class person, it doesn''t even know that there is such a thing as a student.

HTH
Heinz


这篇关于派生类中的运算符重载无法工作...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆