#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
class Human
{
public:
Human(){cout <<"无参构造"<<endl;}
Human(char* a,int b){
int len=strlen(a);
name=new char[len+1];
strcpy(name,a);
age=b;
cout << "有参构造" <<endl;
}
/*
Mystring(const Mystring& other){
int len=strlen(other.name);
name=new char[len+1];
strcpy(name,other.name);
age=other.age;
cout <<"Human拷贝构造"<<endl;
}
*/
~Human(){
if(name!=nullptr)
{
delete []name;
name=nullptr;
}
age=0;
cout<<"析构函数"<<endl;
}
void show()
{
cout << "Human::name" << name << endl;
cout << "Human::age=" << age << endl;
}
private:
char* name;
int age;
};
class Student:public Human
{
private:
int score;
public:
Student(){cout <<"子类无参构造"<<endl;}
Student(char* a,int b,int c):Human(a,b),score(c){
cout <<"Child 有参构造"<<endl;
}
~Student(){
score=0;
cout<<"析构函数"<<endl;
}
void show()
{
cout << "Student::score=" << score << endl;
}
};
class Party:public Human
{
public:
Party(){cout <<"子类无参构造"<<endl;}
Party(char* x,int y,char* a,char* b):Human(x,y){
int len1=strlen(a);
int len2=strlen(b);
activity=new char[len1+1];
strcpy(activity,a);
organization=new char[len2+1];
strcpy(organization,b);
}
~Party(){
if(activity!=nullptr)
{
delete []activity;
activity=nullptr;
}
if(organization!=nullptr)
{
delete []organization;
organization=nullptr;
}
cout<<"析构函数"<<endl;
}
void show()
{
cout << "Party::activity=" << activity << endl;
cout << "Party::organization=" << organization << endl;
}
private:
char* activity;
char* organization;
};
class Cadres:public Student,Party//干部
{
public:
Cadres(){}
Cadres(char* a,int b,int c,char* z,char* e,char* ca):Student(a,b,c),Party(a,b,z,e){
int len=strlen(ca);
posts=new char[len+1];
strcpy(posts,ca);
cout << "有参构造" <<endl;
}
~Cadres(){
if(posts!=nullptr)
{
delete []posts;
posts=nullptr;
}
cout<<"析构函数"<<endl;
}
void show()
{
//Party::Human::show();//‘Human’ is an ambiguous base of ‘Cadres’
Party::show();
Cadres::show();
cout << "Cadres::posts=" << posts << endl;
}
private:
char* posts;
};
int main(int argc, const char *argv[])
{
//Human("zhangsan",20);
//Student("zhangsan",20,95);
//Party("paty","red");
//Cadres()
return 0;
}
09-04
29
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-04
49
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-04
156
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-28