程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> char-一個很低級的問題,怎麼修改。

char-一個很低級的問題,怎麼修改。

編輯:編程綜合問答
一個很低級的問題,怎麼修改。

#include
#include

class CStudent
{
public:
CStudent(char *n, int a);
~CStudent();

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;
}

void main()
{
CStudent stu("張三",25);
CTeacher tea("李四",26);
}

最佳回答:


 #include <iostream.h>
#include <string.h>

class CStudent
{
public:
    CStudent(char *n, int a);
CStudent() { name=new char[100];}
    ~CStudent();
    int age;
private:
    char *name;

};
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()
{
char s1[] = "張三";
char s2[] = "李四";
    CStudent stu(s1,25);
    CTeacher tea(s2,26);
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved