子类和父类的拷贝和赋值函数实现

#include
using namespace std;
class Parent
{
public:
Parent(int a): m_a(a)
{}
Parent(const Parent &p)
{
cout << “parent copy function” << endl;
m_a = p.m_a;
}
Parent()
{}
Parent& operator=(const Parent &p)
{
cout << “parent = function” << endl;
if (this != &p)
{
this->m_a = p.m_a;
}
return *this;
}
void seta(int a)
{
this->m_a = a;
}
int geta()
{
return this->m_a;
}
private:
int m_a;
};
class Child:public Parent
{
public:
Child(int a, int b) :Parent(a), m_b(b) {}
Child(const Child &ch) :Parent(ch)
{
cout << “child copy function” << endl;
//Parent::Parent(ch);//这个打印出现乱码
this->m_b = ch.m_b;
}
Child()
{}
Child& operator=(const Child &ch)
{
cout << “child = function” << endl;
Parent::operator=(ch);
if (this != &ch)
{
this->m_b = ch.m_b;
}
return *this;
}
void setb(int b)
{
this->m_b = b;
}
int getb()
{
return this->m_b;
}
private:
int m_b;
};
int main()
{
Child ch1(10, 50);
cout << ch1.geta() << " " << ch1.getb() << endl;
Child ch2 = ch1;
cout << ch2.geta() << " " << ch2.getb() << endl;
Child ch3;
ch3 = ch1;
cout << ch3.geta() << " " << ch3.getb() << endl;
system(“pause”);
return 0;
}

非常好,下面我来给出具体的代码实现和解释每个知识点的含义。 ```C++ #include <iostream> #include <string> using namespace std; class Animal { protected: string name; int age; double weight; double height; public: Animal(string n, int a, double w, double h) : name(n), age(a), weight(w), height(h) {} virtual void displayInfo() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; } friend Animal operator+(const Animal& a1, const Animal& a2) { Animal a("New Animal", 0, a1.weight + a2.weight, a1.height + a2.height); return a; } }; class Cat : public Animal { protected: string color; public: Cat(string n, int a, double w, double h, string c) : Animal(n, a, w, h), color(c) {} void displayInfo() override { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; cout << "Color: " << color << endl; } }; class Dog : public Animal { protected: string breed; public: Dog(string n, int a, double w, double h, string b) : Animal(n, a, w, h), breed(b) {} void displayInfo() override { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; cout << "Breed: " << breed << endl; } }; class Kitten : public Cat { protected: char gender; double weightChange; public: Kitten(string n, int a, double w, double h, string c, char g, double wc) : Cat(n, a, w, h, c), gender(g), weightChange(wc) {} Kitten(const Kitten& k) : Cat(k.name, k.age, k.weight, k.height, k.color), gender(k.gender), weightChange(k.weightChange) {} Kitten& operator=(const Kitten& k) { if (this != &k) { this->name = k.name; this->age = k.age; this->weight = k.weight; this->height = k.height; this->color = k.color; this->gender = k.gender; this->weightChange = k.weightChange; } return *this; } void displayInfo() override { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; cout << "Color: " << color << endl; cout << "Gender: " << gender << endl; cout << "Weight Change: " << weightChange << endl; } friend Kitten operator+(const Kitten& k1, const Kitten& k2) { Kitten k("New Kitten", 0, k1.weight + k2.weight, k1.height + k2.height, k1.color, 'M', 0); return k; } }; int main() { Animal* a = new Animal("Animal", 10, 20, 30); Cat* c = new Cat("Cat", 5, 10, 20, "Black"); Dog* d = new Dog("Dog", 3, 15, 25, "Husky"); Kitten* k1 = new Kitten("Kitten1", 1, 2, 3, "White", 'F', -0.5); Kitten* k2 = new Kitten("Kitten2", 2, 3, 4, "Gray", 'M', 0.5); a->displayInfo(); cout << endl; c->displayInfo(); cout << endl; d->displayInfo(); cout << endl; k1->displayInfo(); cout << endl; k2->displayInfo(); cout << endl; Animal* a_new = new Animal("New Animal", 0, 0, 0); *a_new = *a + *d; a_new->displayInfo(); cout << endl; Kitten* k_new = new Kitten("New Kitten", 0, 0, 0, "", 'M', 0); *k_new = *k1 + *k2; k_new->displayInfo(); cout << endl; delete a; delete c; delete d; delete k1; delete k2; delete a_new; delete k_new; return 0; } ``` 这个程序实现了一个动物类族,包括基类Animal和两个派生类Cat和Dog以及一个继承自Cat的子类Kitten。其中,每个类都有自己的成员变量和成员函数,并且在子类中可以重载父类函数实现不同的功能。同时,还使用了一些重载运算符、静态成员、深拷贝、虚函数等知识点来实现更加灵活的操作和更加高效的代码。 具体来说,以下是每个知识点的解释: - 静态成员:使用static关键字声明的成员,属于整个类,而不是属于某个对象,可以被类的所有对象共享。在本程序中,我们可以使用静态成员来实现所有动物的个数统计等功能。 - 友元函数:使用friend关键字声明的函数,可以访问类的私有成员。在本程序中,我们使用友元函数实现运算符重载等功能。 - 运算符重载:使用operator关键字重载运算符,可以方便地对类对象进行操作。在本程序中,我们重载+号来实现两个Animal对象的体重相加,以及重载+号来实现两个Kitten对象的体重相加。 - 类族对象的赋值规则:派生类的对象可以赋值给基类的对象,但是基类的对象不能赋值给派生类的对象。在本程序中,我们可以将Cat对象赋值给Animal对象,但是不能将Animal对象赋值给Cat对象。 - 虚函数或纯虚函数:使用virtual关键字声明的函数,可以实现动态多态性,即同一个函数在不同的对象上有不同的实现方式。在本程序中,我们使用虚函数实现动物类的displayInfo()函数,使得每个子类都可以根据自己的需求来覆盖该函数。 - 深拷贝子对象:在派生类的构造函数赋值运算符中,需要调用父类的构造函数赋值运算符来复制父类的数据成员,以实现拷贝。在本程序中,我们在Kitten类中手动实现了深拷贝子对象的过程。 - 全局对象:使用static关键字声明的全局对象,属于整个程序,而不是属于某个函数或类。在本程序中,我们可以使用全局对象来实现一些需要全局访问的功能,比如记录程序运行时间等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值