#include<iostream>
#include<string>
using namespace std;
class student
{public:
student(int n,string nam)
{num=n;
name=nam;
}
void show()
{cout<<"num"<<num<<" "<<name<<endl;}
protected:
int num;
string name;
};
class student1:public student
{public:
student1(int n,sting nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)
{age=a;
adr=ad;
}
void display()
{cout<<"the student is :"<<show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.display();
}
private:
student monitor;
int age;
string adr;
};
int main()
{student1 stu1(10010,"wang-li",10010,"li-sun",19," 115 beijing road ,shanghai");
stu1.display();
stu1.display_monitor();
return 0;
}
有这么些错误:
1.student1(int n,sting nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)第二个参数string写错了
2.cout<<show()不接受这么写法,删除本行或者你直接show()方法里面写显示数据
3.student作为基类,没有display()方法,你使用了基类去调用派生类的方法,这是错误的
以下程序通过编译。另外,纯手工打造的回答,LZ务必选为最佳,否则让热心的答题人心寒啊,谢谢!
#include<iostream>
#include<string>
using namespace std;
class student
{public:
student(int n,string nam)
{
num=n;
name=nam;
}
void show()
{
cout<<"num"<<num<<" "<<name<<endl;
}
protected:
int num;
string name;
};
class student1:public student
{public:
student1(int n,string nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)
{
age=a;
adr=ad;
}
void display()
{
//cout<<"the student is :"<<show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
//monitor.display();
}
private:
student monitor;
int age;
string adr;
};
int main()
{
student1 stu1(10010,"wang-li",10010,"li-sun",19," 115 beijing road ,shanghai");
stu1.display();
stu1.display_monitor();
return 0;
}
#include<string>
using namespace std;
class student
{public:
student(int n,string nam)
{num=n;
name=nam;
}
void show()
{cout<<"num"<<num<<" "<<name<<endl;}
protected:
int num;
string name;
};
class student1:public student
{public:
student1(int n,sting nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)
{age=a;
adr=ad;
}
void display()
{cout<<"the student is :"<<show();//show返回void,不能输出到cout
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.display();//monitor是student类型,student类没有dispaly()成员函数
}
private:
student monitor;
int age;
string adr;
};
int main()
{student1 stu1(10010,"wang-li",10010,"li-sun",19," 115 beijing road ,shanghai");
stu1.display();
stu1.display_monitor();
return 0;
}
void display()
{cout<<"the student is :"<<show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.display();
}
修改结果:
void display()
{cout<<"the student is :"<<endl;
this.show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.show();
}