C++中子类构造函数中怎么调用父类中使用this指针的有参构造函数
classHuman{stringname;intage;public:Human(stringname,intage){this->name=name;this->ag...
class Human
{
string name;
int age;
public:
Human(string name, int age)
{
this->name = name;
this->age = age;
}
};
class Student :public Human
{
Student(string name, int age, string major) // Human(name,age)这样写肯定是不对的,到底该怎么呢??
{
////这里怎么调用父类的构造方法呢???
this->major = major;
}
};
#include<iostream>
#include<string>
using namespace std;
class Human
{
string name;
int age;
public:
Human(string name, int age)
{
this->name = name;
this->age = age;
cout << "Human Constructor output";
}
void setHuman()
{
cout << "please input the name:";
cin >> name;
cout << "please input the age:";
cin >> age;
}
string getName()
{
return name;
}
int getAge()
{
return age;
}
void print()
{
cout << "name:" << name<<endl;
cout << "age:" << age<<endl;
}
};
< " major:" << major << endl;
}
}; 展开
{
string name;
int age;
public:
Human(string name, int age)
{
this->name = name;
this->age = age;
}
};
class Student :public Human
{
Student(string name, int age, string major) // Human(name,age)这样写肯定是不对的,到底该怎么呢??
{
////这里怎么调用父类的构造方法呢???
this->major = major;
}
};
#include<iostream>
#include<string>
using namespace std;
class Human
{
string name;
int age;
public:
Human(string name, int age)
{
this->name = name;
this->age = age;
cout << "Human Constructor output";
}
void setHuman()
{
cout << "please input the name:";
cin >> name;
cout << "please input the age:";
cin >> age;
}
string getName()
{
return name;
}
int getAge()
{
return age;
}
void print()
{
cout << "name:" << name<<endl;
cout << "age:" << age<<endl;
}
};
< " major:" << major << endl;
}
}; 展开
推荐于2016-10-23
展开全部
Student(string name, int age, string major) : Human(name,age) // 为什么觉得这样写不对
{
this->major = major;
}
{
this->major = major;
}
更多追问追答
追问
因为使用Human(name,age)的意思是给Student对象的名字和年龄赋值,即name=name,age=age
追答
所以你觉得哪里不对……
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询