function
- Five points outstanding function
- Return value: direct and indirect return return (pointer)
- To test the validity of the parameters at the entrance of the function: if statements, assertions (assert)
- If the function has a return value, the function returns a pointer to non-body pointer or reference to a local object
- Functions to a single function
Object-Oriented
- 60's software crisis: in the increasingly complex hardware and software systems, the software how to get good design and maintenance
- Structured programming method (60--80 years): Ask a question, the big question is divided into several small problems that the various sub-modules to address each sub-module, and finally through the main function calls sub-modules (according to some top-order down, stepwise refinement)
advantage:
-
- Program is only three basic structures: sequence, selection, cycle
- Divide and rule, all broken
- Top-down, stepwise refinement
Disadvantages:
-
- Operating data and separated from each other: if the data format is changed, the corresponding operation will overwrite function
- Difficult to expand: extensions involve redrawing module, it requires a lot of rewriting of the original function
- Difficult to reuse: compact module in conjunction with the specific application environment, it is difficult to reuse the old module in the new program
- Object-oriented programming methods: the real world is composed of many related to each other and communicate with each entity - the object (object), but each object is composed of two parts, describing the object description data and behavior of the object properties of functions (methods) . Data and functions closely together constitute the object describes the real world
- Features:
- Package: Object data and operation so closely
- Inheritance: support code reuse
-
- Polymorphism: different structures such objects may share the same external interface
- advantage
- Easy to design, maintain: understanding of the law in line with the code, easy to understand, highly readable
- And code reuse design, high quality systems: inheritance and polymorphism
- Easy to expand
- Access to class members
- Friend: let the public function or class can access a class of hidden information
- When the two parts have a different number of classes or instances of different life cycles, in order to maintain "high cohesion" class, a class is often necessary to split into two parts, i.e. two classes, so that two classes require direct access each other's data, the safest way is to define them as a friend of one another
- Abstract package: from reality to a class specific things
- Inheritance: concrete constantly, constantly heritage properties and behavior of the base class, and develop their own unique properties and behavior of the process
- Three kinds of evolutionary ways:
- Retention properties and behavior of the base class: Human and Teacher
- Improved properties and behavior of the base class: Student and Pupil class has DoHomework ()
- 添加新的属性和行为:
- 派生方式
- public:类型继承。派生类是基类的一个子类型,基类所有成员的访问级别在派生类中不做改变(is-a)
- private:实现继承。把基类的公有成员变成自己的私有成员
- protected:把基类的所有公有成员变成protected类型,保护基类的所有接口不被外界访问
- 使用规范
- 拥有派生关系的两个类必须相关:不可为了让“人”具有“飞行”的行为,而让“人”从“鸟”派生
- 不要把组合当成继承:若A是B的“一部分”,则不允许B继承A的功能,如键盘、鼠标是电脑的一部分,但电脑不能由键盘或鼠标派生
- 多态:大多数情况下派生类是基类的“一种”,如学生是人的一种,“教室里有多少人?”其实代表“教室里有多少学生?”,又比如“上车的人请买票”,都是在用基类指代派生类。但有时派生类会有对基类的行为进行自定义的需要,这时就要用到虚函数
- 如果派生类对虚函数重新定义,那么通过基类指针调用的就是具体对象的虚函数
- 当类中有纯虚函数时就成了一个抽象类,仅用于对外界提供公有接口