#include<iostream>
#include<cstring>
using namespace std;
class CStudent
{
public:
CStudent(char *n, int a);
~CStudent();
friend class CTeacher;
private:
char *name;
int age;
};
CStudent::CStudent(char *n, int a) : age(a)
{
int nLen = strlen(n);
name = new char[nLen+1];
strcpy(name,n);
name[nLen] = “”\0″”;
}
CStudent::~CStudent()
{
delete[] name;
}
class CTeacher
{
public:
CTeacher(char *tn, int ta);
~CTeacher();
void SetStuAge(int a);
private:
char *name;
int age;
CStudent stu;
};
CTeacher::CTeacher(char *tn, int ta) :age(ta)
{
int nLen = strlen(tn);
name = new char[nLen+1];
strcpy(name,tn);
name[nLen] = “”\0″”;
}
CTeacher::~CTeacher()
{
delete[] name;
}
void CTeacher::SetStuAge(int a)
{
stu.age = a;
}
int main()
{
CStudent stu(“张三”,25);
CTeacher tea(“李四”,26);
return 0;
}
#include<cstring>
using namespace std;
class CStudent
{
public:
CStudent(char *n, int a);
~CStudent();
friend class CTeacher;
private:
char *name;
int age;
};
CStudent::CStudent(char *n, int a) : age(a)
{
int nLen = strlen(n);
name = new char[nLen+1];
strcpy(name,n);
name[nLen] = “”\0″”;
}
CStudent::~CStudent()
{
delete[] name;
}
class CTeacher
{
public:
CTeacher(char *tn, int ta);
~CTeacher();
void SetStuAge(int a);
private:
char *name;
int age;
CStudent stu;
};
CTeacher::CTeacher(char *tn, int ta) :age(ta)
{
int nLen = strlen(tn);
name = new char[nLen+1];
strcpy(name,tn);
name[nLen] = “”\0″”;
}
CTeacher::~CTeacher()
{
delete[] name;
}
void CTeacher::SetStuAge(int a)
{
stu.age = a;
}
int main()
{
CStudent stu(“张三”,25);
CTeacher tea(“李四”,26);
return 0;
}
解决方案:10分
不知道你的实际功能要干嘛。编译方面:你的CTeacher类中有CStudent对象,在构造的时候会调用CStudent的默认构造函数,但你的CStudent类并没有提供默认的构造函数。另外name的类型为什么不用string呢
解决方案:10分
CStudent 没有不带参数的构造函数,而你CTeacher中定义了一个对象CStudent stu; 没有用构造函数列表进行初始,所以出错