inherit

#include
#include
using namespace std;

//class Person
//{
//public:
// void Print()
// {
// cout << “name” << _name << endl;
// cout << “age” << _age << endl;
// }
//public:
// int _id;
protected 类外不可见,类内部和子类可见
//protected:
// string _name = “peter”;
//private:
// int _age = 18;
//};
继承:类代码复用
class/struct 类名:需要继承的类
子类/派生类 父类/基类
//
class 定义的类 默认继承方式 private
如果为struct 则默认继承方式为public
//class Student : public Person
//{
//public:
// void show()
// {
// cout << _name << “\t” << _id << endl;
// //父类private成员在子类中不可见 但是子类确实有父类的私有成员
// //cout<<_age<<endl;
// }
//};
//
//int test2()
//{
// Person p;
// Student st;
//
// //切片:子类对象 指针 引用可以直接赋值给父类对象 指针 引用 此处不是隐式类型转换
// p = st;
// Person& rs = st;
// Person* ptrs = &st;
//}
//
//class Empty
//{
//
//};
//
//void test()
//{
// cout << sizeof(Person) << endl;
// cout << sizeof(Student) << endl;
// cout << sizeof(Empty) << endl;
// Student st;
// st.Print();
//}
//class Person
//{
//public:
// void Print()
// {
// cout << “name:” << _name << endl;
// cout << “age:” << _age << endl;
// }
// void showId()
// {
// cout << _id << endl;
// }
//public:
// int _id = 1;
//protected:
// string _name = “peter”;
// int _age = 18;
//};
//
//
//class Student : public Person
//{
//public:
// void setNum(int num)
// {
// this->_num = num;
// }
// void showId()
// {
// cout << _id << endl;
// cout << Person::_id << endl;
//
// Person::showId();
// }
//public:
// int _id = 100;
// int _num = 2020;
//};
//
//void test()
//{
// Student st;
// cout << st._num << endl;
// st.setNum(1999);
// cout << st._num << endl;
// st.showId();
//}
//
//class Person
//{
//public:
// Person(int id = 10, int age = 20)
// : _id()
// , _age(age)
// {
// cout << “Person(int,int)” << endl;
// }
// Person(int id, int age, int num)
// {
// cout << “Person(int int int)” << endl;
// }
// Person(const Person& p)
// : _id(p._id)
// , _age(p._age)
// {
// }
// ~Person()
// {
// cout << “~Person” << endl;
// }
//protected:
// int _id;
// int _age;
//};
//
//class Student : public Person
//{
//
//public:
// Student(int id = 100, int age = 25)
// {
// cout << “Student(int,int)” << endl;
// }
//
//};
//
//void test()
//{
// Student st;
//}
//
//
//class Person
//{
//public:
// Person()
// {
// ++_count;
// }
//protected:
// string _name; // 姓名
//public:
// static int _count; // 统计人的个数。
//};
//
//int Person::_count = 0;
//
//class Student : public Person
//{
//protected:
// int _stuNum; // 学号
//};
//
//class Graduate : public Student
//{
//protected:
// string _seminarCourse; // 研究科目
//};
//
//void test()
//{
// Student st;
//
//}
//
//void test2()
//{
// Person p;
// Student s;
// Graduate g;
// p._count = 1;
// s._count = 2;
// g._count = 3;
// Person::_count = 4;
// Student::_count = 5;
// Graduate::_count = 6;
// cout << &p._count << endl;
// cout << &s._count << endl;
// cout << &g._count << endl;
//}

//class Person
//{
//public:
// string _name;
//};
//
//class Student : virtual public Person
//{
//protected:
// int _num;
//};
//
//class Teacher : virtual public Person
//{
//protected:
// int _id;
//};
//
//class Assistant : public Student, public Teacher
//{
//protected:
// string _majorCourse;
//};

//void test3()
//{
// Assistant ass;
// ass._name = “1”;
// cout << sizeof(Person) << endl;
// cout << sizeof(Student) << endl;
// cout << sizeof(Teacher) << endl;
// cout << sizeof(Assistant) << endl;
// cout << sizeof(ass) << endl;
//
//}

class A
{
public:
int _a;
};

class B :public A
{
public:
int _b;
};

class C : public A
{
public:
int _c;
};

class D : public B, public C
{
public:

int _d;

};

void test4()
{
D d;
d.B::_a = 1;
d._b = 2;
d.C::_a = 3;
d._c = 4;
d._d = 5;
}
int main()
{
test4();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值