Understanding classes and objects

Classes and Objects

c is a procedural language focus process-oriented, c ++ language as an object of attention an object-oriented, things split into different object, the object is achieved by the interaction.

An early understanding of classes and objects

1. The introduction of class
class class keyword extraction, before the structure is similar, except that the structure can define a function in vivo function can be defined in the class la (c ++ structure may be defined in function oh)
2. Class define
two definitions fashion class member function: You can define a class definition, but the compiler is likely to be treated as an inline function to handle it recommend the second - to separate the declaration and definition of the function.
3. class access modifier
public, protected, private ---> protected and private modified objects can not be directly accessed outside the class
class class default is private, struct are public by default
object-oriented three characteristics: encapsulation, inheritance, state, where we talk about the definition of the package: the data and methods of data combine operation, the hidden object properties and implementation details, only publicly interface and interact with the object.
4. Class scope
class defines a new scope, to access class members to use a scope qualifier :: visit.
Here we have contacted the four scopes: global scope, local in scope functions, classes, namespaces.
Examples of the (target) Class 5
Class defines those members of the class is no open space, creating a class object with the only real memory space.
Here we need to think about a problem, how to calculate the size of a class of it? ? Class member functions can be included, then how should we do? ?
By testing we can know the size of the class is actually the size of the member variable of the class, class member functions are placed in the public area codes, pay attention to the problem of memory alignment. If it is an empty class, or the class member function only, then the size of what this class is 1, because there must be distinguished when creating objects.
6.this pointer
c ++ for each member function adds a pointer to the default parameters, so that the pointer to the current object (the object of the function call function is running), the operation of all member variables are accessed via the pointer to the function body. But all operations are transparent to the user, i.e. the user does not need to be passed, the compiler automatically. Why we do this is equivalent to the user's own incoming address at the time of mass participation, precisely because of the existence of this function pointer so in order to accurately access to call its body.

  • Characteristics of this pointer
  • this pointer Type: Class Type * const, this pointer points to it is not modified.
  • this parameter as a function pointer, the function call is passed to the address of the object this parameter, so that the object does not store this pointer

    II. Class default constructor function of

    A class definition is good which is not nothing, it will automatically generate the six default function
    1. Constructors
    A constructor is doing it?
    A constructor is a special member function with the same name as the class name, create a class called when an object of type automatically by the compiler to ensure that each data member has an appropriate initial value, and called only once during the lifetime of the object. That constructor object initialization is completed, which is characterized by: the class name and the same function name, no return value, the compiler automatically call the corresponding constructors object is instantiated, the constructor can be overloaded. The system automatically generates a no-argument constructor, of course, we can write a constructor with parameters, then we can pull directly initialized in the definition of the object time, look at the following code:
    class data<br/>{<br/>public:<br/>data(int year=1999,int month=1,int day=1)<br/>{<br/>_year = year;<br/>_month = month;<br/>_day = day;<br/>}<br/>private:<br/>int _year;<br/>int _month;<br/>int _day;<br/>};

Well, in this place we've had to create a constructor to initialize the object you can do it directly when the main function within the definition of an object:
the Data A (1999,10,13);

So we know that the system is no reference to the constructor, so we do not need to bring in a no-argument constructor defined time (), otherwise it becomes a function declaration.
The above code is the constructor we explicitly created when its existence when the compiler will not generate a default constructor

Another point to note: no argument constructor and full default constructor are known as the default function, is allowed to repeat as defined, allows only one, so that the two are not constitute overloaded function.

What role does it constructor is it? -> because the variable type is not just fixed int or char such, there may be user-defined type, then the constructor will call this custom type member function, which is its role.

2. Constructors initial value
above process is not in operation initialization constructor body, then we are used to initialize with constructor initialization list.

Initialization list: start with a colon, followed by a list of data members to a comma-separated, following each "member variables" in with an initial value or expression in parentheses.
public:
Data (int year = 1999, = month The int. 1, int. 1 = Day)
: _year (year)
, _month (month The)
, _day (Day)
{

}

Note: 1. Even without the initialization list to initialize the system will automatically generate a list of each variable is initialized only once, and when the member variable is a reference type, const modified class type member variable (no default constructor) these three cases when you must use the initialization list to initialize it, when there is no default constructor of a class type member variable without initializing the system will not automatically recognize how to initialize, it will go wrong.

