c++题目,有几个错不会改 #include<iostream> #include<string>
c++题目,有几个错不会改#include<iostream>#include<string>usingnamespacestd;classCPerson{public:...
c++题目,有几个错不会改
#include<iostream>
#include<string>
using namespace std;
class CPerson
{
public:
CPerson(int a,string*ad,string n);
virtual ~CPerson();
virtual void show();
protected:
int m_age;
string *m_address;
string m_name;
};
class CStudent:public CPerson
{
public:
CStudent();
~CStudent();
void show();
protected:
string m_grade;
};
CPerson::CPerson(int a,string*ad,string n)
{
m_age=a;
m_address=ad;
strcpy(m_name,n);
}
CPerson::~CPerson()
{
cout<<"~CPerson!"<<endl;
}
void CPerson::show()
{
cout<<"姓名:"<<m_name<<" 年龄:"<<m_age<<" 住址:"<<m_address<<endl;
}
CStudent::CStudent(int a,string *ad,string n,string g):CPerson(a,ad,n)
{
strcpy(m_grade,g);
}
CStudent::~CStudent()
{
cout<<"~CStudent!"<<endl;
}
void CStudent::show()
{
cout<<"姓名:"<<m_name<<" 年龄:"<<m_age<<" 年级:"<<m_grade<<" 住址:"<<m_address<<endl;
}
int main()
{
string s="dizhi";
string *p=s;
CPerson *poCP=new CStudent(20,p,"zhangsan","dayi");
if(NULL==poCP)
{
exit(0);
}
poCP->show();
delete poCP;
cout<<"通过delete析构new运算符创建的对象结束!\n"<<endl;
CStudent oCSt(20,p,"zhangsan","dayi");
oCSt.show();
return 0;
} 展开
#include<iostream>
#include<string>
using namespace std;
class CPerson
{
public:
CPerson(int a,string*ad,string n);
virtual ~CPerson();
virtual void show();
protected:
int m_age;
string *m_address;
string m_name;
};
class CStudent:public CPerson
{
public:
CStudent();
~CStudent();
void show();
protected:
string m_grade;
};
CPerson::CPerson(int a,string*ad,string n)
{
m_age=a;
m_address=ad;
strcpy(m_name,n);
}
CPerson::~CPerson()
{
cout<<"~CPerson!"<<endl;
}
void CPerson::show()
{
cout<<"姓名:"<<m_name<<" 年龄:"<<m_age<<" 住址:"<<m_address<<endl;
}
CStudent::CStudent(int a,string *ad,string n,string g):CPerson(a,ad,n)
{
strcpy(m_grade,g);
}
CStudent::~CStudent()
{
cout<<"~CStudent!"<<endl;
}
void CStudent::show()
{
cout<<"姓名:"<<m_name<<" 年龄:"<<m_age<<" 年级:"<<m_grade<<" 住址:"<<m_address<<endl;
}
int main()
{
string s="dizhi";
string *p=s;
CPerson *poCP=new CStudent(20,p,"zhangsan","dayi");
if(NULL==poCP)
{
exit(0);
}
poCP->show();
delete poCP;
cout<<"通过delete析构new运算符创建的对象结束!\n"<<endl;
CStudent oCSt(20,p,"zhangsan","dayi");
oCSt.show();
return 0;
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询