访问权限:- public 公共接口 :向外界开放,可通过对象或类名访问
- protected 受保护的访问:只向子类开放访问权限 ,不可通过对象访问
- private私有 :只有类成员可以访问,外界不可以通过任何形式访问
继承关系:- public继承:父类成员的访问权限在子类中不变,仍为自己原来的权限。
- protected继承:父类的public成员访问权限在子类中变为protected;父类的protected成员和private成员在子类权限维持原来的权限不变
- private继承:父类的所有成员访问权限在 子类中变为private
其中,public继承是is-a的关系,可以用父类的指针或者引用指向子类的对象;protected和private继承没有is-a的关系,只表示“组合”或者“拥有”的关系,不可以用父类的指针或引用指向子类对象。 C++对象模型中,子类对象的内存空间中包含父类的部分,当用父类指针指向一个子类的对象实时,这个指针可以访问的是相应的父类那部分的内存;在protected和private继承的情况下,父类的内存部分是私有的,不对外开放的,所以,protected和private继承时,不可以用父类的指针或引用指向子类对象。 通过对象访问成员时,只能访问到共有(Public)成员。 需要禁止类以构造形式实例化类的时候,可以将类的构造函数声明为private 和protected 的形式。构造函数声明protected 的形式的类不能被直接实例化,但可以通过被继承,子类可以在实例化的时候调用父类的protected构造函数。 构造函数声明为private的类的实例化不能依赖构造函数,可以提供public 的Instance()的方法,在Instance()定义中调用构造函数,返回类的实例。单例模式就是利用这个原理。
C++ Tutorial - Function members in classes:
Functions declared inside a class can be any of the following four types. This C++ Tutorial explains each one of them as below.Ordinary member functions : These are ordinary functions defined with a return type and parameters. The return type can also be void. The special trait about member functions is they can access the private/protected data members of their class and manipulate them. No external functions can access the private/protected data members of a class. The sample below this C++ Tutorial uses an ordinary member function Add(), returning an integer value.Constructors: Constructors in C++ are special member functions of a class. They have the same name as the Class Name. There can be any number of constructors inside a class, provided they have a different set of parameters. There are someimportant qualities for a constructor to be noted.- Constructors have the same name as the class.
- Constructors do not return any values
- Constructors are invoked first when a class is initialized. Any initializations for the class members, memory allocations are done at the constructor.
In the example class given below in this C++ tutorial has the constructor Example_Class(), with the same name as the class.Destructors: Destructors in C++ also have the same name, except for the fact that they are preceded by a '~' operator. The destructors are called when the object of a class goes out of scope. It is not necessary to declare a constructor or a destructor inside a class. If not declared, the compiler will automatically create a default one for each. If the constructor/destructor is declared as private, then the class cannot be instantiated. Check below for the sample class of the C++ tutorial for an example of destructor.C++ Tutorial - Access Level: The classes in C++ have 3 important access levels. They are Private, Public and Protected. The explanations are as follows.Private: The members are accessible only by the member functions or friend functions.Protected: These members are accessible by the member functions of the class and the classes which are derived from this class.Public: Accessible by any external member. Look at the sample class below.C++ Tutorial - Example of a class: class Example_class //Sample Class for the C++ Tutorial
{
private:
int x; //Data member
int y; // Data member
public:
Example_Class() //Constructor for the C++ tutorial
{
x = 0;
y = 0;
}
~Example_Class() //destructor for the C++ Tutorial
{ }
int Add()
{
return x+y;
}
};
阅读(3488) | 评论(0) | 转发(0) |