Concepts that must be mastered by C ++

Source: Internet
Author: User

I. pointer Concept

Char STR [] = "abcdefg ";
Char * Pc = STR; // PC is a pointer to string Str
Short x = 33;
Short * ppx = & X; // PX is the pointer to short x
Cout <* Pc <Endl; // This statement prints the character 'a'
PC + = 4; // move the pointer to the right. 4 points to 5th characters.
Cout <* Pc <Endl; // This statement prints the character 'E'
PC --; // move the pointer to the left
Cout <* Pc <Endl; // This statement prints the character 'D'
Cout <* ppx + 3 <Endl; // print 36 for this statement as = 33 + 3

In CProgramSuppose we have defined the following variables and functions: int K, TEM, * P1, * P2, a [5], F (), * P3 (); which of the following syntactic errors are described in assignment statements? Describe the cause.

1. p1 = & K;
2. P2 =;
3. P3 = F;
4. p1 = & A [3];
5. p1 = P2;

Answer:
(1) P1 = & K; P1 is a pointer variable. Therefore, the address of the P1 table, while K represents a common variable, and & K represents the address of the retrieved K, so it is correct.
(2) P2 = A; A is the array name, which indicates the starting address of the array stored in the memory, while P2 is the pointer variable, so it is correct.
(3) P3 = f; F indicates the name of the function. In this case, call function f is called. Therefore, the returned value is contained, and P3 is a pointer variable. Therefore, an error occurs.
(4) P1 = & A [3]; P1 table pointer variable, representing the address, while & A [3] Table retrieves the address of an array element whose index is 3, it is correct.
(5) P1 = P2; P1, P2 are pointer variables that represent the address. This description specifies the address of P2 to P1, so it is correct.

Structure concept
Structure is a type, and its members are public by default.

Struct student // define a structure. Student is used to store student data.
{
Int ID; // number
Char name [30]; // name
}
Stuindent S = {555, "Davis, Samuel"}; // initialize student's instance s
Cout <S. ID <"<S. Name <Endl; // This line prints" 555 Davis, Samuel"

I think everyone should be very clear about the concept of class, so I will not talk nonsense.

The concept of class inheritance
Class base
{
PRIVATE:
Int;
Protected:
Int B;
Public:
Int C;
};
Class sub1: public base {...};
Class sub2: Private base {...};

Description of the data members available in base, sub1, and sub2, And the access mode (private, protected, or public) of these data members ).
Ans:
Class data members access mode
Base A private
B protected
C public
Sub1
B protected
C public
Sub2
B private
C private

Virtual functions and abstract classes

Polymorphism)
One of the core concepts of object-oriented programming is polymorphism, which allows a group of similar behavior methods with the same name, however, each object can construct the implementation details of the action of the same name in a suitable way. The key to C ++ polymorphism is the so-called virtual function.

Virtual Functions)
Through virtual functions, the struct class can redefine the member functions of the base class. If you want to create virtual functions in the C ++ Program (and then implement polymorphism ), you only need to use the virtual keyword to declare a function (as shown below)
Virtual void display ();
Use of virtual functions
Objects that share the same base class have a consistent usage attitude. For example, you may define a base class named shape with a draw virtual member function, the circle class and square class are derived from it, and each of them has its own draw member function. each object derived from these classes can call the draw member function, but the compiler can ensure that each of them should call the draw function of that version. is it a base class or a derived class.

Example

Important Concept: pointers to parent classes can also be used to point to child classes.

# Include <iostream. h>
Class baseclass
{
Public:
Virtual void display () {cout <100 <"\ n ";}
};
Class derivedclass: Public baseclass
{
Public:
Virtual void display () {cout <200 <"\ n ";}
};
Void print (baseclass * PBC)
{PBC-> display ();}
Int main ()
{Baseclass * PBC = new baseclass;
Derivedclass * PDC = new derivedclass;
Print (PBC); // display 100
Print (PDC); // display 200
Return 0;
}

V-table (virtual function table)
When a C ++ program calls a non-virtual function, it uses the same static binding method as the C program call function to call the function. however, if a C ++ program calls a virtual function by pointing to a class pointer, the compiled program uses the so-called late binding or static binding) technology to call functions.
C ++ virtual functions use virtual function tables or V-tables to implement dynamic binding. The so-called v-table is an array of function pointers, this is the establishment of the Compilation Program for each class that uses virtual functions.

Pure virtual function)
A member function that can be redefined and must be redefined is called a pure virtual function. You only need to specify a zero value for the function (a null pointer is more effective ), you can convert a virtual member function to a pure virtual member function, as shown in the following figure.
Virtual void printdata () = 0;

Abstract class)
When a class contains at least one pure virtual function, this class is called an abstract class, and you cannot derive the object from this class.

C ++ template classes

General statement and usage
Class collection
{...
Int A [10];
}
Collection object;
Template declaration and use
Template <class T> // note the following:
Class collection
{...
T a [10];} // generic declaration
Collection <int> object; // note the following:
Collection <char> object; // note the following:

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.