Object-oriented notes

Source: Internet
Author: User

1. Object-oriented

Object-Oriented Programming is summarized as one sentence: sending messages to objects.

The object-oriented program module consists of classes.

2. Class
(1) Each class can be defined as an interface and an implementation.

Interface: consists of the operations that the code of this type needs to perform. Implementation: includes the data required for this class.

(2) define data members

You Cannot initialize a data member (like a definition variable) as a part of its definition. You can only specify the name and type of the data member. Initialization can only be controlled through special member functions of the constructor.

(3) struct and class

In C ++, struct can also have public, private, and protect attributes. Struct members are public by default, while Class Members are private by default.

 

(4) private and protect

PRIVATE: only the creator of the class and the internal member functions of the class can access

Protect: the inherited structure is accessible.

(5) member functions

A member function is a class action and part of the implementation of program algorithms. It is the only way to operate encapsulated data. The declaration of a member function is in the class body. The implementation is outside the class definition and the class name must be specified. Ex:

Return_type calss_nane: func (type parameter)

{

Body

}

(6) inline member functions

Implicit Declaration: place the function subject directly in the class body.

Display Declaration: add the inline keyword when implementing a function that is not a class subject.

(7) object

An object is a specific object of this type. Declaration: Type object name

After declaring the class and its object, you can access the public members of the object: Object Name. Public member function name (parameter table)

(8) constructor

Is a special member function that defines how to initialize objects of this class. It will be automatically called by the system when the object is created.

The name of the constructor is the same as the class name, and no return value is returned.

If no constructor is defined, the compiler automatically generates a default constructor.

After the constructor is defined, you can declare and initialize the object as follows: Class Name Object Name (parameter list)

(9) copy constructor

It is a special constructor and has the characteristics of a General constructor. Its form parameter is a reference of this class object. It uses an existing object to initialize a new similar object.

If a copy constructor is not defined, the system automatically generates a default constructor, which copies the values of each data member of the initial object to the newly created object. Ex:

Class Point {

Public:

Point (INT x = 0, y = 0); // Constructor (must be implemented outside the class)

Point (point & P); // copy the constructor (must be implemented outside the class)

PRIVATE:

......

};

(10) destructor

Used to clear objects before they are deleted. The system automatically calls the object at the end of its lifecycle. If the display definition is used, the system automatically generates a default destructor that does nothing.

Ex :~ Clock (){}

(11) combination of Classes

A class is embedded with objects of other classes as members. Composite constructor definition:

Class Name: Class Name (parameter table): embedded object 1 (parameter table), embedded object 2 (parameter table ),...... {Class initialization}

The data members of this class can also be initialized like this. Ex: Class Name (form parameter table): Data member (value)

(12) Class templates

Declare a pattern for the class so that the parameters or return values of some data members and member functions in the class can be of any type. Example:

Template <class T> // class template: allows you to access any data type.

Class store {

PRIVATE:

T item; // item is used to store any data type.

Int value;

Public:

......

};

The member function of the template class must be a template function. Implementation of constructor by default

Template <class T>

Store <t>: store (void): Value (0)

{}

(13) static data member of the class

Common data members of a class have a copy in every object of the class.

The static data member of a class adopts the static declaration. Each class has only one copy, and all objects of the class are maintained and used together to share data between different objects. Static data members do not belong to any object, so they can only be accessed by "Class Name: identifier"

(14) static function member of the class

A. Call to common function members must pass the object name. Public static member functions can be called by class name or object name.

B. static member functions can directly access static data and function members of the class. accessing non-static data members must be accessed through the Object Name (troublesome ).

(15) youyuan

A. Friend functions: non-member functions modified by the keyword frined in the class declaration. A friend function is not a member function of this class. In its function body, you can use the object name secret class to private and protect members.

B. User meta: if Class A is a user Meta class of Class B, all member functions of Class A are user meta functions of class B and can access private and protected members of Class B.

Class B {

...

Friend Class;

...

};

(16) Inheritance and Derivation

Inheritance is a later feature of the new class obtained from the existing class. The process of generating a new class (derived class) from an existing class (base class) is derived. Declaration example of a derived class:

Class drive: Public base1, private base2 // drive of the derived class inherits the base class base1 and base2 in the form of public and private respectively.

{

Public:

Drive (); // Add class Constructor (constructors and destructor in the base class cannot be inherited)

~ Drive ();

};

A. process of generating a derived class:

Absorbs base class members, modifies base class members, and adds new members.

B. Access Control:

Public inheritance, private inheritance, and protection inheritance. (In any case, the Private Members of the base class cannot be accessed through the derived class)

C. Scope identifier ":":

Resolves the ambiguity of access to base class members with the same name.

Drive.info // class member accessing dirve

Drive. Base: info // class member accessing the drive base class

D. virtual base class

Set the common base class to a virtual base class. In this case, the data member with the same name inherited from different paths has only one copy in the memory, and the same function name has only one ing. This solves the unique identification problem of members with the same name.

If a non-default constructor is defined in the virtual base class, the initialization of the virtual base class must be listed in the member initialization table of the constructor in all derived classes. (When an object is created, only the constructor of the farthest derived class calls the constructor of the virtual base class)

(17) polymorphism

Polymorphism means that the same message is completely different when it is received by different types of objects. Include: Heavy Load polymorphism, forced polymorphism, inclusion polymorphism and parameter polymorphism.

A. Operator Overloading (essentially function overloading)

Expression: Return Operator (parameter table ){}

When an overloaded function is a class member, Its Parameter seems to be 1 less than the number of operands. The operator used as a member function has an implicit this parameter, which is limited to the first operand.

B. Virtual Functions

Virtual functions are virtual modified member functions (constructor cannot be declared as virtual functions) to achieve polymorphism.

C. Pure virtual functions

A pure virtual function is a virtual function declared in the base class without a function body (that is, the implementation part of the function is no longer given in the base class ), it does not define specific operation content in the base class, and requires each derived class to define its own version as needed.

D. abstract class: class with pure virtual functions

(17) This pointer

Implicit pointer to this class object.

 

 

 

 

 

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.