#include<iostream> #include<string> using namespace std;
class Person { char *name,*addr,*sex; int age; public: Person(char *name1,char *addr1,char *sex1,int age); ~Person(); print();
}; Person::Person(char *name1,char *addr1,char *sex1,int age) { name=new char(strlen(name1)+1); strcpy(name,name1); addr=new char(strlen(addr1)+1); strcpy(addr,addr1); sex=new char(strlen(sex1)+1); strcpy(sex,sex1); this->age=age; } Person::~Person(){delete name;delete addr;delete sex;} Person::print() { cout<<"姓名:"<<name<<"\n地址:"<<addr<<"\n性别:"<<sex<<"\n年龄:"<<age<<endl; }
class Student:public Person { int math,english; public: Student(char *name1,char *addr1,char *sex1,int age,int math,int english); _print();
}; Student::Student(char *name1,char *addr1,char *sex1,int age,int math,int english):Person(name1,addr1,sex1,age) { this->math=math; this->english=english; } Student::_print() { print(); cout<<"\n数学分数:"<<math<<"\n英语分数:"<<english<<endl; }
void main() { Student s1("张书豪","吉林通化","男",23,98,90); s1._print(); }