c++的基本内容总结

#include<iostream>

using namespace std;

/*

*指针 指针变量

函数指针

递归

基类型 * 指针变量名

*/

#if 0

void fn()

{

}

void fn(int a, int b) {}

int max(int a, int b)

{

return a > b ? a : b;

}

int min(int a, int b)

{

return a < b ? a : b;

}

int Sum(int a, int b)

{

return a + b;

}

int Sub(int a, int b)

{

return a - b;

}

int Mul(int a, int b)

{

return a * b;

}

int Div(int a, int b)

{

if (b != 0)

return a / b;

}

#endif

/*

函数转移表

*/

#if 0

void test(int a, int b, int (*p)(int, int)) //p = Sum

{

cout << p(a, b) << endl; //Sum(4,6)

}

void main()

{

test(4, 6, Sum);

}

#endif

#if 0

void main()

{

int (*p[])(int, int) = {max,min,Sum,Sub,Mul,Div};

int n = sizeof(p) / sizeof(p[0]);

for (int i = 0; i < n; ++i)

{

cout << p[i](4, 8) << endl;

}

}

#endif

#if 0

void main()

{

int (*p)(int, int) = max;

//p = max;

cout << p(4, 7) << endl;

p = min;

cout << p(4, 7) << endl;

}

#endif

/*

ff是一个函数名 里面有int n参数

ff函数返回值是个指针

这个指针指向一类函数 int xx(int,int)

*/

#if 0

int (*ff(int n))(int , int ) //函数指针函数

{

//cout << "ff n = " << n << endl;

//int (*p)(int, int) = max;

//return p;

return max;

}

void main()

{

cout<<ff(100)(6, 8)<<endl;

//int (*q)(int, int) = ff(100);

//cout << q(6, 8) << endl;

}

#endif

//signal 看下函数声明

/*

C++

属性:时针,分针,秒针

操作:设置时间 显示时间

苹果,梨,香蕉...

属性:大小,颜色,形状

操作:吃,补VC

老虎,狮子,狗,猫

属性:大小,体重,颜色,体型

操作:跑,跳,吃

*/

/*

类:将不同类型的属性以及与这些属性相关的操作封装在一起,构成一个集合

形成一种新的数据类型

在里面有公共的(public),保护的(protected),私有的(private)

this--在非static成员函数中隐含了指针 this,接收当前对象的地址

*/

#if 0

class Clock

{

public:

void Set() {}

void Set(int h);

void Set(int h, int m);

void Set(int h, int m, int s);

void Show()

{

cout << m_hour << " :" << m_minute << ":" << m_second << endl;

}

private:

int m_hour;

int m_minute;

int m_second;

};

void Clock::Set(int h)

{

this->m_hour = h;

this->m_minute = 20;

this->m_second = 30;

}

void Clock::Set(int h,int m)

{

this->m_hour = h;

this->m_minute = m;

this->m_second = 30;

}

void Clock::Set(int h,int m,int s)

{

this->m_hour = h;

this->m_minute = m;

this->m_second = s;

}

void main()

{

Clock c,c1,c2,c3;

c.Set(12); //Set(&c,12)

c.Show();

c1.Set(13,20);

c1.Show();

c2.Set(14, 20, 30);

c2.Show();

c3.Set();

c3.Show();

}

#endif

/*

类的大小

1.空类 1个字节

2.一般是类里面数据成员大小之和(普通函数不占大小)

3.static不在类中开辟空间

4.virtual函数多了指针大小

*/

#if 0

class A

{

int m_i;

int m_j;

static int m_k;

void set() {}

virtual void fn() {}

virtual void fn1() {}

virtual void fn2() {}

virtual void fn3() {}

};

void main()

{

cout << sizeof(A) << endl;

}

#endif

/*

带默认值(缺省值)的函数

1.默认值在声明写,定义时不写

2.默认值建议都写 向前看齐

*/

/*

内联函数---编译时

功能比较简单(没有循环和判断),代码比较短小(5),用空间换取时间的消耗

inline只是程序员的建议

宏定义和内联的区别

1.宏定义是预处理命令

内联是函数

2.宏定义是编译之前

内联是在编译时

3.宏定义会出现歧义,不会类型检测

内联不会出现歧义,类型检测

*/

#if 0

class Clock

{

public:

inline void Set(int h = 12, int m = 30, int s = 30);

void Show()

{

cout << m_hour << " " << m_minute << " " << m_second << endl;

}

inline void Sort()

{

//.....

}

private:

int m_hour;

int m_minute;

int m_second;

};

void Clock::Set(int h , int m, int s)

{

m_hour = h;

m_minute = m;

m_second = s;

}

void main()

{

Clock c1, c2, c3, c4;

c1.Set();

c1.Show();

c2.Set(13);

c2.Show();

c3.Set(14, 20);

c3.Show();

c4.Set(15, 20, 40);

c4.Show();

}

#endif

#define N 100p

//#define S(a,b) a/b

#define S(a,b) ((a)/(b))

void main()

{

//int n = 100p;

//int n = S(12,3)/S(12,3); //12/3/12/3

//int n = S(1 + 2, 3 + 4);//1+2/3+4

int n = S(7 + 2, 3 + 4)/S(7+2,3+4);

cout << n << endl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值