[C/C ++ school] 0813-C is different from CPP, and namespace introduction/function overloading and default function parameters/generic auto/Newdelete
Different from CPP in C and introduction to namespaces
Namespace is used in software design to achieve iterative development.
Namespace alias
# Include
Namespace runrunrunrun {int a (10); char * str (gogogo); namespace run // nesting of namespaces {int a (9 );}} namespace runrunrunrun // namespace extension {int y (5); // int a (15); redefinition error} namespace r = runrunrunrun; // give the namespace an alias void main132 () {std: cout <r: run: a <std: endl; // The namespace can be nested with std: cout <r: y <std: endl; std: cout <r: a <std :: endl;} // using namespace r; void main11 () {// std: cout <r: a <
Precautions for namespace Software Development
# Include
Namespace runmove {int y (5); int (* padd) (int, int); // function pointer interface // private: The namespace is transparent int y1 (5 ); class myclass {public: // The default class is private, which encapsulates int a ;}}int add (int a, int B) {return a + B ;} int addp (int a, int B) {std: cout <a <B; return a + B;} struct MyStruct {int B; // The struct is transparent by default private: int a; // private}; void main1123 () {// all data, functions, classes, and objects in namespace are in total MyStruct struct1; // The internal structure is public struct1. B by default ;}
Using Scope
# Include
# Include
Namespace mydata {int a (6);} namespace yourdata {int a (8);} using namespace mydata; // using must be behind the namespace and the scope of using namespace yourdata; // if the value of using is the same as that of another variable, an ambiguous error occurs. Add the namespace modifier void go () {// If the namespace is in the block statement content, scope is to define start to end with BLOCK statement std: cout <mydata: a <std: endl;} // using namespace mydata; // The scope is from code start to end void main () {// std: cout <mydata: a <std: endl; std: cout <
Function overloading and default function parameters
Function overloading Principle
# Include
# Include
// The number of parameters. The parameter type is different and the order is different. It is irrelevant to the return value void go (int a) {std: cout <a;} void go (double) {std: cout <a;} void go (double a, int B) {std: cout <a <B;} void go (int, double B) {std: cout <a <B;} // int go (double) // {// void go (int a, int B) // {//} void main () {void (* pgo1) (int a) = go; void (* pgo2) (double a) = go; void (* pgo3) (double a, int B) = go; void (* pgo4) (int a, double B) = go; printf (% p, pgo1); printf (% p, pgo2); printf (% p, pgo3); printf (% p, pgo4); getchar ();}
Default parameters
# Include
# Include
# Include
// The default parameters must be placed on the right, because the parameter's inbound sequence is from right to left // The default parameters do not allow the use of non-default void print (int c, int a = 1, int d = 2, int B = 3) {std: cout <a <
Generic auto
# Include
# Include
// Automatic variable, automatic acquisition of type, output, generic // automatic variable, can realize automatic cycle of one-dimensional array // automatic cycle, the corresponding must be constant void main1 () {auto num = 10.9; // automatic variable, auto numA = 10/3. 0; std: cout <num <numA <std: endl; system (pause);} void main () {// int num [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; double num [2] [5] = {1.0, 2.0, 3.0, 4.5, 5, 6, 7, 8, 9, 10 }; // num array name is a pointer constant // auto loop begin endl;, must be an array constant // for (auto data: num) // Generic C ++ syntax, cyclic one-dimensional array, constant {std: cout <data <
Newdelete
Object Array
# Include
# Include
Class tansheng1 {int * p; int length; public: tansheng1 () // initialize {std: cout <tan Sheng created 1 <
New can only allocate thread memory
# Include
# Include
# Include
Void main1 () {int num = 10; // stack int * p = new int; // stack * p = 5; std :: cout <* p <std: endl; delete p; // only one std: can be released :: cout <p <std: endl; system (pause);} void main2 () {// int num [10]; int * p = new int [10]; // int I = 0; std: cout <p <std: endl; for (int I = 0; I <10; I ++) {p [I] = I; std: cout <p [I] <std: endl;} delete [] p; // delete the space std :: cout <p <std: endl; system (pause) ;}void main3 () {int * p = new int [80]; int (* px) [10] = (int (*) [10]) p; // int (* px) [10] = new int [80]; new can only be allocated with linear int data = 0; for (int I = 0; I <8; I ++) {for (int j = 0; j <10; j ++) {px [I] [j] = data ++; std: cout <px [I] [j];} std: cout <
Newdelete overload
# Include
# Include
Class tansheng {public: static int jishuqi; // static int * p; int length; public: tansheng () // initialize {std :: cout <tan Sheng was created 2 <std: endl ;}~ Tansheng () // release memory when deleting {std: cout <tan Sheng destroyed 2 <std: endl;} static void * operator new (size_t size) {jishuqi + = 1; std: cout <object created 2 <std: endl; tansheng * ptemp =: new tansheng; // hijack return ptemp ;} static void operator delete (void * p) {jishuqi-= 1; std: cout <object destroyed 2 <std: endl;: delete p ;//:: Global }}; int tansheng: jishuqi = 0; // The new inside the class does not complete the action of allocating memory // a hijacking is performed in the middle of the new to the global // The empty class occupies one byte, indicating that it has its own // class object, data is independent and code is shared // No score Memory configuration, meaningless constructor class MyClass {int num; public: MyClass ();~ MyClass (); private:}; MyClass: MyClass () {} MyClass ::~ MyClass () {} void main () {tansheng * p1 = new tansheng; tansheng * p2 = new tansheng; tansheng * p3 = new tansheng; tansheng * p4 = new tansheng; std:: cout <p1 <p2 <std: endl; delete p1; delete p2; std: cout <tansheng: jishuqi <std: endl; std:: cout <