频道栏目
首页 > 资讯 > C++ > 正文

c++命名空间和引用(代码教程)

18-07-19        来源:[db:作者]  
收藏   我要投稿
< >c++命名空间和引用(代码教程)
#include 
//c++中的输入输出头文件
#include 


//标准命名空间包含了许多标准的订义
using namespace std;
//命名空间类似于java中的包
/*
//自定义命名空间
namespace NSP_A{
 int a = 10;
}

namespace NSP_B{
 int b = 30;
}

//命名空间嵌套结构体
namespace NSP_C{
 int a = 40;
 struct Teacher{
  char* name;
  int age;
 };
 struct Student{
  char* name;
  int age;
 };
}
//命名空间中嵌套命名空间
namespace NSP_D{
 namespace NSP_INNER_D{
  int d = 80;
 }
}

void main(){
 //std::cout << "hello world" << std::endl;
 cout << "hello world" << endl;

 //使用命名空间,::代表修饰符
 cout << NSP_A::a << endl;
 cout << NSP_B::b << endl;

 //使用命名空间中嵌套的结构体
 struct NSP_C::Teacher t;
 t.name = "tom";
 cout << t.name << endl;

 using NSP_C::Teacher;
 Teacher t1;
 t1.name = "jack";
 cout << t1.name << endl;

 //使用命名空间中嵌套的命名空间
 cout << NSP_D::NSP_INNER_D::d << endl;
 system("pause");
 }*/
//c++中多了class类
/*#define PI 3.14

class Circle{
private:
 double r;
 double s;
public:
 void radius(double r){
  this->r = r;
 }
 double getS(){
  return PI*r*r;
 }
};

void main(){
 //类的使用
 Circle c;
 c.radius(4);//函数
 cout << c.getS() << endl;

 system("pause");
}*/
//c++中的结构体进行了加强
/*struct Teacher{
public:
 char* name;
 int age;
public:
 void say(){
  cout << "age"<
//布尔类型,c++中有bool类型
/*
void main(){
 //bool isSingle = true;
 bool isSingle = 17;
 //false -17

 if (isSingle){
  cout << "单身" << endl;
  cout << sizeof(bool) << endl;
 }
 else{
  cout << "有对象" << endl;
 }

 int a = 10, b = 20;
 ((a > b) ? a : b) = 30;
 cout << b << endl;
 system("pause");
}*/
//引用 c++中有引用,也叫别名
/*void main(){
 //变量名-门牌号(内存空间0x00001的别名,可不可以有多个名字?)
 int a = 10;
 //b就是这个内存空间的别名,也就是b叫0x0001
 //& 在c++中叫引用
 int &b = a;
 cout << b << endl;
 system("pause");
}*/
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 x = 10;
 int y = 20;
 //交换前
 cout << x << "和" << y << endl;
 //通过指针交换两个数
 swap(&x, &y);
 cout << x << "和" << y << endl;
 //通过别名,引用交换两个数
 swap(x, y);
 cout << x << "和" << y << endl;
 system("pause");
}
相关TAG标签
上一篇:CBitmap与BITMAP的区别举例说明
下一篇:ios开发学习之数据组件介绍
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站