[C/C ++ school] (4) c ++ class and object/namespace/type enhancement/Three-object operator/const topic/Reference topic/function enhancement, namespace const

Source: Internet
Author: User

[C/C ++ school] (4) c ++ class and object/namespace/type enhancement/Three-object operator/const topic/Reference topic/function enhancement, namespace const
1. Classes and objects

Member functions, member variables, and abstract encapsulation capabilities.

Calculates 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 ;}; 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; system (" pause ");}

2. Command Space

Namespace; c ++ extensions to c. Resolves the identifier conflict. Std: out: domain identifier.
#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);    system("pause");} 



3. syntax enhancement 3.1register keyword enhancement int main () {register int a = 0; printf ("& a = % x \ n", & a); system ("pause "); return 0 ;}

// The register keyword requests the compiler to put variable a directly in the register, which is fast.

// In c language, the variable modified by register cannot take the address, but the content is included in c ++.

Changes in the register keyword

The register keyword requests the "compiler" to store local variables in registers.

Register variable address cannot be obtained in C Language

The register keyword is still supported in C ++.

The C ++ compiler has its own optimization method, which may be optimized without using register.

The address of the register variable can be obtained in C ++.

3.2struct type enhancement

Struct type enhancement:

The struct in C Language defines a set of variables, which the C compiler does not consider as a new type.

Struct in C ++ is a new type 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-object Operator
# Include <iostream> using namespace std; // The Three-object operator in c ++ returns a variable // enables the expression to make the left value. // The left value of 1 can be placed in =. The value is called the left value. // 2 when the left Value value Condition, you can write int main () {int a = 10; int B = 20; int c = 31; // returns a minimum number and assigns a value of 30 to the minimum number. // The Three-object operator is an expression, and the expression cannot be left. // make the expression a left value (a <B? A: B) = 30; // equivalent to * (a <B? & A: & B) = 30; // The error printf ("a = % d, B = % d \ n", a, B) cannot be compiled in c ); system ("pause"); return 0 ;}


5. const topics
In the c ++ compiler, a symbol table named key <---> value | a <--> 10 is created for the variable defined by const, the original const data is not modified. In c, the const constant can be modified.
6. The Reference topic reference is a constant pointer in c ++.
Type & name <---> type * const name; the essence of the reference is that the c ++ compiler helps us to get an address.
# 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 ;} // void main () {int a1 = 10; int b1 = 20; swap (a1, b1); printf ("a1: % d, b1: % d ", a1, b1); system (" pause ");}

7. Function enhancement 7.1 inline functions the function body must be written together with the implementation and cannot be declared separately. Replace macros with parameters to avoid macro side effects.
# Include "iostream" using namespace std; # define MYFUNC (a, B) (a) <(B )? (A): (B) // The inline request keyword, the function body for inline compilation // The inline function, which must be written in an inline int myfunc (int a, int B) together with the implementation) {return a <B? A: B;} int main () {int a = 1; int B = 3; // int c = myfunc (++ a, B ); int c = MYFUNC (++ a, B); // ==>( (++ a) <(B )? (++ A): (B); printf ("a = % d \ n", a); // 3 printf ("B = % d \ n ", b); // 3 printf ("c = % d \ n", c); // 3 system ("pause"); return 0 ;}

7.2 combination of function overload function and function pointer; you can add power failure, select statement-by-statement execution, and observe the running effect.
# Include "iostream" using namespace std; int func (int x) // int (int a) {return x;} int func (int a, int B) {return a + B;} int func (const char * s) {return strlen (s);} // defines a pointer type (pointing to the pointer type of the function) typedef int (* PFUNC) (int a); // int (int a) void main () {PFUNC p = func; int c = p (1 ); printf ("c = % d \ n", c); system ("pause ");}




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.