面向对象程序设计 Object Oriented Design

Overview

This course mainly focuses on the idea of object-oriented programming and the C++ programming language, including:

  • Features of OOD
  • The difference between C++ and C
  • Inheritance and Derivation
  • Polymorphism and virtual functions

一、Features of OOD

Unlike structured programming, OOD puts data and operations on data together. It abstracts objects and operations on objects into classes.

OOD has four major characteristics:

  • 抽象 Abstract
  • 封装 Encapsulation
  • 继承 Inheritance
  • 多态 Polymorphism

二、C++ 与 C 相比

C++ adds object-oriented concepts and corresponding processing mechanisms on the basis of C.

C is a subset of C++.

C++ has some unique features.

1. 名字空间 Namespace

The namespace is a more flexible global domain. We can name a namespace like this:

namespace ns1{
    float a, b, c;
    fun1(){}
}

If you want to use namespace members, you can declare them with a using statement. All components in the standard C++ library are declared and defined in the namespace std.

using namespace std;

2. 引用 Reference

Reference refers to using two or more variable names to point to the same address. Operations on references are operations on the referenced variables.

Like:

int num = 1;
int& ref = num;
num++;
ref++;

After the statement is executed, the value of num is 3

In C++, we often use references as function parameters. In C language, if the main function and sub-functions want to write to a non-global variable, we need to use pointers. But pointers can easily cause address overflow.

Like:

void swap(int &a, int &b){
    int temp = a;
    a = b;
    b = temp;
}

三、Inheritance and Derivation

Inheritance is an important feature of OOD. Code reuse can be achieved through inheritance. Inheritance is to create a new class based on a class, the new class contains all the members of the base class.

C++ supports single inheritance and multiple inheritance.

1. Three ways of inheritance

In C++, the members of a class have three access types: public, protected, and private. Similarly, there are three ways of inheritance.

Under different inheritance methods, the access attributes of the base class members are different. Private members of the base class can only be accessed by members of the base class.

2. Virtual Base Class

When there is a common base class on multiple inheritance paths, the common base class will produce multiple copies at the confluence of some of these paths. If we only want to save an instance of this base class, we can declare it as a virtual base class.

class Bed : virtual public Furniture{
    public:
    	Bed(){}
    	void Sleep(){
            cout<<"Sleeping..."<<endl;
        }
}

四、多态与虚函数 Polymorphism and Virtual Functions

C++ supports two types of polymorphism: compile-time polymorphism and runtime polymorphism. The former is realized by overload, and the latter is realized by inheritance and virtual functions.

1. 重载 Overload

There are two types of overload: function overload and operator overload.

Function overload means that two or more functions have the same name, but the number or types of parameters are different.

Like:

int max(int a, int b){
    return a > b ? a : b;
}
float max(float, float b){
    return a > b ? a : b;
}

Operator overloading is to give operators new meaning. Not all operators support overloading.

比如,重载运算符 ‘+’:

Box operator+(Box& b){
    Box newbox;
    newbox.length = this->length + b.length;
    return newbox;
}
Box3 = Box1 + Box2;

2. 虚函数 Virtual Functions

The virtual function provides an interface, which is defined in the base class. After the subclass inherits the base class, the virtual function needs to be redefined.

Each subclass can define the function of the virtual function by itself. Unlike overloaded functions, virtual functions require exactly the same function name, return value type, and parameter sequence.

面向对象程序设计 Object Oriented Design

上一篇:关于鉴权,看懂这篇就够了


下一篇:自定义泛型类