简介
通过各种xxxxFun函数对C++中类(class)、命名空间(namespace)、const、内联函数(inline)、函数重载等进行使用。
//#include <stdio.h>
//#include <stdlib.h>
#include <cstdio>//采用stdio.h时可以直接使用printf,但是c++中使用iostream时printf在命名空间std中标准用法是std::printf
#include <cstdlib>
#include <iostream>//用于控制台输入输出头文件
#include <fstream>//用于文件操作头文件
#include <complex.h>//用于复数计算头文件
class Student {//定义一个student类
public:
char *name;
int age;
float score;
void say() {
printf("%s的年龄是 %d,成绩是 %f\n", name, age, score);
}
};
void classFun(void)
{
class Student stu1;
stu1.name = "小明";
stu1.age = 15;
stu1.score = 92.5f;
stu1.say();
}
/*
命名空间的名字是Li 它可以包含变量、函数、类、typedef、#define 等,最后由{ }包围。
*/
namespace Li
{
int a;
}
namespace Han
{
int a;
}
void namepaceFun(void)
{
//::域解析操作符
Li::a = 3;
Han::a = 4;
printf("%d %d\n", Li::a, Han::a);
//关键字using声明后面遇见a默认是Li::a
using Li::a;
a = 5;
Han::a = 6;
printf("%d %d\n", a, Han::a);
//关键字using除了声明命名空间的一个变量外,还可以声明整个命名空间
using namespace Li;
a = 7;
Han::a = 8;
printf("%d %d\n", a, Han::a);
}
namespace Diy
{
class Student
{
public:
char *name;
int age;
float score;
public:
void say()
{
printf("%s的年龄是 %d,成绩是 %f\n", name, age, score);
}
};
}
void namepaceFun2(void)
{
Diy::Student stu1;
stu1.name = "小明";
stu1.age = 15;
stu1.score = 92.5f;
stu1.say();
}
void stdFun(void)
{
//要重新声明
using namespace std;
cout << "string、cin、cout都位于命名空间std" << endl;
}
/*
//如果所有函数都使用命名空间std则声明在全局范围即可
这样虽然使用方便,但在中大型项目开发中是不被推荐的,
这样做增加了命名冲突的风险,我推荐在函数内部声明 std。
*/
//using namespace std;
void stdFun2(void)
{
//声明std命名空间
using namespace std;
cout << "cout" << endl;
stdFun();
}
void cinCoutFun(void)
{
using namespace std;
//cout后面endl与c中\n意思一样
int x;
float y;
cout << "Please input an int number:" << endl;
cin >> x;
cout << "Please int number is x= " << x << endl;
cin >> y;
cout << "The float number is y = " << y << endl;
}
void cinCoutFun2(void)
{
using namespace std;
//cout后面endl与c中\n意思一样
int x;
float y;
cout << "Please input an int number:" << endl;
cin >> x >> y;
cout << "Please int number is x= " << x <<" y = "<< y << endl;
}
int sum(int n)
{
int total = 0;
for (int i = 1; i <= n; i++)
{
total += i;
}
return total;
}
/*
C99任意位置可定义变量
*/
void c99testfun(void)
{
using namespace std;
cout << "Input a interge:";
int n;
cin >> n;
cout << "Total: " << sum(n) << endl;
}
/*
布尔类型的值是0和1不是true和false
推荐布尔类型用true表示1、false表示0
*/
void boolfun(void)
{
using namespace std;
bool flag = true;
if (flag)
{
cout << "true" << flag << endl;
}
else
{
cout << "false" << flag << endl;
}
flag = false;
if (flag)
{
cout << "true" << flag << endl;
}
else
{
cout << "false" << flag << endl;
}
}
/*
以下代码演示了C中修改const修饰的值
//#include <stdio.h>
在C++中都是是替换过程,所以修改地址n的值还是10
C语言对 const 的处理和普通变量一样,会到内存中读取数据;
C++ 对 const 的处理更像是编译时期的#define,是一个值替换的过程。
因此
C++ 中的 const 变量虽然也会占用内存,也能使用&获取得它的地址,
但是在使用时却更像编译时期的#define;#define也是值替换,可见范围也仅限于当前文件。
*/
void C_constfun(void)
{
const int n = 10;
int *p = (int*)&n;
printf("%d\n", n);
*p = 99;
printf("%d\n", n);
/*
除了以上问题
C和C++中全局 const 变量的作用域相同,都是当前文件,
不同的是它们的可见范围:C语言中 const 全局变量的可见范围是整个程序,
在其他文件中使用 extern 声明后就可以使用;而C++中 const 全局变量的可见范围仅限于当前文件,
在其他文件中不可见,所以它可以定义在头文件中,多次引入后也不会出错。
*/
}
inline void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
/*
使用内联函数的方法很简单只需要在声明时候加上inline关键字(注:在声明时候加inline是无效的)
由于内联函数比较短小,我们通常的做法是省略函数原型,
将整个函数定义(包括函数头和函数体)放在本应该提供函数原型的地方。
inlinefun这种使用方法是不被推荐的
*/
void inlineFun(void)
{
using namespace std;
int m, n;
cin >> m >> n;
cout << m << " " << n << endl;
swap(&m,&n);
cout << m << " " << n << endl;
}
/*
使用宏必须小心翼翼
使用内联函数代替宏
和宏一样,内联函数可以定义在头文件中(不用加 static 关键字),
并且头文件被多次#include后也不会引发重复定义错误。
内联函数主要有两个作用,
一是消除函数调用时的开销,
二是取代带参数的宏。
不过我更倾向于后者,取代带参数的宏更能凸显内联函数存在的意义。
正确的使用发方法是将内联函数放在头文件中,不需要声明内联函数
*/
#define SQ1(y) y*y
#define SQ2(y) (y)*(y)
#define SQ3(y) ((y)*(y))
inline int SQ(int y) { return y * y; }
void inlineFun2(void)
{
using namespace std;
int i = 9;
cout << SQ1(i) << " " << SQ1(i + 1) << endl;
cout << SQ2(i + 1) << " " << 200 / SQ2(i + 1) <<" " <<200/SQ3(i+1)<< endl;
cout << SQ(i) << " " << 200 / SQ(i + 1) << endl;
}
/*
C++可以给函数指定默认值
C++规定,默认参数只能放在形参列表的最后,而且一旦为某个形参指定了默认值,
那么它后面的所有形参都必须有默认值。
C++ 规定,在给定的作用域中只能指定一次默认参数。(也就是同一.cpp文件声明与定义不能都带默认值)
*/
void defaultTest(int a, int b , float c = 30.1);
void defaultTest(int a, int b = 10, float c);
void defaultTest(int a, int b , float c )
//void defaultTest(int a, int b=10, float c=30.1)
{
using namespace std;
cout << a << " " << b << " " << c <<endl;
}
void defaultfFun(void)
{
defaultTest(3,4,5.3);
defaultTest(6);
}
/*
C++ 允许多个函数拥有相同的名字,只要它们的参数列表不同就可以,
这就是函数的重载(Function Overloading)。借助重载,一个函数名可以有多种用途。
C++函数重载规则
函数名称必须相同。
参数列表必须不同(个数不同、类型不同、参数排列顺序不同等)。
函数的返回类型可以相同也可以不相同。
仅仅返回类型不同不足以成为函数的重载。
*/
void swapTest(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void swapTest(float *a, float *b)
{
float temp = *a;
*a = *b;
*b = temp;
}
void swapTest(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void swapTest(bool *a, bool *b)
{
bool temp = *a;
*a = *b;
*b = temp;
}
void swapFun(void)
{
using namespace std;
int n1 = 10, n2 = 20;
cout << n1 << " " << n2 << endl;
swapTest(&n1 ,&n2);
cout << n1 << " " << n2 << endl;
float f1 = 12.34, f2 = 56.78;
cout << f1 << " " << f2 << endl;
swapTest(&f1, &f2);
cout << f1 << " " << f2 << endl;
char c1 = 'A', c2 = 'B';
cout << c1 << " " << c2 << endl;
swapTest(&c1, &c2);
cout << c1 << " " << c2 << endl;
bool b1 = false, b2 = true;
cout << b1 << " " << b2 << endl;
swapTest(&b1, &b2);
cout << b1 << " " << b2 << endl;
}
int main()
{
swapFun();
system("pause");
return 0;
}