c++:在类中定义了一个私有成员指针,在进行类的初始化的时候使它指向一个字符串常量?

流光抛却人 流光抛却人 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include<iostream>
#include<string.h>
using namespace std;
class person
{
private:
char *name;
public:
person(const char *name)
{
name=new char;
strcpy(this->name, name);
}
~person( )
{
delete name;
cout<<"person::~person( ) is called"<<endl;
}
void showname( )
{
cout<<*name;
}
};
class date
{
private:
char *birth;
public:
date( )
{

}
date(const char *birth)
{
birth=new char;
strcpy(this->birth, birth);
}
~date( )
{
delete birth;
cout<<"date::~date( ) is called"<<endl;
}
void showbirth( )
{
cout<<*birth;
}
};

class student: public person
{
private:
date birthday;
public:
student(char *name, date birth):person(name)
{
birthday=birth;
}
~student( )
{
cout<<"student::~student( ) is called"<<endl;
}
void show( )
{
cout<<"student's name: ";
person::showname( );
cout<<'\t'<<"student's birth: ";
birthday.showbirth( );
}
};
class professor: public person
{
private:
date birthday;
public:
professor(char *name, date birth):person(name)
{
birthday=birth;
}
~professor( )
{
cout<<"professor::~professor( ) is called"<<endl;
}
void show( )
{
cout<<"professor's name: ";
person::showname( );
cout<<'\t'<<"professor's birth: ";
birthday.showbirth( );
}
};
int main( )
{
date sd("1993"), pd("1948");
student s("wangyu", sd);
professor p("zhaonan", pd);
s.show( );
p.show( );
return 0;
}

错误多多,请大神们指正
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
流光抛却人
C++ 语言 65,212 成员 250,513 内容