大小不定的类的对象,构造函数的初始化

#include<iostream> //构造函数,如果初始值地址空间是变化的大小的话,则要显式的定义构造函数,并且应用。名字类型。
#include<string.h> //注意区分string,string.h,cstring三个的不同。
using namespace std;

class student //counter 计数类
{



private:
    char* name;//姓名


    int score;//分数
public:
    student(char*,int);
    ~student(void);
    void show(void);

};

student::student(char* a,int i)
{
    name=new char[strlen(a)+1];             //注意要用string字符串进行复制和计算
    strcpy(name,a);
    score=i;

    cout<<endl<<"the object is initialized!";
}

student::~student()
{
    delete name;

    cout<<endl<<"the object has been destroyed!";
}

void student::show(void)
{
    cout<<endl<<"the name:"<<name<<"          "<<"the score= "<<score;


}


int main(void)
{
    student num1("zhang ming",98);
    num1.show();

return 0;


}

猜你喜欢

转载自blog.csdn.net/gaocui883/article/details/88353028