首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

类有关问题。多谢

2012-05-21 
类问题。。。谢谢#includeiostreamusing namespace stdclass Student{public:Student()Student(double r)

类问题。。。谢谢
#include<iostream>
using namespace std;

class Student
{
public:
Student();
Student(double r);
Student(double r, double i);
Student(const Student &a);
~Student();
operator double();
void Display();
protected:
double m_r;
double m_i;
};

Student::Student()
{
m_r=0;
m_i=0;
}

Student::Student(double r)
{
m_r=r;
m_i=0;
}

Student::Student(double r, double i)
{
m_r=r;
m_i=i;
}

Student::Student(const Student &a)
{
m_r=a.m_r;
m_i=a.m_i;
}

Student::~Student()
{
cout<<"jiao yang is a good boy"<<endl;
}

Student::operator double()
{
return m_r;
}

void Student::Display()
{
cout<<m_r<<m_i<<endl;
}






class Teacher:public Student
{
public:
Teacher();
Teacher(double tr);
Teacher(double r1, double i1,double r2,double i2, double r);
Teacher(const Teacher &a);
~Teacher();
operator double();
void Display1();
private:
double num;
Student monitor;
};

Teacher::Teacher():Student()
{
num=0;
}

Teacher::Teacher(double tr)
{
num=tr;
}

Teacher::Teacher(double r1, double i1,double r2,double i2, double r):Student(r1,i1),monitor(r2,i2)
{
num=r;
}

Teacher::Teacher(const Teacher &a)
{
num=a.num;
}

Teacher::~Teacher()
{
cout<<"jiao yang is a good boy"<<endl;
}

Teacher::operator double()
{
return num;
}

void Teacher::Display1()
{
Display();
monitor.Display();
cout<<num<<endl;
}
int main()
{
Teacher a(1,2,3,4,5);
a.Display1();/*这里输出1 2 3 4 5*/
a=a+1.2;/*这里输出0 0 0 0 6.2。。。。我觉得应该输出1 2 3 4 6.2。。。为什么会这样。。。怎么改,,,求大神。。*/
a.Display1();
double r;
r=1.2+a;
cout<<" dsdsf"<<r<<endl;

return 0;
}

问题在注释部分。。。谢谢。。。。

[解决办法]
你为什么要用Teacher类继承Student类??老师也是一个学生??还要重载double做什么??
[解决办法]

C/C++ code
加一个 operator+重载:class Teacher:public Student{public:Teacher();Teacher(double tr);Teacher(double r1, double i1,double r2,double i2, double r);Teacher(const Teacher &a);~Teacher();operator double();/////////重载Teachar operator+(double val){Teachar t(*this);t.num+=val;return t;}void Display1();private:double num;Student monitor;}; 

热点排行