C++,3道题,请高手帮忙

来源:百度知道 编辑:UC知道 时间:2024/07/03 09:27:31
1.定义一个学生类
属性:学号,姓名,年龄,......
成员函数。
2.定义学生类中的成员函数,构造函数(无参和带参)
解析函数,普通成员函数(至少两个)
3.编写一个main()函数,定义一个学生类的对象,
并调用你定义的成员函数

#include <iostream>
using namespace std;

class CStudent
{
public:
CStudent();
CStudent(int nID, char* szName, int nAge)
: m_nID(nID), m_nAge(nAge), m_szName(NULL)
{
m_szName = new char[strlen(szName) + 1];
strcpy(m_szName, szName);
}
virtual ~CStudent()
{
if (m_szName)
{
delete []m_szName;
m_szName = NULL;
}
}

public:
inline int GetID()
{ return m_nID; }

inline int GetAge()
{ return m_nAge; }

inline operator char* ()
{ return m_szName; }

private:
int m_nID;
int m_nAge;
char* m_szName;

};

void main()
{
CStudent stu(1, "小明", 18);
cout << "学号\t姓名\t年龄\n" << stu.GetID() << "\t" << stu << "\t" << stu.GetAge() <<endl;

syste