类的继承有三种方式:
1、公有继承 2、私有继承3、保护继承
基类中的私有成员在派生类中时隐藏的,只能在基类中访问
派生类中的成员不能访问基类中的私有成员,但是可以访问基类中的公有成员和保护成员。
派生类从基类公有继承时,基类的公有成员和保护成员仍然是派生类中的公有成员和保护成员
私有继承时,公有成员和保护成员变成私有成员
保护继承时,基类的额公有成员变成保护成员,基类的而保护成员仍然是保护成员
#include<iostream>
using namespace std;
class base
{
private:
int a;
public:
void inita(int x)
{
a = x;
}
int geta()
{
return a;
}
};
class derived :public base
{
private:
int b;
public:
void initb(int y)
{
b = y;
}
int getb()
{
return b*geta();
}
};
int main()
{
derived ob;
ob.inita(12);
ob.initb(13);
cout << ob.getb() << endl;
system("pause");
return 0;
}