#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;
}
子类和父类的拷贝和赋值函数实现
