单继承
C++中所谓的继承就是在一个已存在的类基础上建立一个新的类
已存在的类称为基类或父类,新建立的类称为派生类或子类
//单继承--公有派生
#include <iostream>
using namespace std;
class A
{
public:
int z;//公有数据成员
A(int a, int b, int c)
{
cout << "调用类A带参的构造函数" << endl << endl;
x = a;
y = b;
z = c;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
int getz()
{
return z;
}
void print()
{
cout << "x=" << x << ",y=" << y << ",z" << z << endl << endl;
}
protected:
int y;//保护数据成员
private:
int x;//私有数据成员
};
class B : public A
{
public:
B(int a, int b, int c, int d, int e) :A(a, b, c)
{
cout << "调用派生类B带参的构造函数" << endl