Monday, March 3, 2014

C++ OOP Knowledge

1. 类(Class)与 对象(Object)的关系 就是具体(Concrete)与(Abstract)的关系

2. 构造函数(constructor)

     构造函数的函数名和相应的类名相同。构造函数没有任何返回值,也不加void; 一个类可以不包含构造函数,此时当类的对象被定义时,编译器会自动定义一个默认构造函数; 构造函数必须要保证一旦一个对象被构造,类中的所有数据成员都有合理的定义好的值。
     Constructors are recognized by their names which is equal to the class name. Constructors do not specify return type, not even void.
     It is possible to define a class lacking any constructors. In that case the compiler defines a default constructor that is called when an object of that class is defined.
     Constructors must ensure that all the class's data members have sensible or as-least well-defined values once the object has been construct.

     1)默认构造函数(default constructor);Default constructor has no argument.
     2)构造函数可以不带任何参数;
     3)构造函数可以带有默认参数,参数的个数可以任意;
     4)构造函数的重载(overload of constructor):一个类有多个不同参数形式及参数类型不同的构造函数;
     Example: Class point;
                        构造函数: point();
                                          point(double x, double y);

     5)拷贝构造函数 (Copy Constructor):作用是用一个已经存在的对象来初始化该类的对象;用户可以根据需要定义拷贝构造函数,也可以由系统生成一个默认拷贝构造函数.

     A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance.        According to the C++ standard, the copy constructor for MyClass must have one of the following signatures:
    MyClass( const MyClass& other );
    MyClass( MyClass& other );
    MyClass( volatile const MyClass& other );
  MyClass( volatile MyClass& other );    Myclass p1;
    Myclass p2(p1); //copy constructor is called;
    

    Copy Constructor is called in following three ways:
    1) Using an object of the class to instantiate another object;
    2) The formal parameter of a function is an object of a class. The copy constructor is called when the formal parameter and the actual parameter are combined;
    3) The return value of a function is an object of a class. The copy constructor is called when the function is completed and the return the caller.
     6)默认拷贝构造函数(Default Copy Constructor):当用一个已经存在的对象初始化本类的新对象时,如果没有自定义拷贝构造函数,则系统会自动生成一个默认的拷贝构造函数来完成初始化的工作。
          When using an existing object to instantiate a new object, if there is no self-defined copy constructor, the system will automatically generate a default copy constructor to complete the instantiation. 



3. 析构函数
    也是一种特殊的成员函数,也被声明为共有成员。析构函数的作用是释放分配给对象的内存空间。 The function of the destructor is to release the memory space that is allocated to objects.
    Destructor has no parameters, no return value, can not be reloaded. There can only be one destructor.


4.  友元(Friend)
    1)友元函数(Friend Function)
             友元函数不是当前类的成员函数,而是独立于当前类的外部函数;它可以是普通函数或其他类的成员函数。            友元函数使用前必须要在类定义时声明,声明是在其函数名前加上关键字friend。友元函数的定义一般在外部进行,不需要在其名字前加class::. 友元函数不是类的成员,不能直接引用对象成员的名字,也不能通过this指针引用对象成员,必须通过作为入口参数传递进来的对象名或对象指针来引用该对象的成员。       

 class point{                friend double dist(point &p1, point &p2);}
 double dist(point &p1, point &p2){}
        当一个函数需要访问多个类时,应该把这个函数同时定义为这些类的友元函数。    2)友元成员 (friend member function)
            当一个类的成员函数是另一个类的友元函数,则称这个成员函数为友元成员。通过友元成员函数,不仅可以访问自己所在类对象中的私有和公有成员,还可以访问由关键字friend 声明语句所在的类对象中的私有和公有成员。    3)友元类(Friend Class)
            当一个类作为另一个类的友元时,称这个类为友元类。这个类的所有成员函数都成为另一个类的友元函数。 因此,友元类中的所有成员函数都可以通过对象名直接访问另一个类中的私有成员。友元类的声明可以放在类声明中的任何位置。



5. 继承与派生(Inheritance and Derivation)
Base\继承方式      Public                  Private                  Protected    

Private                  private                 不可访问               不可访问    
Public                   public                     private                  protected     
Protected              protected                 private                  protected      
      1) 派生函数的构造函数            派生类的数据成员由 所有基类的数据成员,派生类新增的数据成员共同组成,如果派生类新增成员中还有对象成员,那派生类的数据成员中还间接含有这些对象的数据成员。因为对派生类对象初始化,就要对基类数据成员,新增数据成员和对象成员的数据进行初始化。           

 <派生类名>::<派生类名> (参数总表):基类名(参数表),对象成员名1(参数表1),..., 对象成员名n(参数表n){                        //派生类新增成员的初始化语句            
      2)派生函数的析构函数
            析构函数不能被继承,如果需要,则在派生类中重新定义。      

      3)构造和析构的顺序
            构造:先base再derivation;            析构:先derivation再base;



6. 多重继承(Multiple Inheritance)
      1)二义性问题(ambiguity)
            当多重继承(有多个基类)以及多个基类中有同名成员时,会产生二义性问题            解决方法:                 

 1)使用作用域运算符“::” , 指定相应的基类                  
2)使用同名覆盖额原则在派生类中重新定义与基类中同名的成员(如果是成员函数,则参数表也要相同,参数不同的情况为重载)以隐蔽掉基类的同名成员,在引用这些同名的成员时,就是使用派生类中的函数。                  
3) 使用虚函数,具体方法见“7.虚基类”的解释。


7. 虚基类(Virtual Base Class)
      虚基类引入的作用:解决基类中由于同名成员的问题而产生的二义性 
      1)base1继承了base,base2继承了base,那就可以把base定义为虚基类,继承时应该这样写      

class base1: virtual public base{
      }      

class base2: virtual public base{
      }



8. 多态(Polymorphism)
      多态就是同一个函数名称,作用在不同的对象上将产生不同的操作。就是“一个接口,多种实现”,就是同一种事物表现出的多种形态。
      多态包括:重载多态,强制多态,包含多态,参数多态
      包含多态:定义于不同类中的同名成员函数的多态行为,主要通过虚函数来实现;


      多态的实现方式: 1)编译时的多态;2)运行时的多态
       1)编译时的多态/静态多态性, 由静态联编支持的,用函数重载和运算符重载来实现;
       2)运行时的多态/动态多态性,由动态联编支持的, 用继承和虚函数来实现;

       3) 虚函数的定义在基类中进行 在原先定义的函数名和返回type前加 virtual
 
              重载虚函数: 要求函数名、返回类型、参数个数、参数的类型和参数的顺序必须与基类中的虚函数的原型完全相同;
              析构函数也可以声明成虚函数,只要在“~”前加上virtual;
              一个虚函数无论被继承多少次,仍保持其虚函数的特性,与继承次数无关

9. 抽象类(Abstract Class)多态的一种
       抽象类为类提供统一的操作界面,简历抽象类的原因就是为了通过抽象类多态使用其中的成员函数,抽象类是带有纯虚函数的类。
     1)纯虚函数(Pure Virtual Function)
            在积累中不能为虚函数给出一个有意义的实现时,可以将其声明为纯虚函数。 纯虚函数的具体实现可以留给派生类完成。纯虚函数的作用是为派生类提供一个一致的接口(A consistent interface)。
            纯虚函数: virtual void set()= 0
      2) 抽象类
            带有纯虚函数的类称为抽象类,抽象类不能定义对象。
            实际中,为了强调一个类是抽象类,可以将该类的构造函数说明为保护的访问控制权限

No comments:

Post a Comment