#include <iostream> using namespace std; class Date { public: int year,month,day; Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day) {} }; class Cperson { private: string Name; string Num; Date brith; Cperson(const char *pName="张三", string Num="123456789123456789", int year=2000,int month=1,int day=1): Name(Name),Num(Num),brith(year,month,day) {} string GetName() { return Name; } string GetNum() { return Num; } int Getbrith() { return brith; } }; cass Cstudent: private Cperson { private: int StNum; }; class Cteacher:public Cperson { private: Date Time; }; int main() { cout << "手机编程APP"; return 0; }有什么错误
时间: 2024-01-15 16:03:26 浏览: 72
#include_iostream.h_
4星 · 用户满意度95%
在类Cperson的构造函数中,初始化成员变量时,应该使用初始化列表中的参数,而不是使用类内部定义的变量。正确的构造函数应该是:
```
Cperson(const char *pName="张三",
string Num="123456789123456789",
int year=2000,int month=1,int day=1):
Name(pName),Num(Num),brith(year,month,day)
{}
```
此外,在类Cperson的成员函数Getbrith中,返回值应该是一个Date对象,而不是int类型。应该改为:
```
Date Getbrith()
{
return brith;
}
```
最后,在类Cstudent和Cteacher中,应该声明访问权限为public,否则外部无法访问这些类的成员。修正后的代码如下:
```
#include <iostream>
using namespace std;
class Date {
public:
int year,month,day;
Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day)
{}
};
class Cperson {
public:
string Name;
string Num;
Date brith;
Cperson(const char *pName="张三",
string Num="123456789123456789",
int year=2000,int month=1,int day=1):
Name(pName),Num(Num),brith(year,month,day)
{}
string GetName()
{
return Name;
}
string GetNum()
{
return Num;
}
Date Getbrith()
{
return brith;
}
};
class Cstudent: public Cperson {
public:
int StNum;
};
class Cteacher:public Cperson {
private:
Date Time;
public:
// 省略构造函数
};
int main() {
cout << "手机编程APP";
return 0;
}
```
阅读全文