



#include <iostream>
using namespace std;
//父类
class Father
{
int age;
public:
double money;
int *f;
void show_add();
//无参构造
Father():f(new int (0)){}
//有参构造
Father(int age,double money,int f,string name):age(age),money(money),f(new int (f)),name(name){}
Father(double money,int f,string name):money(money),f(new int (f)),name(name){}
//拷贝构造
Father(const Father &other):age(other.age),money(other.money),f(new int (*(other.f))),name(other.name){}
~Father()
{
delete f;
}
// = 运算符重载
Father &operator=(const Father &other);
// + 运算符重载
Father operator+(const Father &other);
// << 运算符重载
friend ostream &operator<<(ostream &out,const Father &f1);
// >> 运算符重载
friend istream &operator>>(istream &in,Father &f1);
int get_age()const
{
return age;
}
void set_age(int age)
{
this->age = age;
}
protected:
string name;
};
Father &Father::operator=(const Father &other)
{
age = other.age;
*f = *(other.f);
money = other.money;
name = other.name;
return *this;
}
ostream &operator<<(ostream &out,const Father &f1)
{
out << "age = " << f1.age << "\t"
<< "money = " << f1.name << "\t"
<< "name = " << f1.name << "\t";
return out;
}
istream &operator>>(istream &in,Father &f1)
{
cout << "请输入age:";
in >> f1.age;
cout << "请输入money:";
in >> f1.money;
cout << "请输入f:";
in >> *f1.f;
cout << "请输入name:";
in >> f1.name;
return in;
}
Father Father::operator+(const Father &other)
{
Father temp;
temp.age = age+other.age;
temp.money = money+other.money;
temp.name = name+other.name;
return temp;
}
void Father::show_add()
{
cout << "father_address:\t\t\t" << &age << endl;
cout << "father::money_address:\t\t" << &money << endl;
cout << "father::name_address:\t\t" << &name << endl;
}
class Son : public Father
{
int a;
int *s;
public:
void show_Sadd();
//无参构造
Son():s(new int (0) ){}
//有参构造
Son(int age,double money,int f,string name,int a,int s):Father(age,money,f,name),a(a),s(new int (s)){}
//拷贝构造 直接传子类的other对象调用父类的拷贝构造
Son(const Son &other):Father(other),a(other.a),s(new int(*(other.s))){}
~Son()
{
//delete f;
delete s;
}
// = 运算符重载
Son &operator=(const Son &other);
// + 运算符重载
Son operator+(const Son &other);
// << 运算符重载
friend ostream &operator<<(ostream &out ,const Son &s1);
};
Son &Son::operator=(const Son &other)
{
this->Father::operator=(other);
*s = *(other.s);
a = other.a;
return *this;
}
ostream &operator<<(ostream &out ,const Son &s1)
{
out << "age = " << s1.get_age() << "\t"
<< "money = " << s1.money << "\t"
<< "*f = " << *s1.f << "\t"
<< "name = " << s1.name << "\t"
<< "a = " << s1.a << "\t"
<< "*s = " << *s1.s;
return out;
}
Son Son::operator+(const Son &other)
{
Son temp;
temp.Father::operator=(this->Father::operator+(other));
delete s;
s = new int (*this->s + *other.s);
temp.a = a+other.a;
return temp;
}
void Son::show_Sadd()
{
cout << "s = " << s << endl;
cout << "*s = " << *s << endl;
cout << "对象大小:" << endl;
cout << "sizeof(Father) = " << sizeof(Father) << endl;
cout << "sizeof(Son) = " << sizeof(Son) << endl;
cout << "\n地址信息:" << endl;
cout << "son_address:\t\t\t" << this << endl;
show_add();
cout << "son.a_address:\t\t\t" << &a << endl;
cout << "\n地址偏移量:" << endl;
cout << "money偏移:\t\t\t" << (char*)&money - (char*)this << endl;
cout << "name偏移:\t\t\t" << (char*)&name - (char*)this << endl;
cout << "a偏移:\t\t\t\t" << (char*)&a - (char*)this << endl;
}
int main()
{
Son s1(18,999999,66,"张三",11,22);
Son s2;
s2 = s1;
s1.show_Sadd();
cout << s1 << endl;
s2.show_Sadd();
cout << s2 << endl;
return 0;
}