C + + 14th class object oriented

Source: Internet
Author: User

First, process-oriented programming

The program is mainly composed of a function.

There is a close correspondence between the calling procedure of the function and the algorithm's solving steps.

Second, object-oriented programming

The program consists of classes.

When the program runs, multiple "objects" collaborate with each other to accomplish various functions.

"Objects" usually correspond to things in the physical world.

III. Classes and objects

1. Class definition: Class is similar to struct, and is a custom data type. Class typically contains data members and method members. The general form is as follows:

Class Name {

Public

Type data members;

Type method member ();

Private

Type data members;

Type method member ();

};

Data members (also called member properties) are used to describe the properties of an entity.

The method member (also called the member function) corresponds to the action applied to the property.

2. Definition of object: The effect of defining an object is to allocate a piece of storage area to the object in memory.

Each data member occupies a portion of the object's storage area.

Each method member is stored in a common area of the class, and each object does not have to be stored separately.

3. Use of objects: through the member operator "." Access data members and method members.

4. A pointer to an object refers to a member by "."

5. Total members and private members:

Public members that are publicly qualified can be accessed either by member functions or outside the class, and private members under private qualification can only be accessed by member functions.

6. Out-of-class definition member functions:

float Student::averagesocre ()//:: Scope qualifier

Four, the constructor function

1. Constructors are member functions that are responsible for object initialization. Features are:

The function name is the same as the class name;

It is automatically executed once when the object is defined and cannot be called directly;

Does not return any values;

If there are no constructors in the class definition, the compiler automatically generates a constructor. The function body of this constructor is empty and has no parameters.

2. You can initialize a data member with a parameter initialization table.

Box∷box (int h, int w, int len):

Height (h), Width (w), Length (len) {}

3. function with name overload

Five, the destruction function

1. Destructors are functions that are automatically executed when an object is disposed.

The name of the destructor is "~" before the class name;

A class can have only one destructor with no arguments.

Vi. inheritance and derivation of classes

The so-called "inheritance" is the creation of a new class (called a derived class or subclass) on the basis of a defined class (called a base class or parent class). The definition of a derived class is as follows:

Class Derived classes Name: Public base class name

{

Newly added members of derived classes

Type data members;

Type method member;

} ;

Attention:

(1) A derived class cannot access a private member of the base class;

(2) The public in "class Master:public Student" may be replaced by private, protected;

(3) Derived classes can be inherited by lower-level derived classes, resulting in multilevel inheritance.

Vii. Summary

1. Object-oriented programming has 4 main features:

Abstraction is generalization and generalization

Encapsulation classes are the encapsulation of data and operations

Inheriting derived classes inheriting base classes

Polymorphism

Benefits: Enhances reusability and reduces programming effort.

C + + 14th class object oriented

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.