C ++ object-oriented programming study notes (4)

Object class (2)

string class

C ++ representation only backwards compatible character C, but also a more convenient statement string type, i.e., the string class.
Want to use the string class, you must include the header file string, that is, to declare

#include<string>

The method of statement string Like other objects custom, may also need to use a definition of the
string class in addition to many operators particularly visible

Usage summary of the standard C ++ string class

Function to pass the object

Using the object as a function parameter: This method is consistent with the normal use of the method to pass parameters to the function, modification of the object in the object itself does not affect the function.
Object pointer as a function parameter using: a method consistent with the normal use of the method to pass parameters function pointers, the object modification affects the function of the object in itself.
Object reference as a function parameter: pointer using the method consistent results, but more simple, direct, for example:

void fun(tmp &a) {}

Assignment and copy objects

Object assignment

Use operator '=' to the value of the data member is assigned to the previous operator object.
Note: The
types of the two objects must be the same.
After the assignment is still separate the two objects, one object changes will not affect the other.
When there is a pointer object, the method can not be used directly for evaluation.

Copy constructor

Action copy constructor is used in an existing object on a newly created object initialization
features
(1) does not return value
(2) only one parameter, and is a reference to the same object
(3) for each class must have a copy constructor, the copy constructor can be defined by its own, if not defined, the system will automatically generate a default copy constructor.

Custom copy constructor

Custom copy constructor general form as follows:

类名::类名(const 类名 &对象名)
{
    //拷贝构造函数的函数体
}

The general form of copy constructor call is:

类名 对象2(对象 1);

You can also call the copy constructor assignment method:

类名 对象 2=对象 1
The default copy constructor

If there is no definition of the system will automatically generate a default copy constructor, an identical copy of a new object.
Note: If the pointer type can not present a default copy constructor.

Call the copy constructor case

When an object (1) is used to initialize another object class.
(2) the target parameter is a function of the class, and calls the function parameter arguments when combined.
(3) function returns the value of the object class when the back of the function call returns a value of the function call is completed.

Static member

Static data members

In a class, if a data member described as static, such members are called static data members, no matter how many objects of class establishment, have only one copy of a static data idiom.
Idiom defined static data format is as follows:

static 数据类型 数据成员名;

NOTE:
Initial (1) static data member to be separately outside the class, and is performed before the definition of the object, initialize the following format:

数据类型 类名 ::静态数据成员=初始值;

(2) static data members belong to the class, so you can use "class name ::" access to static data members in the following format:

类名::静态数据成员名

(3) public static data members can be accessed before the object definition. After the object definition, you can also be accessed through the object, the following format:

对象名.静态数据成员名
对象指针->静态数据成员名
Static member function

If a member function described as static, such members are called static member function. Static member function belongs to the class as a whole is shared by all objects in the class member functions, not part of an object class, define static member functions in the following format:

static 返回类型 静态成员函数名(函数表);

There are several ways to call a static member function:

类名::静态成员函数名(实参表)
对象.静态成员函数名(实参表)
对象指针->静态成员函数名(实参表)

Note:
(1) In general, static member function is mainly used to access static data members.
(2) static member function can be used to create any objects before calling the static function idiom to deal with static data members.
(3) static member functions are part of a class, and not part of the object, thus calling static member functions outside the class preferable to use the following format:

类名::静态成员函数名()

(4) static member function of the same name does not conflict with other file functions.
(5) is not this static member function pointers, if you need to access the object's non-static members, need to pass the object name (object pointer, object references).

Friend

Friend is an aid for private members of the class of the external access type, which either do not belong to any class of non-member functions, can also be a member function of another class, it is independent of the external current class function.
When the class is declared a friend function, it is necessary before the function name keyword friend, can be defined inside or outside of class class.

The function is declared as non-member friend function

Example:

class tmp
{
    private:
        int a,b;
    public:
        tmp(int _a,int _b):a(_a),b(_b){}
        friend void show(tmp &t)
        {
            cout<<t.a<<' '<<t.b<<endl;
        }

};

int main()
{
    tmp t(1,2);
    show(t);
    return 0;
}

Note:
(1) a friend function is not a class member function, so no need to "class name ::" outside the class to access a friend function.
(2) a friend function is not a member of the class, so the data can not directly access the object's idiom, nor can this pointer to the object name must be passed by parameter (object pointer, object reference) to access reference data members of the object.
It must be added the name of the object (3) access.
Note friend function concealment break encapsulation and data type, and therefore need to be cautious friend function.

The member function as a friend function declaration

Member function of a class can be a friend of another class.
Note:
(1) member function of a class before becoming a friend of another class of functions, you must first define the class, the class name to add class member function when the function is located in a statement this friend.
(2) Failure to declare may add "class declaration class;" in front of the class, ahead of the statement.

Tomomoto类

A class can also be a friend, a statement of the method is to add another class in another class declaration statement friend 类名.
When the class Y is described as a friend of class X, all member functions of class Y have to be a friend function of class X, all of which means that all member functions as a friend class Y can have access to the class X member.
Note: friendship is one-way, non-commutative nor transitive.

The combination of class

Embedded object in another class as a class data member, referred to as a composite class. Embedded object called object members, also known as sub-objects.
When you create a class object that, if the class of an object with an embedded member, then the embedded object members will also be created automatically.
In general this class constructor is defined in the form:

X::X(形参表 0):对象成员 1(形参表 1),对象成员 2(形参表 2)......
{
    //类X的构造函数体
}

When calling the constructor X :: X (), according to the order of the objects in the class declaration of the member once they are called constructors, and finally bank execution constructor class X, the opposite order of the objects initialization calls the destructor.

Chang type

Type refers to the type often used const modifiers described, the value of which is variable or object member during the program run is immutable.

Often quoted

When using the modifier const reference in the description, the reference is the reference constant, as changes to the parameter argument will not be described in the form of:

const 类型 &引用名;
Often the object

When using the modifier const in the description of the object, the object is the constant object of its data members will not change for the life of the object, indicating the form of:

const 类型 对象名[(参数表)];
类型 const 对象名[(参数表)];

Often the object must be initialized and can not be updated

Often target members

Data members often
use the modifier const data members described often referred to as data members, often members of the data can only be initialized by the member initialization list the data members, it can not be any other function assigned

Member function is often
used out modifier const member functions often called member function described in the following format:

类型说明符 函数名 (参数表) const;

const is an integral part of the function type, should have the const keyword in the function declaration and definition of functions, no need to add const when calling.
Note:
(1)

Data members Ordinary member functions Often a member function
Ordinary data members Can access, you can also change the value Access to, can not change the value of
Often data members Access to, can not change the value of Access to, can not change the value of
Data members often object Does not allow access, the value can not be changed Access to, can not change the value of

(2) member function is often the only external interface often object

Guess you like

Origin www.cnblogs.com/springfield-psk/p/11837201.html