Objects and classes (in) C ++

Class in which the default thing ......

 

1. Class 6 Default member function:

If a class is not what the members are referred to as empty class. Empty class do nothing? Not, any class in case we do not write, will automatically generate the following six default member function.

class Date{};

 

2. constructors:

Constructor, class name with the same name, the type of object to create a class called 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.

 Given by way of following constructors, parameters can be given a default value, may be overloaded constructors.

class Date
{
public:
    Date(int year = 2000,int month=1,int day=1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    Date d1;
    return 0;
}

A constructor is a special member function should be noted that, although the name is constructed, but note that the constructor is the main task of the constructor is not open space to create an object, but the object is initialized.
Having the following characteristics:
1, the same as the class name and function name.

2, no return value.

3, when the object is instantiated compiler automatically call the corresponding constructor.

4, constructors can be overloaded.

5, if no explicit definition of the class constructor, the C ++ compiler will automatically generate a default constructor with no arguments, once the user explicitly define the compiler will not be generated.

6, no-argument constructor and a full default constructor is called the default constructor, and can only have a default constructor. Note: no-argument constructor, the default constructor whole, we did not write the compiler-generated default constructor, it can be considered a default member function.

3. destructor:

Destructor: Instead constructor function, a destructor object is not complete destruction, local object destruction is done by the compiler. The object will automatically call the destructor when destroyed, some of the resources to complete the clean-up class.

Given by way destructor follows:

class SeqList 
{ 
public : 
    SeqList (size_t Capacity = 10 ) 
    { 
        _pdata = ( int *) the malloc (Capacity * the sizeof ( int )); 
        _size = 0 ; 
        _capacity = Capacity; 
    }

     ~ SeqList ()   // destructor no parameters 
    {
         IF (_pdata) 
        { 
            Free (_pdata); // free space 
            _pdata = nullptr a;   // pointer is set to null
            _capacity = 0;
            _size = 0;
        }
    }

private:
    int* _pdata;
    size_t _size;
    size_t _capacity;
};

int main()
{
    SeqList s1;
    return 0;
}

Destructor are special member functions.
The following characteristics:
1. The destructor name plus the class name ~ characters.

2. No parameter returns no value.

3. There is only one class a destructor. If not explicitly defined, the system will automatically generate a default destructor.

4. When the object end of life, C ++ compiler system automatically calls the destructor.

5. destructor on automatically generated compiler, will do some things? The following program we will see, the compiler generates a default destructor, to call its destructor custom type members.

4. The copy constructor:

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

The copy constructor is overloaded function constructor

class Date
{
public:
    Date(int year = 2000,int month=1,int day=1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    Date(constD & DATE) // copy constructor must be a reference type, traditional values will lead to infinite recursion 
    { 
        _year = d._year; 
        _month = d._month; 
        _day = d._day; 
    } 

Private :
     int _year;
     int _month;
     int _day ; 
}; 

int main () 
{ 
    a Date D1 ( 2018 , . 1 , . 1 ); 
    a Date D2 (D1); 
    d1.Print (); 
    d2.Print (); 
    return  0 ; 
}

Copy constructor is a special member function, which has the following characteristics:
1. a copy constructor overloads of constructors.

2. Parameter copy constructor only one and must use a reference parameter passing, use by value will lead to infinite recursive call.

3. If not explicitly defined, the system generates a default copy constructor. The default copy constructor objects stored by the memory copy is completed by byte sequence, we call such a shallow copy copy, or copies of the value.

5. Overload assignment operator:

Operator Overloading
C ++ in order to enhance readability of the code introduced operator overloading, operator overloading is a function of a special function name, and also has its return type, function names and arguments, the return type and argument list ordinary functions similar .
The function name is: access operator symbols need to override keyword operator behind.
Prototype: Return Value Type operator operator (parameter list).
Note:
can not be connected to other symbols to create a new operator: operator @, such as an overloaded operator must operate a number of enumeration type or class type for built-in types of operators, can not change its meaning, for example: a built-in int +, can not change its meaning.
Overloaded functions as members of a class parameter which looks less than the number of operands an operator member function has a default the this parameter, defined as the first parameter *, ::, sizeof,:. ?, Note that the above five operator can not be overloaded. This often occurs in multiple-choice questions in the written test.

Given by way of assignment operator:

class Date
{
public:
    Date(int year = 2000,int month=1,int day=1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    Date(constD & DATE) // must be a reference type, traditional values will lead to infinite recursion 
    { 
        _year = d._year; 
        _month = d._month; 
        _day = d._day; 
    } 
// Assignment operator overloading 
    DATE & operator = ( const DATE & D) // the return value to complete the continuous assignment 
    {
         iF ( the this = & D!)    // determines whether an assignment ourselves 
        { 
            _year = d._year; 
            _month = d._month; 
            _day = d._day; 
        } 
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1(2018, 1, 1);
    Date d2(d1);
    Date d3 = d2;
    d1.Print();
    d2.Print();
    system("pause");
    return 0;
}

Assignment operator mainly four points:
1. Parameter Type

2. Return Values

3. Detection ourselves whether the assignment

4. Return * this

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.

6. The default copy constructor and the assignment operator overloading problems:

The default copy constructor and assignment operator overloads are by copying the value of the byte sequence, if the class object has resource management, resources can not be completed copy. The solution is a deep copy.

7. const member functions:

const member functions of the class modified
to modified class member function const const member function call, const modified class member function, the member function is actually modified implicit this pointer can not indicate any member of the class member functions in modify.  

    void SetDate ( int year, int month The, int Day) const 
    {                     // compiling prompt unmodifiable 
        _year = year; 
        _month = month The; 
        _day = Day; 
    }

const object can not call non-const member functions.

Non-const object can call const member functions.

const member functions can not call non-const member functions.

Non-const member functions can call const member functions.

8. Take address and address-const operator overloading:

Both the default generally do not have to redefine member functions, the compiler will generate default. These operators generally do not need heavy-duty, using the compiler-generated default overloads can take the address, only special circumstances, only need to overload, such as want others to get to the specified content.

    Date* operator&()
    {
        return this;
    }

    const Date* operator&()const
    {
        return this;
    }

If the fetch address given operator overloading, but also to eat then fetch address const type operator overloading.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/fengkun/p/11871624.html