C + + Basic Grammar notes

Source: Internet
Author: User


Seventh Chapter

Functions are declared before they are called.

New and delete can be used to allocate memory and release. Although malloc and free are also reserved, it is not recommended.

The general format used by the delete operator is the delete [] pointer variable

Sometimes you need to put several different types of variables into the same memory unit, you should use union. The general form of a declaration is:

Union type name {member table column};

Enum type: enum Weekday{sun,mon,tue,....}

After declaring an enumeration type, you can use it to define variables. such as weekday workday, Week_end;

typedef int INTEGER;

typedef float REAL;

Eighth Chapter

If neither private nor public is specified in the definition of the class, the system is implicitly considered private. ,

To summarize the above declaration of the class type, you can get its general form as follows:

Class

Class name

{

Private

Private data and member functions

Public

Common data and member functions

};

To reduce the time overhead, C + + automatically handles these as built-in functions if they do not include control structures such as loops in the member functions defined in the class body.

This member function is specified as a built-in function only if the member function defined outside the class is small and the frequency of the call is high.

Each of these objects consumes only the storage space occupied by the data portion of the object, not the storage space consumed by the function code.

The declaration of a class is included in the header file, so the class can be used to define the object in the program. Since the declaration of the member function is included in the class body, the


The public member functions of these objects can be called in the program. In order to conceal the information described in the previous section, the definition of a class member function is generally not placed in the


Header file, and the other in a file.

Note: Student.h is placed in the user's current directory, so it is wrapped with double apostrophes on both sides of the file name ″student.h″ without the angle brackets.


<student.h>

Otherwise, the file will not be found at compile time

A library consists of two components: a class declaration header file, a definition of a member function that has been compiled, and a target file

Users only need to load the class library into their own computer system (typically installed in the subdirectory of the C + + compilation system), and in the program with the Include command line


You can use these classes and their member functions to run the program successfully by including the header files for the class declaration.

Nineth Chapter



Constructor: Slightly

You can also use the following form:

Box::box (int h, int w, int len): Height (h), Width (w), Length (len) {}

Box (int h=10,int w=10,int len=10); Specifying a default parameter when declaring a constructor

destructor: ~

The function of destructors is not to delete objects, but to undo objects

Occupy the memory before doing some cleanup work to make this part of the memory

Can be used by programs to assign to new objects.

The function of the destructor is not limited to releasing resources, but it can also be used to execute the user's desired execution after the last use of the object.


Any action

If a static local object is defined in a function, the constructor is called only once when the program first calls this function to establish the object, at the end of the call, and


Does not release, and therefore does not call the destructor, only the destructor is called when the main function ends or the exit function is called to end the program.

The fruit constructor has only one parameter, and you can provide the arguments directly within the curly braces after the equals sign when you define the array.

Tudent stud[3]={//Defining an array of objects

Student (1001,18,87),//Call the constructor of the 1th element, giving it 3 arguments

Student (1002,19,76),//Call the constructor of the 2nd element, giving it 3 arguments

Student (1003,18,72)//Call the constructor of the 3rd element, giving it 3 arguments

}

A pointer variable that defines a point to a member function should take the following form:

void (time:: *P2) ();

You can point it to a common member function by simply assigning the entry address of a public member function to a pointer variable that points to a public member function.

p2=&time∷get_time;

The general form of a defined constant object is

Const Class Name Object name (argument table column);

You can also write a const to the left.

mutable int count;

If you declare member functions as constant member functions, you can only reference data members in this class, and you cannot modify them, such as for output data only.

void Get_time () const; Note the position of the const after the function name and parentheses

If a constant object is defined, only the const member function can be called, not the non-const member function

The general form of a constant pointer to an object (the pointer value is always initial and cannot be changed to point to)

Class name *const pointer variable name;

Pointer variable pointing to a constant object:

Const class NAME * variable name

Const time &t1=t; object's constant reference, cannot change the value of the indicated position

When the operator is executed, the destructor is automatically called before the memory space is freed, completing the cleanup work.

The assignment between the elephants is also done by the assignment operator "=".

The assignment of an object only assigns values to the data members, not to the member function

The data members of a class cannot include dynamically allocated data, otherwise there may be serious consequences when assigning a value

Copying of objects: Box box2 (Box1);

If the user does not define the copy constructor themselves, the compilation system automatically provides a default copy constructor that simply replicates the class


Each data member.

C + + also provides another user-friendly form of replication, using assignment numbers instead of parentheses, such as Box box2=box1;

