Chapter 1 Object-Oriented Programming (1)

Source: Internet
Author: User

Object-Oriented programming is based on three basic concepts: data abstraction, inheritance, and dynamic binding. In C ++, classes are used for data abstraction, and classes are derived from one class to another: Derived classes inherit the members of the base class. Dynamic binding enables the compiler to determine whether to use the functions defined in the base class or the functions defined in the derived class at runtime.
Object-oriented programming (OOP)
15.1 Object-Oriented Programming: overview
1. Inheritance
A derived class can inherit the Members defined by a base class. A derived class can use operations that are irrelevant to the specific features of a derived type without any change, A derived class can redefine the member functions related to the derived type, feature the function, and consider the features of the derived type. Finally, in addition to the members inherited from the base class, the derived class can also define more members.
Classes associated with inheritance constitute an inheritance level (inheritance hierarchy ). One class is called the root, and all other classes directly or indirectly inherit the root class.
In C ++, the base class must specify which functions you want the derived class to redefine. A function defined as virtual is the base class that is expected to be redefined by the derived class, the function to be inherited from a base class cannot be defined as a virtual function.
2. dynamic binding
In C ++, dynamic binding occurs when a virtual function is called through a reference (or pointer) of a base class. A reference (or pointer) can point to a base class object or a derived class object, which is the key to dynamic binding. The virtual function called by reference (or pointer) is determined at runtime. The called function is defined by the actual type of the object referred to by reference (or pointer.
15.2 define base classes and derived classes
15.2.1 define the base class

Class Item_base
{
Public:
Item_base (const string & book = "", double sales_price = 0.0): isbn (book), price (sales_price ){}
String book () const {return isbn ;}
Virtual double net_price (size_t n) const
{
Return n * price;
}
Virtual ~ Item_base (){}
Private:
String isbn;
Protected:
Double price;
};
1. base class member functions
The reserved word virtual is used to start dynamic binding. The Member is a non-virtual function by default, and the call to the non-virtual function is determined during compilation. To prove that a function is a virtual function, add the reserved word virtual before the return type. Except constructors, any non-static member function can be a virtual function. The reserved word virtual only appears in the member functions inside the class, and cannot be used in the function definitions that appear outside the class definition body.
In general, any function to be redefined in a derived class should be defined as a virtual function.
2. Access Control and inheritance
The public and private labels have common meanings: User code can be a public member of the category class but cannot access a private member. private Members can only be accessed by members of the base class and friends. The permission of the derived class to access the public and private Members of the base class is the same as that of any other part of the program: it can access the public member but cannot access the private member.
A protected member can be accessed by a derived class object but cannot be accessed by a common user of this type.

From xufei96's column

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.