#include <iostream>
using namespace std;
class animal
{
private:
string name;
string color;
int *age;
public:
animal(){}
animal(string name,string color,int age):name(name),color(color),age(new int(age))
{
cout << "父类有参构造" << endl;
}
~animal()
{
delete age;
cout << "父类析构" << endl;
}
animal(const animal &other):name(other.name),color(other.color),age(new int(*(other.age)))
{
cout << "父类的拷贝构造" << endl;
}
animal &operator=(const animal &other)
{
if(this != &other)
{
name=other.name;
color=other.color;
age=new int(*(other.age));
cout << "父类的拷贝赋值" << endl;
}
return *this;
}
void show()
{
cout << " name = " << name<< " color = " << color << " age = " << *age << endl;
}
};
class Dog:public animal
{
private:
int *leg;
public:
void speak()
{
cout << "朱文涛在狗叫!" << endl;
}
Dog(){}
Dog(string name,string color,int age,int leg):animal(name,color,age),leg(new int(leg))
{
cout << "子类的构造" << endl;
}
~Dog()
{
delete leg;
cout << "子类的析构" << endl;
}
Dog(const Dog &other):animal(other),leg(new int(*(other.leg)))
{
cout << "子类的拷贝构造" << endl;
}
Dog &operator=(const Dog &other)
{
if(this != &other)
{
animal::operator=(other);
leg=new int(*(other.leg));
cout << "子类的拷贝赋值" << endl;
}
return *this;
}
void show()
{
animal::show();
cout << "leg = " << *leg << endl;
}
};
int main()
{
animal a1("diandian","white",6);
a1.show();
Dog g1("朱文涛","白色儿",88,4);
g1.show();
return 0;
}
#include <iostream>
using namespace std;
class human
{
private:
string name;
public:
human() {}
human(string name):name(name)
{
cout << "我是讲解员:" << name << endl;
}
virtual void speak() = 0;
virtual ~human()
{
cout << "等一哈,喝个水" << endl;
}
};
class elephant:public human
{
private:
string name;
public:
elephant() {}
elephant(string n,string name):human(n),name(name){}
void speak()
{
cout << "这是我们园内最出名的非洲象,叫" << name << ",他最喜欢吃煎饼" << endl;
}
};
class monkey:public human
{
private:
string name;
public:
monkey (){}
monkey(string n,string name):human(n),name(name){}
void speak()
{
cout << "接下来让我们看看大明星——白皮黑毛猴子,名为:" << name << ",他十分喜欢偷别的动物的桃子" << endl;
}
};
int main()
{
elephant e1("Dj","时兴隆");
human *p = &e1;
p->speak();
monkey m1("Dj","猪文涛");
p = &m1;
p->speak();
}