C ++ learning Summary

Source: Internet
Author: User

I. constructor and destructor

C ++ functions written behind the scenes for you: A copy constructor, a value assignment operator, a destructor, and a pair of access operators. In addition, if you do not declare any constructor, it will also declare a default constructor for you. All these functions are public. In other words, if you write:

class Empty{};

This is the same as writing:

Class empty {public: Empty (); // default constructor empty (const empty & RHs); // copy constructor ~ Empty (); // destructor ---- whether it is a virtual function. See the following description for empty & operator = (const empty & RHs); // value assignment operator empty * operator &(); // address retrieval operator const empty * operator & () const ;};

Deep copy and shallow copy:

By default, all the copy constructors are shortest copies. However, a class may have other resources. If the constructor allocates a heap memory and the destructor releases the memory, a deep copy is required, deep copy cannot be implemented by the compiler. Declare a copy constructor and a value assignment operator for classes that require dynamic memory allocation.

Call the copy constructor:
1. When another object of the class is initialized with an object of the class.
2. When an object is passed as a real parameter of a function to a function.
3. When the return value of a function is a class object, it is returned after the function is executed.

When another constructor is called in the constructor, a temporary object is generated and released immediately.
String c = A; only the copy constructor is called. String C; C = A; Calls constructor and value assignment function respectively.

Notes for constructor and destructor:
1. constructor and destructor cannot return values.
2. constructor and destructor can be explicitly called.
3. Copy (copy) constructors cannot be passed with values
4. Do not throw an exception or call a function with an exception in the constructor or destructor. Memory leakage may occur!
5. Determine whether the base class has a virtual destructor

Ii. Static, const, youyuan and virtual functions

1. static declarations are used by static members. There will always be only one instance in the memory (static variables, in-class declarations, and out-of-class definitions)
2. It is common to class objects.
3. static member variables can be accessed by member functions, but static member functions can only access static member variables.
4. youyuan allows a common function to directly access the protection or even private member mechanism of a class.

Virtual functions:
Add the virtual keyword before a common member function.
If a function declares a virtual field in the base class, all the derived classes are virtual.
A function is a common function in the base class, and a function defined as virtual in the derived class is called a bitwise function.

Abstract class:
Classes with pure virtual functions are abstract classes.
Abstract classes cannot be instantiated, so they can only be applied as pointers.
Abstract classes can prevent slice from occurring.
Abstract classes do not generate virtual tables.

Const object and member:
1. the const object can only access the const member function, but not the const object can access any member function, including the const member function.
2. Members of the const object cannot be modified. However, objects maintained by the const object through pointers can be modified.
3. the const member function cannot modify the data of an object, regardless of whether the object has the const nature.
4. However, the data member with the mutable modifier can be modified by any means under any circumstances. In this case, the const member function can be modified.

Iii. Design and Implementation

An important rule in C ++ object-oriented programming is: Public inheritance means "being ". Remember this rule.

C ++ has two types of polymorphism:
1. polymorphism at the time of compilation. polymorphism at the time of compilation depends on function overload or template implementation.
2. Run-Time polymorphism. Run-Time polymorphism depends on the virtual function interface.

Reference Usage principles:
1. Do not use pointers when reference is available.
2. The reference can be used as the "Left value"
3. The reference cannot be blank. If an object is empty, a pointer must be used. The reference points to a null value, which is very harmful!
4. Try to use "Pass reference" instead of "pass value"
5. Do not try to return a reference when an object must be returned
6. Do not return a reference to a local object or a reference to a pointer initialized with new in the function.

Class has const data, must have a constructor to initialize it.
Static const double A; // static double constant declaration!
Const double class name: A = 1.04; // static initialization!

T & operator [] (INT index); // returns an element of the array, which is readable and writable.
Const T & operator [] (INT index) const; // returns an element of the array, which is readable and cannot be written.

To identify whether a function is overloaded:
(1) only function parameters can be used to differentiate heavy-duty functions (types, numbers, and default parameters)
(2) the return value of a function cannot be used to distinguish between overloaded functions.

Iv. Differences between C ++ and C

The difference between struct in C and struct in C ++:
The struct of C ++ can be used as a class. The difference is that the Members in class are private by default, while the Members in struct are public by default.
In C, struct can only be a collection of some variables. It can encapsulate data but cannot hide data, and the member cannot be a function.
Struct in C is a user-defined data type (UDT), and struct in C ++ is an abstract data type (ADT). It supports the definition of member functions.

C ++ language guarantee. If P is null, delete p does not do anything.
Delete P is a two-step process: Call the Destructor and release the memory.
Delete P calls operator Delete (void *), while Delete [] P calls operator Delete [] (void *).

Static keywords have at least the following functions:
(1) The static variable in the function body applies to this function body. Unlike the auto variable, the memory of this variable is allocated only once, therefore, the value remains the value of the previous time during the next call;
(2) The static global variables in the module can be accessed by the functions used in the module, but cannot be accessed by other functions outside the module;
(3) The static function in the module can only be called by other functions in the module. The scope of use of this function is limited to the module that declares it;
(4) static member variables in the class belong to the entire class and only one copy of all objects in the class;
(5) The static member function in the class belongs to the whole class. This function does not receive the this pointer, so it can only be a static member variable of the category.

The const keyword has at least the following functions:
(1) to prevent a variable from being changed, you can use the const keyword. When defining the const variable, you usually need to initialize it, because there will be no chance to change it in the future;
(2) For pointers, you can specify the pointer itself as const, or you can specify the data referred to by the pointer As const, or both of them as const;
(3) In a function declaration, const can modify the form parameter, indicating that it is an input parameter and cannot change its value within the function;
(4) If the member function of the class is specified as the const type, it indicates that it is a constant function and the member variables of the class cannot be modified;
(5) For a member function of the class, you must specify its return value as the const type so that its return value is not the "Left value ".

Implementation of a base class and Its Inheritance class:

class Base{    public:    Base(const char *s=NULL);    Base(const Base& rth);    Base & operator=(const Base & oth);    virtual ~Base();    private:    char *m_data;};Base::Base(const char* s){    if(s==NULL)    {        m_data=new char[1];        m_data[0]='\0';    }    else    {        int length=strlen(s);        m_data=new char[length+1];        strcpy(m_data,s);    }}Base::Base(const Base & oth){    int len=strlen(oth.m_data);    m_data=new char[len+1];    strcpy(m_data,oth.m_data);}Base & Base::operator=(const Base & oth){    if(this==&oth) return *this;    delete []m_data;    int len=strlen(oth.m_data);    m_data=new char[len+1];    strcpy(m_data,oth.m_data);    return *this;}Base::~Base(){    delete[] m_data;}class Derived : public Base{    public:    Derived(const char* s=NULL,int a=0);    Derived(const Derived& oth);    Derived& operator=(const Derived& oth);    private:    int m;};Derived::Derived(const char* s,int a):Base(s),m(a){ }Derived::Derived(const Derived& oth):Base(oth),m(oth.m){ }Derived& Derived::operator=(const Derived& oth){    if(this==&oth) return *this;    Base::operator=(oth);    m=oth.m;    return *this;}

The C ++ compiler does not support template header files and code separation compilation.

In a separate compilation environment, the compiler compiles a file. the CPP file does not know another one. CPP file exists, and it will not be searched (when there is a pending symbol, it will be sent to the connector ). This mode works well without a template, but it does not work when it comes to a template, because the template is only available when needed. Therefore, when the compiler only sees the template declaration, it cannot present the template. It can only create a symbol with an external connection and expects the connector to determine the address of the symbol. However, when this template is implemented. when the template is not used in the CPP file, the compiler is too lazy to use the template. Therefore, the entire project. OBJ cannot find a line of binary code with the template as the entity, so the connector is also silly!

Related Article

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.