The form of a copy constructor is this:

Box::box (const Box &b) {

Height=b.height;

Width=b.width;

...}

Static data members can be initialized, but can only be initialized outside of the class body.

A static data member can be referenced either by an object name or by a class name.

A static member function can be called through a class, or by an object name, such as

Box::volume ();

A.volume ();

Static member functions do not have the this pointer and cannot access non-static members in this class.

Static member functions can refer directly to static data members in this class, and if you must refer to non-static members of this class, you should add object names and member operations


Symbol "."

The class body is declared with friend, and this function is called the friend function of this class. A friend function can access a private member in this class.

A in the definition body of Class B, declare the class as its friend class in the following statement:

Friend B;

A friend class relationship is not passed and is not used in the work.

Sometimes, there are two or more classes whose functionality is the same, just different data types, at which point a generic class template can be declared:

Template<class type parameter name >

When actually used, specify the actual type name within the angle brackets: compare<int>, at which point the Numtype in Compare are replaced by int

When you define a class member function outside of a class, you need to use the following form:

Numtype Compare<numtype>::max () {...}

Tenth operator overloading


11th Chapter Inheritance and derivation

How derived classes are declared: Class Student1:public student{...}

Public inheritance: The public and protected members of the base class maintain the original Access property in the derived class, and their private members remain private to the base class.

Private inheritance: The public and protected members of the base class are private members in the derived class. Its private members are still private to the base class.

Protected inheritance: The public and protected members of a base class are protected members in derived classes, and their private members are still private to the base class.

The general form of a derived class constructor is:

Derived class constructor name (total parameter table column): base class constructor name (parameter table column)

Student1 (int n, String nam, char s, int A, string AD): Student (n, NAM, s) {

Age=a;

Addr=ad;

}

You can also include inline objects:

Derived class constructor name (total parameter table column): base class constructor name (Parameter table column), child object name (parameter table column), for example:

Student (int n, string nam, int n1, string nam1, int ad, string AD): Student (n, NAM), monitor (N1, NAM1)

When derived, a derived class cannot inherit a destructor for a base class, and a destructor for a base class is called by a destructor of the derived class. In a derived class


You can define your own destructors as needed to clean up the added members of the derived class.

Multiple inheritance: Class D:public A, Private B, protected C

Derived class constructor name (total parameter table column): base class 1 Constructor (Parameter table column), base class 2 constructor (parameter table column) {...}

Two semantic problems caused by multiple inheritance: Two base classes have members of the same name, two base classes, and derived classes, all of whom have the same name.

Rule: A member of the same name in a base class is masked in a derived class.

Virtual base class: Only one copy of the member is retained when inheriting the indirect common base class (the virtual base class needs to be declared in all direct derived classes).

Class B:virtual public a{...}

Class C:virtual public a{...}

In the last derived class, you need to initialize not only the direct base class, but also the virtual base class.

A derived class object can override a base class to assign or initialize a reference to a base class object.

12th Chapter

Static polymorphism determines which function is called at compile time, and dynamic polymorphism is at run time. Static polymorphism is implemented through function overloading and operator overloading, and dynamic


Polymorphism is realized by virtual function.

Virtual functions: In the same invocation form, you can invoke both a derived class and a function of the same name as the base class. In the program, it is not called by different object names.


Functions with the same name in the derived hierarchy, but they are called by pointers.

virtual function declaration: virtual void Play ();

When a member function is declared as a virtual function, the function with the same name in its derived class automatically becomes a virtual function.

Redefining this function in a derived class requires that the function name, function type, number of function arguments, and type are all the same as the virtual functions of the base class.

By using a virtual function with a pointer variable to a base class object, you can easily invoke a function of the same name in the same class, as long as you first use the base class


The pointer points to it.

The process of determining the specific object to invoke is called an association (binding).

After a member function is declared as a virtual function, a class in the same family can no longer define a non-virtual one but has the same parameters as the virtual function (


Functions with the same name, including number and type) and function return value types.

When the destructor of a base class is a virtual function, the system takes a dynamic association and calls the corresponding destructor to clean up the object, regardless of which class object in the same class family is referred to by the pointer.

Virtual float area () const =0;//Pure virtual function

A pure virtual function does not have a function body, but only informs the compiler that a virtual function is declared here, which is left to be defined in the derived class.

All classes that contain pure virtual functions are abstract classes.










C + + Basic Grammar notes

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.