#include<iostream>
using namespace std;
namespace A
{int a =10;int b =20;int c =30;}voidtest01(){//访问A命名空间下的a的两种方式//cout << A::a << endl;
using A::a;
cout << a << endl;//int a = 100;//error,因为上述代码使用using后,就相当于重新在该函数内部定义a(此时a就可以理解为局部变量),因此不可以重复定义,但是可以多次赋值
a =100;
cout << a << endl;}voidtest02(){//在当前函数的作用域(该函数执行结束后自动销毁)下,都可以使用命名空间A中的变量或者函数
using namespace A;
cout << a << endl;
cout << b << endl;
cout << c << endl;//以下代码不会报错,是因为A命名空间中定义的a是全局变量,而下面定义的a是局部变量,因此不会报错int a =1000;int b =2000;int c =3000;
cout << a << endl;
cout << b << endl;
cout << c << endl;}intmain(){//test01();test02();system("pause");return0;}
#include<iostream>
using namespace std;struct Person
{char name[64];int age;voidfunc(){
cout <<"helloworld"<< endl;}};voidtest01(){//在cpp中,自定义的结构类型在使用的时候是不需要加struct关键字的
Person p1;//在cpp中,自定义的数据类型中可以写函数
p1.func();}intmain(){test01();system("pause");return0;}
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>voidtest(){int a =10;int b =20;printf("%d\n", a > b ? a : b);//(a > b ? a : b) = 100;err//这个表达式返回的是右值,是数值,返回的是20//20 = 100;*(a > b ?&a :&b)=100;printf("b=%d\n", b);}intmain(){test();system("pause");return EXIT_SUCCESS;}
//值传递voidValueSwap(int m,int n){int temp = m;
m = n;
n = temp;}//地址传递voidPointerSwap(int* m,int* n){int temp =*m;*m =*n;*n = temp;}//引用传递voidReferenceSwap(int& m,int& n){int temp = m;
m = n;
n = temp;}voidtest(){int a =10;int b =20;//值传递ValueSwap(a, b);
cout <<"a:"<< a <<" b:"<< b << endl;//地址传递PointerSwap(&a,&b);
cout <<"a:"<< a <<" b:"<< b << endl;//引用传递ReferenceSwap(a, b);
cout <<"a:"<< a <<" b:"<< b << endl;}//返回局部变量引用int&TestFun01(){int a =10;//局部变量return a;}//返回静态变量引用int&TestFunc02(){staticint a =20;
cout <<"static int a : "<< a << endl;return a;}intmain(){//不能返回局部变量的引用int& ret01 =TestFun01();//如果函数做左值,那么必须返回引用test();TestFunc02();TestFunc02()=100;TestFunc02();return0;}