【C/C++学院】0813-C与CPP不同以及命名空间简介/函数重载与函数默认参数/泛型auto/Newdelete

来源:互联网 发布:project facets java 编辑:程序博客网 时间:2024/06/05 22:43

CCPP不同以及命名空间简介

命名空间在软件设计中的作用就是为了实现迭代式开发。

命名空间的别名

#include <iostream>namespace runrunrunrun{int a(10);char *str("gogogo");namespace run   //命名空间的嵌套{int a(9);}}namespace runrunrunrun  //命名空间的拓展{int  y(5);//int  a(15);重定义错误}namespace r = runrunrunrun;//给命名空间起一个别名void main132(){std::cout << r::run::a << std::endl;//命名空间可以嵌套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;}

命名空间软件开发注意事项

#include <iostream>namespace runmove{int  y(5);int(*padd)(int, int);//函数指针接口//private: 命名空间是透明的int  y1(5);class myclass{public://类默认是私有,实现封装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;//结构体默认是透明的private:int a;//是私有};void main1123(){//namespace所有数据,函数,类,对象都是共有MyStruct struct1;//结构体内部默认公有struct1.b;}

Using作用域

#include<iostream>#include<stdlib.h>namespace mydata{int a(6);}namespace yourdata{int a(8);}using namespace mydata;//using必须在命名空间的后面,作用域using namespace yourdata;//using如果变量重名,会出现不明确错误,加上命名空间修饰符void go(){//命名空间如果在块语句内容,作用域是定义开始到块语句结束std::cout << mydata::a << std::endl;}//using namespace mydata;//作用域为从代码开始到结束void main(){//std::cout << mydata::a << std::endl;std::cout <<yourdata::a << std::endl;system("pause");}

函数重载与函数默认参数

函数重载原理

#include<iostream>#include<stdio.h>//参数的个数,参数的类型不同,顺序不同,与返回值无关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);printf("%p\n", pgo2);printf("%p\n", pgo3);printf("%p\n", pgo4);getchar();}

默认参数

#include<stdio.h>#include<stdlib.h>#include<iostream>//默认参数必须放在右边,因为参数的入栈顺序是从右至左//默认参数中间不允许出现不默认的void print(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(1,2,3);//函数指针没有默认参数,必须全部输入数据//函数重载与函数默认参数冲突,需要你输入的参数类型不一个,个数不一样,顺序不一样不会出现问题,否则一定报错void(*pt1)(int c, int a , int d , int b ) = print;pt1(100,1,2,3);//函数指针调用,没有用默认的print(100);system("pause");}

泛型auto

#include<iostream>#include<stdlib.h>//自动变量,自动获取类型,输出,泛型//自动变量,可以实现自动循环一维数组//自动循环的时候,对应的必须是常量void main1(){auto num = 10.9;//自动变量,自动匹配类型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数组名是一个指针常量//auto 自动循环 begin  endl;,必须是一个数组的常量//for (auto data : num)//泛型C++语法,循环一维数组,常量{std::cout << data<<std::endl;for (int i = 0; i < 5; i++){std::cout << *(data + i) << std::endl;}}system("pause");}

Newdelete

对象数组

#include <iostream>#include<stdlib.h>class tansheng1{int *p;int length;public:tansheng1()//构建的时候初始化{std::cout << "谭胜被创建1"<<std::endl;}~tansheng1()//删除的时候释放内存{std::cout << "谭胜被销毁1" << std::endl;}};void main(){tansheng1 *p = new tansheng1[3];delete  []p;//基本数据类型,delete,复杂类型必须[]system("pause");}

New只能分配线程的内存

#include<stdio.h>#include<stdlib.h>#include<iostream>void main1(){int num=10;//栈上int *p = new int;//堆上*p = 5;std::cout << *p << "  " << p << std::endl;delete p;//delete p;//只能释放一次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;//删除数组的空间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只能分配线性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<<std::endl;}system("pause");}

Newdelete重载

#include <iostream>#include<stdlib.h>class tansheng{public:static int jishuqi;//静态int *p;int length;public:tansheng()//构建的时候初始化{std::cout << "谭胜被创建2" << std::endl;}~tansheng()//删除的时候释放内存{std::cout << "谭胜被销毁2" << std::endl;}static void * operator new(size_t size){jishuqi += 1;std::cout << "对象被创建2" << std::endl;tansheng *ptemp = ::new tansheng;//劫持return ptemp;}static void  operator delete(void *p){jishuqi -= 1;std::cout << "对象被销毁2" << std::endl;::delete p;//::全局}};int tansheng::jishuqi = 0;//类的内部的new没有完成分配内存的动作//通往全局的new中间做了一个劫持//空类占一个字节,表示自己存在//类的对象,数据是独立,代码是共享的//没有分配内存,构造函数无意义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 <<"myclass size"<<sizeof(MyClass) << std::endl;system("pause");}


1 0