C++学习/复习20--继承的权限/向上转换/重定义/默认成员函数/友元/静态成员/菱形虚拟继承/组合

一、继承的概念

二、继承的权限

三、向上转换

四、重定义(隐藏)

五、派生类的默认成员函数

六、继承与友元

七、继承与静态成员

八、菱形继承

数据冗余与二义性

虚拟继承(virtual)

九、继承组合

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个示例: ```c++ #include <iostream> using namespace std; struct MyStruct { int a, b; MyStruct(int a = 0, int b = 0) : a(a), b(b) {} // 加法运算符载 MyStruct operator+(const MyStruct& other) const { return MyStruct(a + other.a, b + other.b); } // 减法运算符载 MyStruct operator-(const MyStruct& other) const { return MyStruct(a - other.a, b - other.b); } // 乘法运算符载 MyStruct operator*(const MyStruct& other) const { return MyStruct(a * other.a, b * other.b); } // 除法运算符载 MyStruct operator/(const MyStruct& other) const { return MyStruct(a / other.a, b / other.b); } // 赋值函数 MyStruct& operator=(const MyStruct& other) { a = other.a; b = other.b; return *this; } // 拷贝函数 MyStruct(const MyStruct& other) { a = other.a; b = other.b; } // 友元函数 friend ostream& operator<<(ostream& os, const MyStruct& obj) { os << "a: " << obj.a << ", b: " << obj.b; return os; } }; int main() { MyStruct a(1, 2), b(3, 4), c; // 测试加法运算符载 c = a + b; cout << "a + b = " << c << endl; // 测试减法运算符载 c = a - b; cout << "a - b = " << c << endl; // 测试乘法运算符载 c = a * b; cout << "a * b = " << c << endl; // 测试除法运算符载 c = a / b; cout << "a / b = " << c << endl; // 测试赋值函数 MyStruct d = a; cout << "d = " << d << endl; // 测试拷贝函数 MyStruct e(a); cout << "e = " << e << endl; return 0; } ``` 输出结果: ``` a + b = a: 4, b: 6 a - b = a: -2, b: -2 a * b = a: 3, b: 8 a / b = a: 0, b: 0 d = a: 1, b: 2 e = a: 1, b: 2 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值