首先复习一下:
类中成员可以分为3种:
1)公有成员:可以在类内访问,也可以在类外访问(通过对象访问),子类和本类都可以访问;
2)保护成员:不可以类外访问,可以在子类或本类中访问
3)私有成员:不可以类外访问,不可以子类中访问,不可以对象访问,只能本类中访问;
(1)继承的方式:
1)公有继承
2)保护继承继承
3)私有继承
1)公有继承
1----子类成员可以访问父类的公有成员和保护成员
2 ----子类对象可以访问父类的公有成员
3----子类无法访问父类的私有成员
4----子类的子类可以访问父类的公有成员和保护成员(具体访问权限需要看子类和子类的子类的继承关系)
5----子类的子类无法访问父类的的私有成员
公有继承的例子如下:
#include<iostream>
using namespace std;
class father
{
public:
void father_public();
protected:
void father_protected();
private:
void father_private();
};
void father::father_public()
{
cout<<"this is father_public"<<endl;
}
void father::father_protected()
{
cout<<"this is father_protected"<<endl;
}
void father::father_private()
{
cout<<"this is father_private"<<endl;
}
//类的公有继承。
//通过子类对象可以直接访问父类的公有成员;
//可以在子类中直接访问父类的保护成员,但是不能通过子类的对象访问父类的保护成员;
//子类不能在子类中直接访问父类的私有成员,
class son:public father
{
public:
void son_call_on_protected();
};
void son::son_call_on_protected()
{
cout<<"son_son_call_on_public ";
father_protected(); //可以访问father中的protected;
//father_private();仍然无法访问
}
void test_public_inherrit()
{
father father_obj;
// father_obj.father_protected(); protected修饰的成员不能再外部访问,只能在类中访问
son son_obj;
son_obj.father_public();
// obj.father_protected();保护成员只能在本类和子类中被访问,不能在外部被访问
// son_obj.father_private();父类的私有成员子类无法直接访问,父类的私有成员只能被父类的成员访问。
son_obj.son_call_on_protected();
}
int main()
{
test_public_inherrit();
}
2)保护继承
1----子类对象无法访问父类
2----子类成员可以访问父类的公有成员和保护成员
3----子类无法访问父类的私有成员 (无论是成员访问还是对象访问都不行)
4----子类的子类可以通过子类的子类的成员访问父类的公有成员和保护成员
5----子类的子类无法访问父类的的私有成员 (无论是成员访问还是对象访问都不行)
例子:
#include<iostream>
using namespace std;
class father
{
public:
void father_public();
protected:
void father_protected();
private:
void father_private();
};
void father::father_public()
{
cout<<"father_public"<<endl;
}
void father::father_protected()
{
cout<<"father_protected"<<endl;
}
void father::father::father_private()
{
cout<<"father_private"<<endl;
}
class son:protected father//保护继承
{
public:
void s_call_on_f_public_and_protected();
};
void son::s_call_on_f_public_and_protected()
{
cout<<"this is son"<<endl;
father_public();
father_protected();
// father_private();报错:在son类中访问father的私有成员
}
class grandson:public son
{
public:
void g_call_on_f_public_and_protected();
};
void grandson::g_call_on_f_public_and_protected()
{
cout<<"this is grandson"<<endl;
father_public();
father_protected();
// father_private();报错:在grandson类中访问father的私有成员
}
int main()
{
son son_obj;
son_obj.s_call_on_f_public_and_protected();
grandson g_son_obj;
g_son_obj.g_call_on_f_public_and_protected();
// son_obj.father_public();报错:通过对象访问father的公有成员
//son_obj.father_protected();报错:通过对象访问father的保护成员
// father_private();//报错:通过对象访问father的私有成员
}
3)私有继承
1----子类的成员可以访问父类的公有成员和保护成员
2----子类的对象不能访问父类的公有成员和保护成员(无论是成员访问还是对象访问都不行)
3----子类的子类不能访问子类的父类的成员,(无论是成员访问还是对象访问都不行)
例子:
#include<iostream>
using namespace std;
class father
{
public:
void father_public();
protected:
void father_protected();
private:
void father_private();
};
void father::father_public()
{
cout<<"father_public"<<endl;
}
void father::father_protected()
{
cout<<"father_protected"<<endl;
}
void father::father::father_private()
{
cout<<"father_private"<<endl;
}
class son:private father//私有继承
{
public:
void s_call_on_f_public_and_protected();
};
void son::s_call_on_f_public_and_protected()
{
cout<<"this is son"<<endl;
father_public();
father_protected();
// father_private();报错:在son类中访问father的私有成员
}
class grandson:public son
{
public:
void g_call_on_f_public_and_protected();
};
void grandson::g_call_on_f_public_and_protected()
{
cout<<"this is grandson"<<endl;
// father_public();报错:在grandson类中访问father的公有成员
// father_protected();报错:在grandson类中访问father的保护成员
// father_private();报错:在grandson类中访问father的私有成员
}
int main()
{
son son_obj;
son_obj.s_call_on_f_public_and_protected();
// son_obj.father_public();报错:通过对象访问father的公有成员
//son_obj.father_protected();报错:通过对象访问father的保护成员
// father_private();//报错:通过对象访问father的私有成员
grandson g_son_obj;
g_son_obj.g_call_on_f_public_and_protected();
}