Cwith theCPPIntroduction to different and namespaces
The role of namespaces in software design is to achieve iterative development.
Aliases for namespaces
#include <iostream>namespace runrunrunrun{int A (ten), Char *str ("GoGoGo"), namespace run //namespace nested {int a (9);}} namespace Runrunrunrun //namespace Extension {int y (5);//int A (15); redefine error}namespace r = runrunrunrun;// Give namespace an alias void main132 () {std::cout << r::run::a << std::endl;//namespace can be nested std::cout << r::y << std :: Endl;std::cout << r::a << Std::endl;} Using namespace R;void main11 () {//std::cout << r::a <<r::str<< std::endl;//std::cout << a < < str << Std::endl;}
Namespaces Software Development Considerations
#include <iostream>namespace runmove{int y (5), int (*padd) (int, int),//function pointer Interface//private: namespace is transparent int y1 (5); class myclass{public://classes are private by default, implementation 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;} The struct mystruct{int b;//struct is transparent by default Private:int a;//is Private};void main1123 () {//namespace all data, functions, classes, objects are all shared MyStruct struct1;/ /struct internal default public struct1.b;}
Using Scope
#include <iostream> #include <stdlib.h>namespace mydata{int A (6);} Namespace Yourdata{int A (8);} Using namespace mydata;//using must be behind the namespace, scope using namespace yourdata;//using If the variable has the same name, an ambiguous error occurs, plus the namespace modifier void Go () {// namespace if in block statement content, scope is defined start to block statement end Std::cout << mydata::a << Std::endl;} The using namespace mydata;//scope is from code start to end void Main () {//std::cout << mydata::a << Std::endl;std::cout < <yourdata::a << std::endl;system ("pause");}
function overloading and function default arguments
function Overloading principle
#include <iostream> #include number of <stdio.h>//parameters, different types of parameters, different order, not related to return value void Go (int a) {std::cout << A;} void Go (double a) {Std::cout << A;} void Go (double a,int b) {std::cout << a << b;} void Go (int A, double b) {std::cout << a << b;} int Go (double A)//{//////}//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\n", Pgo1);p rintf ("%p\n", Pgo2);p rintf ( "%p\n", Pgo3);p rintf ("%p\n", Pgo4); GetChar ();}
Default parameters
#include <stdio.h> #include <stdlib.h> #include <iostream>//default parameters must be on the right, because the order of the parameters is right-to-left/ Non-default void print is not allowed in the middle of default parameters (int c,int a = 1, int d=2, int b = 3) {std::cout << a<<b<<c << Std::endl;} void print (double c) {}void main () {//print ()///function pointer has no default parameters, must all input data//function overload and function default parameter conflict, need you to enter the parameter type not one, the number is different, The order does not appear the problem, otherwise must be an error void (*PT1) (int c, int A, int d, int b) = PRINT;PT1 (100,1,2,3);//function pointer call, not with the default print (); System ("Pause ");}
generic Auto
#include <iostream> #include <stdlib.h>//automatic variable, auto get type, output, Generic//automatic variable, can realize automatic loop one-dimensional array//automatic loop, corresponding must be constant void Main1 () {Auto num = 10.9;//automatic variable, auto-match type 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, ten};d ouble num[2][5] = {1.0, 2.0, 3.0, 4.5, 5, 6, 7, 8, 9, 10} The//num array name is a pointer constant//auto auto cycle begin Endl; must be an array of constants//for (auto Data:num)//generic C + + syntax, loop one-dimensional array, constants {Std::cout << data<<std::endl;for (int i = 0; i < 5; i++) {std::cout << * (data + i) << Std::endl;}} System ("Pause");}
Newdelete
Object array
#include <iostream> #include <stdlib.h>class tansheng1{int *p;int length;public:tansheng1 ()//build at the time of initialization { Std::cout << "Tan Sheng was created 1" <<STD::ENDL;} ~TANSHENG1 ()//release memory when deleted (Std::cout << "Tan Sheng destroyed 1" << Std::endl;}}; void Main () {tansheng1 *p = new Tansheng1[3];d elete []p;//basic data type, delete, complex type must []system ("Pause");}
New can only allocate memory for threads
#include <stdio.h> #include <stdlib.h> #include <iostream>void main1 () {int num=10;//stack int *p = new int ;//Heap *p = 5;std::cout << *p << " " << p << std::endl;delete p;//delete p;//can only be released once Std::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 of the array std::cout << P << std::endl;system ("pause");} void Main3 () {int *p = new Int[80];int (*PX) [Ten] = (int (*) [ten]) P;//int (*PX) [Ten] = new int[80];new can only be assigned linear int data = 0;for (in t i = 0; I < 8; i++) {for (int j = 0; J <; J + +) {Px[i][j] = data++;std::cout << "" << Px[i][j];} Std::cout<<std::endl;} System ("Pause");}
Newdelete Overloading
#include <iostream> #include <stdlib.h>class tansheng{public:static int jishuqi;//static int *p;int length; Public:tansheng ()//build when initialize {std::cout << "Tan Sheng is created 2" << Std::endl;} ~tansheng ()//release memory when deleted (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 =:: NE W tansheng;//hijack return ptemp;} static void operator delete (void *p) {Jishuqi-= 1;std::cout << "Object destroyed 2" << Std::endl;::d elete p;//:: Global}};int Tansheng::jishuqi = The internal new of the 0;//class does not complete the action of allocating memory//to the Global new middle does a hijack//empty class takes up a byte, indicates that oneself exists//Class of object, data is independent, code is shared//not allocated memory, Constructor meaningless class Myclass{int num;public:myclass (); ~myclass ();p rivate:}; 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;st D::cout << Tansheng::jishuqi << std::endL;std::cout << "MyClass size" <<sizeof (MyClass) << Std::endl;system ("pause");}
Copyright notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!
"C + + institute" 0813-C and CPP and introduction to namespaces/function overloading and function default parameters/generics Auto/newdelete