C/C++快速回顾

C/C++的库参考大全:http://www.cplusplus.com/reference/

C语言:

C语言的入口方法:main(int argc , const char* argv[])
int argc指控制台传入的参数个数,argv是传入的值


宏定义:#define Pi 3.14 //在编译阶段替换
宏方法:#define MAX(a,b) \
        a>b?a:b

C中的switch需要写break;,否则会一直往下执行,而GO不用


//结构体就是一个对象,一个对象里面有许多方法
结构体指针:(1个mallco对应一个free)
struct people *p = malloc(sizeof(struct people));
p->age = 10;
free(p);


函数指针:void (*p)()  //sayhello();
p = sayhello;
p();//调用sayhello函数


typedef自定义类型:typedef struct{int age;}people; people p; p.age=10;
typedef void(*Func)(); Func p = sayhello; p(); 


//C的结构体内不能有函数,但可以用函数指针
//面向对象只是一种思想。对象应该有创建,销毁,初始化等的方法


自定义头文件:
#ifndef gongcheng_mok_h
#define gongcheng_mok_h
void sayHello();
#endif


文件操作:
FILE *f = fopen("data.txt","w");
fprintf(f,"hello world\n");
fseelk(f,0,SEEK_END);//光标调整到文件末尾,开头是SEEK_SET
fclose(f);


创建随机数:
#include<stdlib.h>
#include<time.h>
srand((int)time(NULL));
int randNum = rand();

C++:

c++字符串:
#include<string>
#include<sstream>
using namespace std;
string str;
str="hello";
str+=" world";
stringstream ss; //拼接不同类型的数据
ss<<"hello";
ss<<200;
cout<<ss.str()<<endl;


//C++命名空间,区分同名的东西,避免重名冲突:
namespace mok{int age;}    //mok::age = 10;
或者using namespace mok    //age = 10; 


c++面向对象:(可直接在对象中添加函数方法,C要用函数指针)
class People{
public:
	void hello(){
	printf("hello world\n");
	}	
}
int main(int argc,const char* argv[]){
	People *p = new People();
	p->hello();
	delete p;//一个new对应一个delete
	return 0;
}
//一般在头文件写声明,在CPP中实现方法,::相当于中文"的"
//指针的好处是占用空间少,因为是一个地址而已,所以有时传指针效率高

 
构造和析构方法:
class Object{
public:
	Object(){};//创建对象的同时执行构造方法(new)
	~Object(){};//销毁对象的同时执行析构方法(delete)
}


类的继承:
class Man:public People{}        //Man类继承People类的成员和方法
Man::Man(int age):People(age,1){}//子类调用父类的构造方法
People::hello();                 //调用父类的函数方法



虚函数和纯虚函数:
People *p = new Man(20);
p->hello();
//如果父类和子类都不是虚函数,则执行父类的hello方法
//如果父类和子类都是virtual虚函数,则执行子类的hello方法
//如果父类的虚函数hello()=0,子类实现hello方法,则hello为纯虚函数


函数重载:函数名相同,传入参数不同,根据传入的参数定位函数方法
void hello(){}
void hello(char* name){}


运算符重载:(针对类对象)
void operator+=(Point p){add(p);}
Point *p = new Point(5,5);
(*p)+=Point(10,10);


伪函数:(把一个类当成函数对待,通过对括号运算符进行重载)
class hello{
public:
	void operator()(){
		printf("hello world");
	}
};
hello p;
p();


引用:不拷贝,直接覆盖(相当于指针)
void add(Point &p){this->x+=p.x;this->y=p.y;}
Point p(1,1);
Point p2(2,2);
p.add(p2);


友元类:访问其他类的私有成员
class A{
	friend class B;
	int num = 10;
};

class B{
public:
	B(){
		A a;
		printf("%d\n",a.num);
	}
};


c++标准库容器:(list,其他参考cplusplus.com)
#include<list>
using namespace std;
list<string> l;
l.push_back("hello world");
l.push_back("Brand new day");
for(list<string>::iterator it = l.begin(); it!=l.end(); it++)
{cout<<(*it)<<endl;}


c++文件操作:
#include<fstream>
#include<sstream>
using namespace std;
ofstream of("data.txt");//写文件
of<<"hello world"<<endl;
of.close();
ifstream inf("data.txt")//读文件
stringbuf buf;
inf>>&buf;
cout<<buf.str()<<endl;



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值