OOP Object-Oriented Programming-C ++

Source: Internet
Author: User
Tags class manager float max

Some concepts of OOP programming:

 

I. Object: This concept can be said to be the most core concept in object-oriented systems. If you cannot find an Object, how can you be object-oriented? Objects, that is, several factors designed in the problem you want to deal with. For example, if you make Student Score statistics, students are of course the objects you want to consider.

 

Ii. Class: in essence, classes are generated only when objects exist first, because when dealing with actual programming problems, you are faced with a specific number of objects. What should we do? By category, things are clustered by category. By the way, the objects with the same attributes are considered as one type. Both Zhang San and Li Si are examples of people. In the specific language implementation, the order of the two needs to be reversed, that is, you must first have a class before instantiating and generating objects.

 

3. Three main features of object-oriented architecture:

 

3.1 Encapsulation

 

The so-called encapsulation refers to the implementation of different permission settings for data and methods in the class during the class design. Some data and methods, we do not want users to call them Through instantiation outside the class, so we declare them as private. In this way, these data and methods can only be accessed by member functions within the class, if you want to be able to be accessed by an instance, it is declared as public. As for the Protection Type of the third type of access separator (protected), it is applied when the class is inherited, let's explain it to inheritance.

 

A concept related to encapsulation is abstract. The main functions of the classes to be designed will be expressed. These functions will be accessed by other classes and functions.

 

3.2 inheritance

 

The concept related to inheritance is the relationship. The relationship describes the relationship between classes. There are four basic types: inheritance, combination, exploitation, and instantiation ". Inheritance means that a class has all the data and methods of another class. Of course, you can modify a part and add new data and methods. There are two types of inheritance: Single-inheritance and multi-inheritance.

 

Single-inheritance, that is, the class Derived inherits the class Base, which is defined as follows:

 

Class Base

 

{

 

};

 

Class Derived: public Base

 

{

 

}

 

It can be seen that when defining an inherited class, you must specify the inherited class and the inheritance level (public, protected, private). Through the modification of these three access delimiters, you can change the access permissions of the base class data in the derived class. The public inheritance will not change. If protected inherits, the original public data and methods will become the protected type data and methods of the derived class, and the others will remain unchanged, private inheritance sets the access permissions of all data and methods to private, that is, these data and methods cannot be accessed outside the class.

 

The structure and structure sequence of the derived class. First, the base class is constructed, and then the derived class is constructed. The middle is the role of the class, and the structure sequence is the opposite.

 

Multi-inheritance means that a class inherits the data or methods of two or more classes. The main problem foreseen here in Multi-inheritance is two ambiguities: first, when the inherited two base classes contain the same data or methods, the calls in the derived classes cannot be distinguished, solution: Specify the member variables and methods of the called class, use the scope decomposition operator, and redefine the data or method in the derived class. The second type of ambiguity occurs in multi-layer inheritance. B and C inherit from A, and D inherit from B and C, just as the rectangle and diamond inherit the Quadrilateral while the square inherit the rectangle and the diamond, the two inheritance paths make the base class be inherited twice, and direct access cannot be distinguished. solution: the expenditure inheritance PATH uses the virtual method, when defining the inheritance relationship, add virtual to make the base class always have only one copy.

 

3.3 Polymorphism

 

There are two types of polymorphism: static polymorphism and dynamic polymorphism. The so-called polymorphism, that is, clearly the same thing, has different manifestations under different circumstances.

 

Static polymorphism (pre-defined) is implemented through funcition overload. A function contains three parameters: return value, function name, and function parameter. Multiple Names indicate the function, the differentiation of return values cannot differentiate functions (IMAGINE forced conversion of data classes ). Therefore, the overload means that the function name is the same and the parameters are different. Different values can be represented by different numbers of parameters, different parameter types, and different parameter order.

 

For example, int max (int, int)

 

Float max (float, float)

 

In addition, we have defined our own hope that the new type can still implement our original arithmetic operations, logical operations and other operators. The solution is to overload operators, that is, we can redefine the system's original operators (not all) on our own classes. For example

 

Complex: operator + (const Complex & C1)

 

The plural class defines its own addition operation.

 

Dynamic polymorphism refers to different performance during program execution. One concept involved is "association", that is, we need to use the method of A Class. Generally, we always use the instance or pointer of this class, this is the system must know which class the function is, that is, the process from the object to the class, some of which can be combined during compilation, but some can only be determined when the program is actually executed, that is, delayed Association. To implement delayed association, we need to set the function of the base class to virtual function (virtual) to implement delayed Association.

 

For example, both the class Manager and Saler inherit the class Employee, and both of the three classes have the CalcSalary () method. The program is as follows:

 

Employee * ptr;

 

Ptr = new Manager;

 

Ptr-> CalcSalary ();

 

Ptr = new Saler;

 

Ptr-> CalcSalary ().

 

What we hope is that two functions have different outputs, and virtual functions achieve this.

 

Pure virtual function, virtual CalcSalary () = 0; the function has no entity, and is waiting to be re-designed in the derived class. The class that contains one or more pure virtual functions is an abstract class, abstract classes cannot be instantiated but can be pointed to by pointers.

 

4. Constructor and destructor

 

Constructor and destructor are two special functions in the class. The constructor initializes the data within the class. The constructor has no return value. It has the same name as the class and can be reloaded. The Destructor implements initialization and performs tail scanning before the class ends. There is no return value type and the name is the same as that of the class ~ It indicates no. It cannot be reloaded and does not need to be called.

 

5. Const keywords and Volatile keywords

 

Role 2: Define the constant variable type const double PI = 3.1415926;

 

Modifier parameters are generally used to modify input parameters so that they can only be read and not written.

 

In addition, it is important to prevent the pointer from being modified.

When it comes to Const, you cannot skip the Volatile keyword. The two constitute c-v qualifier (so-called c-v limitation)

It is a pain in my heart to say this keyword: It was never understood by a cool man ~~ Home location!

Query and learn ...... In general, it tells the compiler "I don't know when it will change"

In other words, the declared things can be changed by external programs, and the compiler is prohibited from optimizing read/write operations.

It is rare to express an uncertain message with such certainty! But, this is not an excuse I don't know how to use it. It seems that I still need to practice it.

 

Sat. Static keywords

 

Defines static functions and static data. All objects of this class in the static data table maintain one data, which is equivalent to the global variables of this class and can only be accessed through the class and initialized to 0. Static functions implement functions that do not define the struct class of objects. Static functions can only access static data.

 

7. inline keywords

 

Inline functions to replace short functions and increase the speed.

In fact, this keyword has a deep connotation, which is similar to define in C, but there are also many differences, not just simple replacement effect.

I remember to discuss the functions of this function with some cool people, and I learned a lot from it ......

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.