c++:在类中定义了一个私有成员指针,在进行类的初始化的时候使它指向一个字符串常量?
#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;
}
错误多多,请大神们指正
#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;
}
错误多多,请大神们指正

...全文
相关推荐
C++程序设计试练习
C++ 程序设计练习题目 一、选择题 1、在int a=3,*p=&a;中,*p的值是( )。 A)变量a的地址值 B)无意义 C)变量p的地址值 D) 3 2、在int a[5]={1,3,5};中,数组元素a[1]的值是( )。 A) 1; B) 0; C)3; D)2 3、( )不是构造函数的特征。 A)构造函数的函数名与类名相同 B)构造函数可以重载 C)构造函数可以设置缺省参数 D)构造函数必须指定类型说明
C++ 300题 必会
C++面试 300题必会,总结整理 谢谢
C++随手笔记C++随手笔记
C++笔记,对新手会比较有用,比较简单的,适合入门选手
C++基础知识全面汇总
个人认为C++注意事项,把C++书籍上的个人认为需注意的语句摘录
C++定义一个MyString类,实现两个字符串相加
定义一个MyString类,实现两个字符串相加。要求定义类的数据成员为字符指针,实现深拷贝函数。 1.理解构造函数与析构函数的含义及特点。 2.掌握构造函数的几种类型。 3.掌握构造函数与析构函数的执行顺序。 4.掌握在继承机制中派生类的构造函数和析构函数的执行顺序。


