如果一个基类的构造函数带有参数,那么如何编写该基类的派生类呢?
- #include<tchar.h>
-
#include<iostream>
-
using namespace std;
-
-
class Person
-
{
-
public:
-
Person(int nAge)
-
{
-
m_nAge = nAge;
-
}
-
-
protected:
-
int m_nAge;
-
};
-
-
class Men: public Person
-
{
-
public:
-
Men(int nMenAge, int nMenWeight):Person(nMenAge)
-
{
-
m_nWeight = nMenWeight;
-
}
-
-
void Print()
-
{
-
cout << _T("年龄为") << m_nAge << endl;
-
cout << _T("体重为") << m_nWeight << endl;
-
}
-
-
private:
-
int m_nWeight;
-
};
-
-
int _tmain(int argc, TCHAR argv[], TCHAR envp[])
-
{
-
Men Mike(21, 150);
-
Mike.Print();
-
return 0;
-
}
上述代码的执行结果如下:
上述代码中,我们可以看到,Person类的构造函数是带有参数的,Men类从Person类
共有派生,这就需要在Men类的构造函数名后面显示的调用Person类的构造函数。
阅读(4239) | 评论(0) | 转发(0) |