1. Classes and objects
member functions, member variables, the ability to abstract encapsulation.
To find the area of the circle;
#include <iostream>using namespace Std;class circle{private: double m_r;//member variable public: void Setr (double R)//member function { m_r = R; } Double Getr () { return m_r; } Double GetS () { return 3.14*m_r*m_r; }}; void Main () { circle C1; C1.SETR (4); cout << "R:" << C1.getr () << "s:" << c1.gets () << Endl; C1.SETR (5); cout << "R:" << C1.getr () << "s:" << c1.gets () << Endl;
2. Command space
namespace; C + + extension. Resolves identifier conflicts. Std::out:: Domain action.
#include <iostream>namespace namespacea{ int a = 0;} namespace namespaceb{ int a = 1; namespace namespacec{ struct teacher{ char name[10]; int age; };} } void Main () { using namespace NamespaceA; printf ("namespacea:a=%d\n", a); printf ("namespaceb:a=%d\n", namespaceb::a); Using Namespaceb::namespacec::teacher; Teacher T1 = {"AAA", 3}; printf ("T1.name =%s\n", t1.name); printf ("T1.age =%d\n", t1.age);
3. Syntax enhancement3.1register keyword Enhancementint main () { Register int a = 0;printf ("&a =%x\n", &a);System ("pause"); return 0;}
Register keyword request compiler let the variable a directly inside the register, fast
Register -Modified variables in C are not addresses, but in C + + content
Change of register keyword
Register keyword Request "compiler" to store local variables in registers
cannot get register variable address in C language
register keyword is still supported in C + +
The C + + compiler has its own optimization method, and It may be optimized without register
Address of Register variable can be obtained in C + +
3.2struct Type Enhancement
Struct type enhancement: C language struct defines a set of variables, c compiler does not consider this to be a new type C++ struct is a new type of definition declaration |
Struct student { char name[100]; int age; }; Int main (int argc, char *argv[]) { student s1 = {"Wang", 1};//struct Student s1={}; student s2 = {"Wang2", 2}; return 0; } |
4. Three mesh operator
#include <iostream>using namespace std;//in C + + three mesh operator return is a variable//Let the expression do lvalue//1 left value can be put in = do value is called Lvalue //2 when left value condition, this memory space can be with being you write int main () {int a = 10;int b = 20;int c = 31;//Returns a minimum number and assigns the minimum number to the 30//three mesh operator is an expression, the expression cannot do lvalue//Let the expression do Lvalue (a < b? a:b) = 30;//equivalent to * in C ((a < b &a: &b)) = 30;//compiled in c However, error printf ("A =%d, B =%d\n", A, b); system ("pause"); return 0;}
5.const topics
const-defined variables, in the C + + compiler, made a symbol table, key <--->value | a<-->10; Modify the time, just modified the reallocation of space, the original const data did not cause modification. In C, however, you can modify the const constant.
6. Citing topicsA reference is a constant pointer inside C + +.
type &name <---> type * const name;the nature of the reference is that the C + + compiler has helped us do an address-taking operation.
#include <iostream>using namespace std;void swap (int &a, int &b) {int c = 0;c = A;a = B;b = C;} void swap2 (int *a, int *b) {int c = 0;c = *a;*a = *b;*b = C;} When the reference and Lvalue are bound, void main () {int a1 = ten; int B1 = 20;swap (A1, B1);p rintf ("a1:%d, b1:%d", A1, B1); System ("pause");
C/C + + Academy (4) + + Opening/class and Object/namespace/type enhancement/three-mesh operator/const/citation topic