2. The members of the initialization sequence variable is declared in accordance with the order of the member variables of the decision, regardless of the order initialization list, then we try to order a statement of the same in the initialization list, not to use variable assignment, very prone to error.

  1. explicit keywords
    found in c ++ in an interesting phenomenon, data a = 100; This statement can actually compile the different sides of the equal sign operand type but can be compiled, so we can look at this copy overloaded call when the process, been to testing we found during the assignment operator first calls a constructor when overloaded, to construct a 100 class! Therefore, not only the role of the constructor configured for a single parameter (argument) configuration and the types of conversion action function.
    But sometimes we do not want this happening, with explicit constructor will be modified before the ban this type of transformation.


3.static members

in c ++ can modify the static member variables and member functions, modified by the static variable or function called a static member variables or static member function
static objects modified for all classes shared!

Difference:
| static member variables | ordinary member variable |
| initialization outside class | class initialization |
| all shared objects | Each object contains a |
| name :: class can access the static member by name | only by Object access |
| all shared objects | each object contains a |

Static member variables will not affect the size of the class, that is, static members do not need to calculate the size of the calculation of the size of the class, they are using the same memory space

| Static member function | ordinary member functions |
| no hidden this pointer (not access non-static members) | have this pointer |
| const can not be modified (as modified const member functions are actually modified when this pointer) | may be |
| by calling the object can not | be invoked through objects |

With a static modification of static member functions can only access static member variables can not be accessed ordinary member variables.
The ordinary member functions are accessible.

II. Destructor

Destructor function corresponding to the constructor, and the constructor function overloading configuration, that is the function name is the class name, in front of the function name ~ for destructor, this function has no parameters and return values, not destructor the class object for destruction, but some resources to complete the clean-up class. The object will automatically call the destructor at the end of the life cycle.

III. Copy constructor

The copy constructor: only a single parameter, the parameter is a reference to an object of the present class type (const modified commonly used) , it is automatically invoked by the compiler in creating a new object with existing objects of class type.

Note: The copy function is an overloaded constructor for its only parameter and must be a reference to the ship, if the call-by will be infinite recursion.

There exists then we can define a new object with existing objects in the object defining this function:
such as: date d1; data d2 (d1 );

Similarly, the compiler will generate for us a default copy constructor, but when it completed a shallow copy of the work, such as the following circumstances:

class String
{
public:
    String(const char* str = "jack")
    {
        _str = (char*)malloc(strlen(str) + 1);
        strcpy(_str, str);
    }

    ~String()
    {
        cout << "~String()" << endl;
        free(_str);
    }
private:
    char* _str;
};

int main()
{
    String s1("hello");
    String s2(s1);
}

In the constructor we completed the creation of dynamic memory space, the default copy constructor will be intact to this address copied to the new object d2, which is not the result we want, d1 and d2 share the same a memory space to be modified d1 d2 will change. This is called a shallow copy, so we need to have a constructor customize a dark copy. In doing this late introduction.

IV. Assignment operator overloading

Before talking about heavy-duty assignment operator is necessary for us to talk about what's operator overloading, then the two classes are not comparable size, but if we perform operator overloading, in accordance with the size of the class is also a relationship overloaded then use the comparison operators other operators is the same.

Calculates overloading operator keyword
with a special type of operator overloading, is the front and rear ++ ++ overloads to differ

Assignment Operators There are four points:

  1. Parameter Type
  2. return value
  3. Detecting whether the assignment ourselves
  4. Return the this // the this as a return value to solve the problem of continuous assignment
  5. If a class is not explicitly defined in the assignment operator overloading, compiler also generates a complete byte order by value object is a copy.

Also completed is a shallow copy, so we also need to realize himself an assignment operator overloading. Achieve deep copy is also on the back it said.

The default function is to take the last two address common variable and const variables, both of which rarely achieve their own.

IV. Friend

If we overload the output of the operator "<<" how to do it
ostream & operator << (ostream & _cout ) ------> This is a << overloaded function function, if such a function definition, then this pointer contained therein as the first parameter, so to call functions intended to output a class d can not be output in this manner according to conventional cout <<, the first parameter is a function of this pointer, the call to d << cout, so as to normal output.
So how can we avoid this way instead of << overloaded according to the conventional way of doing things

Here will we have an overloaded function of the two parameters passed in accordance with the normal order of global scope definition, but you can not can not refer to this private member function in the class, as long as we declared in the class with a friend this function then the function key modification is the friend function of this class will be visited in the class

Note: The
friend function is a member function is not an ordinary class
private member friend functions can access the class, but not a class member functions
friend function can not be modified by const (const modified because this is a pointer, and a friend this function is not a pointer)
friend function declaration can be limited in any place of the class definition, not class qualifiers access
a friend function function can be multiple classes of
the same principle calls and calls with the ordinary function of a friend function

Also a class can also be a friend of another class, the class can access private members of its internal, but note that this process is one-way and can not be passed.

Guess you like

Origin blog.51cto.com/14239789/2439354