MOOC C ++ Notes (2)

Second week: base classes and objects

Four basic characteristics of the object-oriented programming

Abstraction, encapsulation, inheritance, polymorphism.

Object-oriented programming process

1. From the objective things abstract class
abstract things with the member variables and member functions (functions with similar structure)
member variables and member functions are collectively referred to as members of the class.
By class, we can define variables, class variables defined out, we called objects.
2. Memory allocation object
the size of a member of the space occupied by the object is equal to the size of all the member variables and.
The space occupied by the object does not contain member functions.
Each object has its own memory space, member variables of an object is changed, it will not affect the other.
Operation between the object 3.
Like the structure variable assignment between objects may be, but not using>, <, ==,! =, Etc. operations, unless the operator is overloaded.
4. The member variables and member functions of the class using the
usage a: name of the object member.
Usage II: Pointer -> Member Name
Usage Three: A reference member name
5. Definition of classes and member functions of the class can be written separately from
the class name :: Function name

int A::sum(void){ }

6. Class members can access a range of
accessible range in the definition of class, with the following scope of access, use keywords to explain the scope of the following access classes:
Private: Private members of the
public: public members
protected: the protection of members of the
three keywords the frequency of use and the order does not limit the
mechanism set up private members, called hidden.
7. Members may also function overloading and default
see above a blog

Constructor

basic concept

One member of the function, but no return value:
1 Function name as a class, may have parameters, you can not have a return value;

2. The role is to initialize the object, such as the initial value assigned to the member variables

3. If you do not write the definition of the class constructor, the compiler automatically generates a no-argument constructor does not make sense.

4. If the constructor defined, the compiler does not generate the constructor.

5. The object generator constructor is invoked automatically. Once the object generation, can not duplicate configuration.

6 may be a plurality of class constructors

Why Constructors:
1. constructor executes necessary initialization, the constructor will not have to write special initialization function than their own.

2. Using an uninitialized object will cause an error.

Constructor in the array

A parameter to use {a, b, c};

And to use two or more - {A (a, b), A (a, b)};

Copy constructor

concept

Only one parameter, namely a reference to the same object.
The form X: X (X &) or X :: X (const X &) , choose between them, or can be in a constant object as a parameter.
If you do not define the copy constructor, the compiler will generate their own.
If you define your own copy constructor, the default copy constructor does not exist.
This copy constructor does not allow X :: X (X).

Copy the three cases constructor function

1. When an object is used to initialize another object of the same kind.

2. If a parameter is a function of the object class A, then the function is called, the copy constructor class A will be called.

3. If the return value of the function is the class of object A, when the function returns, A copy constructor is called.

Note: Assignment between objects does not lead to the copy constructor is called.

Use constant reference parameters: If the parameter is a function of the object, then it must be used when calling the copy constructor, large overhead, in order to solve this problem, we can use the reference object, if you do not want the value of the object is referenced within the function , you can use const.

Type conversion constructor

concept

Object constructor is defined to achieve conversion type automatic conversion.
Only a constructor parameter, and not the copy constructor, can usually be seen as a conversion constructor.
When necessary, the compiler will automatically call type conversion constructor to generate an unnamed temporary object

Destructor

~ Type called class destructor.
A class can have a destructor.
Destructor do remedial work before the object die.

If no destructor defined class, which the compiler automatically generates a default destructor, but this function does nothing.

An array of objects at the end of the lifetime of each array member will call the destructor
automatically calls the destructor when the delete function
destructor in an object returned as a function return value after calling

Guess you like

Origin www.cnblogs.com/z-y-k/p/11484207.html