注册 登录
编程论坛 C++教室

继承问题,为什么这个程序运行不了?提示没有合适的默认构造函数可用

jioper 发布于 2017-04-14 14:25, 2956 次点击
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
   Student(int n, string nam )
   {
      num=n;
      name=nam;
   }
   void display( )
   {
      cout<<"num:"<<num<<endl<<"name:"<<name;
   }
protected:
   int num;
   string name;
};
class Student1: public Student
{
public:
   Student1(int n1, string nam1,int a):Student(n1,nam1)
   {
      age=a;
   }
   void show( )
   {
      cout<<"This student is:"<<endl;
      display();
      cout<<"age: "<<age<<endl;
   }
private:
   Student monitor;
   int age;
};
int main( )
{
   Student1 stud1(20120107, "Johnson", 20);
   stud1.show( );
   return 0;
}
7 回复
#2
rjsp2017-04-14 16:23
你说 monitor 怎么构造?
#3
jioper2017-04-14 19:51
回复 2楼 rjsp
最终也不输出 monitor,题目最后只给了一个学号,一个名字,一个年龄然后把这三个输出。但是却要建立一个monitor,所以我也很奇怪这个怎么弄。。。
#4
jioper2017-04-14 19:54
回复 3楼 jioper
您能写一下这块的代码吗?我这个基础不是太扎实
#5
rjsp2017-04-14 22:47
回复 4楼 jioper
把题目提出来看看
从monitor这个词上看,不应该是student
#6
yangfrancis2017-04-15 13:49
猜monitor不是student的实例,是个子类
#7
jioper2017-04-15 18:58
回复 5楼 rjsp
6楼说的是,这个montior是个子类,要和age一起成为student1的私有成员
#8
luoj752017-04-19 19:42
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
   Student(int n, string nam )
   {
      num=n;
      name=nam;
   }
   void display( )
   {
      cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
   }
protected:
   int num;
   string name;
};
class Student1: public Student
{
public:
   Student1(int n1, string nam1,int a):Student(n1,nam1)
   {
      age=a;
   }
   void show( )
   {
      cout<<"This student is:"<<endl;
      display();
      cout<<"age: "<<age<<endl;
   }
private:
   int age;
};
int main( )
{
   Student1 stud1(20120107, "Johnson", 20);
   stud1.show( );
   return 0;
}
1