C++学习记录


注意

C++中使用new创建delete销毁,而C中使用malloc创建free销毁。

C++类class{}后一定要加分号;。


用例

单文件写类

此工程仅由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class People
{
public:
	People()
	{
	};
	~People()
	{
	}
	void sayHello()
	{
		std::cout << "Hello!" << std::endl;
	}
};

int main(int argc, const char* argv[])
{
	People *p = new People();
	p->sayHello();
	delete p;

	//system("pause");
	return 0;
}

按类分写

此工程由三个文件组成,分别是main.cpp, People.h, People.cpp。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include "People.h"

int main(int argc, const char* argv[])
{
	People *p = new People();
	p->sayHello();
	delete p;

	//system("pause");
	return 0;
}

People.h如下:

//People.h

#pragma once
class People
{
public:
	People();
	~People();
	void sayHello();
};

People.cpp如下:

//People.cpp

#include <iostream>
#include "People.h"

People::People()
{
}


People::~People()
{
}

void People::sayHello()
{
	std::cout << "Hello!" << std::endl;
}

使用命名空间

此工程由三个文件组成,分别是main.cpp, People.h, People.cpp。其中对于main.cpp列举了两种使用方法,两种方法选其一即可。

main.cpp使用方法一

//main.cpp
//方法一:不使用using namespace

#include <iostream>
#include <stdlib.h>
#include "People.h"

int main(int argc, const char* argv[])
{
	LQX::People *p = new LQX::People();
	p->sayHello();
	delete p;

	system("pause");
	return 0;
}

main.cpp使用方法二

//main.cpp
//方法二:使用using namespace

#include <iostream>
#include <stdlib.h>
#include "People.h"

using namespace LQX;

int main(int argc, const char* argv[])
{
	People *p = new People();
	p->sayHello();
	delete p;

	//system("pause");
	return 0;
}

People.h如下:

//People.h

#pragma once

namespace LQX
{
	class People
	{
	public:
		People();
		~People();
		void sayHello();
	};
}

People.cpp如下:

//People.cpp

#include <iostream>
#include "People.h"

namespace LQX
{

	People::People()
	{
	}


	People::~People()
	{
	}

	void People::sayHello()
	{
		std::cout << "Hello!" << std::endl;
	}

}

类的继承

此工程由五个文件组成,分别是main.cpp, People.h, People.cpp, Man.h, Man.cpp。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include "Man.h"

int main(int argc, const char* argv[])
{
	Man *m = new Man();
	m->sayHello();
	delete m;

	//system("pause");
	return 0;
}
People.h如下:

//People.h

#pragma once

class People
{
private:
	int age;
	int sex;

public:
	People();
	int getAge();
	int getSex();
	People(int age, int sex);
	void sayHello();
};

People.cpp如下:

//People.cpp

#include <iostream>
#include "People.h"

People::People()
{
	this->age = 10;
	this->sex = 1;
}

int People::getAge()
{
	return this->age;
}

int People::getSex()
{
	return this->sex;
}

People::People(int age, int sex)
{
	this->age = age;
	this->sex = sex;
}

void People::sayHello()
{
	std::cout << "Hello!" << std::endl;
}

Man.h如下:

//Man.h

#pragma once
#include "People.h"

class Man :
	public People
{
public:
	Man();
	~Man();
};

Man.cpp如下:

//Man.cpp

#include "Man.h"

Man::Man()
{
}


Man::~Man()
{
}

构造方法和析构方法

此工程由一个main.cpp文件组成,但分了三种情况演示。

情况一:使用new方法创建自动执行构造,只有使用delete才会执行析构。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object
{
public:
	Object()
	{
		printf("Create Object\n");
	}
	~Object()
	{
		printf("Delete Object\n");
	}
};

int main(int argc, const char* argv[])
{
	Object *obj = new Object();
	delete obj;

	system("pause");
	return 0;
}
结果:

情况二:不使用new和delete,之间创建,不会自动执行析构。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object
{
public:
	Object()
	{
		printf("Create Object\n");
	}
	~Object()
	{
		printf("Delete Object\n");
	}
};

int main(int argc, const char* argv[])
{
	Object obj;

	system("pause");
	return 0;
}
结果:



情况三:使用函数直接创建,函数运行完后自动析构。证明在适用范围中,若无效时自动析构。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object
{
public:
	Object()
	{
		printf("Create Object\n");
	}
	~Object()
	{
		printf("Delete Object\n");
	}
};

void runObject()
{
	Object obj;
}

int main(int argc, const char* argv[])
{
	runObject();

	system("pause");
	return 0;
}

结果:


情况四:确认上情况证明。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object
{
public:
	Object()
	{
		printf("Create Object\n");
	}
	~Object()
	{
		printf("Delete Object\n");
	}
};

void runObject()
{
	Object obj;
}

int main(int argc, const char* argv[])
{
	runObject();
	printf("end\n");

	system("pause");
	return 0;
}

结果:


情况五:证明使用是在有效范围内

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object
{
public:
	Object()
	{
		printf("Create Object\n");
	}
	~Object()
	{
		printf("Delete Object\n");
	}
};

void runObject()
{
	{
		Object obj;//局部变量,括号标识代码块范围
	}
}

int main(int argc, const char* argv[])
{
	runObject();
	printf("end\n");

	system("pause");
	return 0;
}

结果:


情况六:再清晰判断。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object
{
public:
	Object()
	{
		printf("Create Object\n");
	}
	~Object()
	{
		printf("Delete Object\n");
	}
};

void runObject()
{
	Object obj();
	printf("runObject end\n");
}

int main(int argc, const char* argv[])
{
	runObject();
	printf("end\n");

	system("pause");
	return 0;
}

结果:



执行父类的构造方法

此工程由五个文件组成,分别是main.cpp, People.h, People.cpp, Man.h, Man.cpp。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include "Man.h"

int main(int agrc, const char* agrv[])
{
	Man *m = new Man(20);
	printf("age:%d\n", m->getAge());

	system("pause");
	return 0;
}
People.h如下:
//People.h

class People
{
private:
	int age;
	int sex;

public:
	People();
	People(int age, int sex);
	~People();
	int getAge();
	int getSex();
	void sayHello();
};
People.cpp如下,public People中的public意思是从People继承过来的东西是公开的:
//People.cpp

#include "People.h"
#include <iostream>

People::People(int age, int sex)
{
	this->age = age;
	this->sex = sex;
}

People::~People()
{

}

int People::getAge()
{
	return this->age;
}

int People::getSex()
{
	return this->sex;
}

void People::sayHello()
{
	printf("Hello!\n");
}
Man.h如下:
//Man.h
#include "People.h"

class Man :public People
{
public:
	Man(int age);
	~Man();
};
Man.cpp如下,java 用super()执行:
//Man.cpp
#include "Man.h"

Man::Man(int age):People(age, 1)
{

}

Man::~Man()
{

}
结果:


执行父类的方法

此工程由五个文件组成,分别是main.cpp, People.h, People.cpp, Man.h, Man.cpp。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include "Man.h"

int main(int argc, const char* argv[])
{
	Man *m = new Man(21);
	m->sayHello();

	system("pause");
	return 0;
}
People.h如下:

//People.h

class People
{
private:
	int age;
	int sex;

public:
	People();
	People(int age, int sex);
	~People();
	int getAge();
	int getSex();
	void sayHello();
};

People.cpp如下:

//People.cpp

#include "People.h"
#include <iostream>

People::People()
{
	this->age = 10;
	this->sex = 1;
}
People::People(int age, int sex)
{
	this->age = age;
	this->sex = sex;
}

People::~People()
{

}

int People::getAge()
{
	return this->age;
}

int People::getSex()
{
	return this->sex;
}

void People::sayHello()
{
	printf("People say: Hello!\n");
}

Man.h如下:

//Man.h
#include "People.h"

class Man :public People
{
public:
	Man(int age);
	~Man();
	void sayHello();
};
Man.cpp如下:

//Man.cpp
#include "Man.h"
#include <iostream>

Man::Man(int age):People(age, 1)
{

}
//java 用super()执行

Man::~Man()
{

}

void Man::sayHello()
{
	printf("Man say: Hello!\n");
}
结果:


如果将Man.cpp修改为如下,其他不变,则可以用父类的方法。

Man.cpp如下:

//Man.cpp
#include "Man.h"
#include <iostream>

Man::Man(int age):People(age, 1)
{

}
//java 用super()执行

Man::~Man()
{

}

void Man::sayHello()
{
	People::sayHello();
	printf("Man say: Hello!\n");
}
结果:

或者将main.cpp做一下修改,可以直接使用父类的方法。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include "Man.h"

int main(int argc, const char* argv[])
{
	Man *m = new Man(21);
	m->People::sayHello();

	system("pause");
	return 0;
}
结果:


实函数、虚函数、纯虚函数、函数重写

此工程由五个文件组成,分别是main.cpp, People.h, People.cpp, Man.h, Man.cpp。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include "Man.h"

int main(int argc, const char* argv[])
{
	People *p = new Man(20);
	p->sayHello();
	delete p;

	system("pause");
	return 0;
}
People.h如下:

//People.h

class People
{
private:
	int age;
	int sex;

public:
	People();
	People(int age, int sex);
	~People();
	int getAge();
	int getSex();
	void sayHello();
};

People.cpp如下:

//People.cpp

#include "People.h"
#include <iostream>

People::People()
{
	this->age = 10;
	this->sex = 1;
}
People::People(int age, int sex)
{
	this->age = age;
	this->sex = sex;
}

People::~People()
{

}

int People::getAge()
{
	return this->age;
}

int People::getSex()
{
	return this->sex;
}

void People::sayHello()
{
	printf("People say: Hello!\n");
}

Man.h如下:

//Man.h
#include "People.h"

class Man :public People
{
public:
	Man(int age);
	~Man();
	void sayHello();
};
Man.cpp如下:

//Man.cpp
#include "Man.h"
#include <iostream>

Man::Man(int age):People(age, 1)
{

}
//java 用super()执行

Man::~Man()
{

}

void Man::sayHello()
{
	People::sayHello();
	printf("Man say: Hello!\n");
}
结果:

若需使其自动定位,需要将函数定义为虚函数,将People.h和Man.h两个文件做一下修改。

People.h如下:

//People.h

class People
{
private:
	int age;
	int sex;

public:
	People();
	People(int age, int sex);
	~People();
	int getAge();
	int getSex();
	virtual void sayHello();
};

Man.h如下:

//Man.h
#include "People.h"

class Man :public People
{
public:
	Man(int age);
	~Man();
	virtual void sayHello();
};

结果:


纯虚函数可以在其他类中定义

若需使其自动定位,需要将函数定义为虚函数,将main.cpp, People.h和Man.h三个文件做一下修改。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include "Man.h"

int main(int argc, const char* argv[])
{
	People *p = new Man(20);
	p->sayHello();
	p->eat();
	delete p;

	system("pause");
	return 0;
}
People.h如下:

//People.h

class People
{
private:
	int age;
	int sex;

public:
	People();
	People(int age, int sex);
	~People();
	int getAge();
	int getSex();//实现了的实函数
	virtual void sayHello();//实现了的虚函数
	virtual void eat()=0;//定义一个抽象方法,=0是抽象方法标志,未被实现的函数
};
Man.h如下:
//Man.h
#include "People.h"
#include <iostream>

class Man :public People
{
public:
	Man(int age);
	~Man();
	virtual void sayHello();
	virtual void eat()
	{
		printf("Man eat\n");
	}
};
结果:



纯虚类

就是类里面全部都是虚的,可与java里面的单一定义多重实现比较。


函数重载

此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Hello
{
public:
	void sayHello()
	{
		printf("Hello everybody!\n");
	}
	void sayHello(char * name)
	{
		printf("Hello %s\n",name);
	}
};


int main(int argc, const char* argv[])
{
	Hello *h = new Hello();
	h->sayHello();//自动定位到第一个不带参的函数
	printf("-------------\n");
	h->sayHello("zhangsan");//自动定位到第二个带参函数
	printf("-------------\n");
	std::string name = "zhangsan";//这是因为上一个前者会出现不匹配的警告,而实际上直接写"xxx"是属于std的string型
	h->sayHello((char*)name.c_str());//这里要做引用c_str,并且还需强制转换成对应的char*类型

	system("pause");
	return 0;
}
结果:


直接用C++而不带C东西重写程序,可以如下。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <string>

class Hello
{
public:
	void sayHello()
	{
		printf("Hello everybody!\n");
	}
	void sayHello(std::string name)
	{
		std::string str="Hello ";
		str += name;

		std::cout << str << "\n";//记得include <string>
	}
};


int main(int argc, const char* argv[])
{
	Hello *h = new Hello();
	h->sayHello("zhangsan");//自动定位到第二个带参函数

	system("pause");
	return 0;
}
结果:


运算符重载

此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <string>

class Point
{
private:
	int x, y;

public:
	Point(int x, int y)
	{
		this->x = x;
		this->y = y;
	}
	int getX()
	{
		return this->x;
	}
	int getY()
	{
		return this->y;
	}
	void add(Point p)
	{
		add(p.getX(), p.getY());
	}
	void add(int x, int y)
	{
		this->x += x;
		this->y += y;
	}
	void operator+=(Point p)//重载运算符
	{
		add(p);
	}
};


int main(int argc, const char* argv[])
{
	Point p(10,9);
	p.add(Point(12, 11));
	std::cout << p.getY() << "\n";
	printf("------------------\n");
	p += Point(13, 14);
	std::cout << p.getY() << "\n";
	printf("------------------\n");
	Point *pn = new Point(5, 6);//这里取用指针输入
	(*pn) += Point(2, 3);//*号为取其运算符
	std::cout << pn->getY() << "\n";

	system("pause");
	return 0;
}
结果:


伪函数

它不是一个真正的函数,它是一个类或者是一个结构体。此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

void hello()
{
	printf("hello\n");
}

class Hello
{
public:
	void operator()()
	{
		printf("Hello!\n");
	}
};


int main(int argc, const char* argv[])
{
	hello();
	printf("----------------\n");
	Hello h;
	h();

	system("pause");
	return 0;
}
结果:


函数指针

此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object
{
public:
	void(Object::*sayHi)();
};

class Hello :public Object
{
public:
	Hello()
	{
		sayHi = (void (Object::*)())&Hello::HelloSayHi;
		(this->*sayHi)();
	}
	void HelloSayHi()
	{
		printf("Hello!\n");
	}
};

int main(int argc, const char* argv[])
{
	Hello *h = new Hello();
	delete h;

	system("pause");
	return 0;
}
结果:

写得简单一点。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Object;
typedef void(Object::*SayHi)();

class Object
{
public:
	SayHi sayHi;
};

class Hello :public Object
{
public:
	Hello()
	{
		sayHi = (SayHi)(&Hello::HelloSayHi);
		(this->*sayHi)();
	}
	void HelloSayHi()
	{
		printf("Hello!\n");
	}
};

int main(int argc, const char* argv[])
{
	Hello *h = new Hello();
	delete h;

	system("pause");
	return 0;
}
结果:

延时实例

main.cpp如下,但因使用平台非unix,所以没有实现延时,但用了线程的概念:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <thread>
//#include <unistd.h>//unix头文件

class Object;
typedef void(Object::*SayHi)();
typedef void(Object::*CallaterHandler)();

void threadFunc(Object *target, CallaterHandler handler, int delay)
{
	//sleep(delay);
	(target->*handler)();
}
void callater(Object *target, CallaterHandler handler, int delay)
{
	std::thread t(threadFunc, target, handler, delay);
	t.join();
}

#define CH(fp)(CallaterHandler)(&fp)

class Object
{
public:
	SayHi sayHi;
};

class Hello :public Object
{
public:
	Hello()
	{
//		sayHi = (SayHi)(&Hello::HelloSayHi);
//		(this->*sayHi)();
		callater(this, CH(Hello::HelloSayHi), 3);
	}
	void HelloSayHi()
	{
		printf("Hello!\n");
	}
};



int main(int argc, const char* argv[])
{
	Hello *h = new Hello();
	delete h;

	system("pause");
	return 0;
}
结果:


引用

此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class Point
{
private:
	int x, y;
public:
	Point(int x, int y)
	{
		this->x = x;
		this->y = y;
	}
	
	int getX()
	{
		return x;
	}

	int getY()
	{
		return y;
	}

	void add(Point &p)//&为引用
	{
		this->x += p.x;
		this->y += p.y;
	}
};

int main(int argc, const char* argv[])
{
	Point p(1,2);
	p.add(Point(2, 3));
	//printf("------------\n");
	Point p1(3,4);
	p.add(p1);

	system("pause");
	return 0;
}

友元类

将私有类公开给特定的类。此工程由一个main.cpp文件组成。需注意安全。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class A
{
	friend class B;//指明B为A的友元类,会使类间耦合度过高
//private://不加修饰符则默认为私有
	int num;
public:
	A()
	{
		num = 10;
	}
};

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

int main(int argc, const char* argv[])
{
	B b;

	system("pause");
	return 0;
}
结果:



倘若没有继承关系也是可以的。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>

class A
{
	friend class B;//指明B为A的友元类,会使类间耦合度过高
//private://不加修饰符则默认为私有
	int num;
public:
	A()
	{
		num = 10;
	}
};

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

int main(int argc, const char* argv[])
{
	B b;

	system("pause");
	return 0;
}
结果:



标准库容器的基本用法

下面以list容器为例。此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <list>
#include <string>

int main(int argc, const char* argv[])
{
	std::list<std::string>l;
	l.push_back("Hello");
	l.push_back("everybody");

	for (std::list<std::string>::iterator it = l.begin(); it != l.end(); it++)
	{
		std::cout << *it << "\n";
	}

	system("pause");
	return 0;
}
结果:


下面以mapt容器为例。此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <list>
#include <string>
#include <map>

int main(int argc, const char* argv[])
{
	std::list<std::string>l;
	l.push_back("Hello");
	l.push_back("everybody");

	for (std::list<std::string>::iterator it = l.begin(); it != l.end(); it++)
	{
		std::cout << *it << "\n";
	}

	std::map<std::string, std::string>m;
	m.insert(std::pair<std::string, std::string>("hello","Hello everybody"));
	m.insert(std::pair<std::string, std::string>("name", "zhangsan"));
	std::cout << m.at("hello") << "\n";

	system("pause");
	return 0;
}
结果:



再进一步使用重载中括号[]运算符。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <list>
#include <string>
#include <map>

int main(int argc, const char* argv[])
{
	std::list<std::string>l;
	l.push_back("Hello");
	l.push_back("everybody");

	for (std::list<std::string>::iterator it = l.begin(); it != l.end(); it++)
	{
		std::cout << *it << "\n";
	}

	std::map<std::string, std::string>m;
	//m.insert(std::pair<std::string, std::string>("hello","Hello everybody"));
	//m.insert(std::pair<std::string, std::string>("name", "zhangsan"));
	//std::cout << m.at("hello") << "\n";
	m["name"] = "zhangsan";
	std::cout << m["name"] << "\n";

	system("pause");
	return 0;
}
结果:


字符串常用操作

此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, const char* argv[])
{
	string str;
	str += "Hello ";
	str += "everybody";
	cout << str << "\n";

	printf("-------------\n");

	stringstream ss;
	ss << "Hello ";
	ss << 200;
	ss << " ";
	ss << 2.5;
	ss << "Hello!" << "you" << 1000;
	cout << "C++: "<<ss.str() << "\n";//访问到ss的C++字符串
	cout << "C: " << ss.str().c_str() << "\n";//访问到ss的C字符串

	system("pause");
	return 0;
}
结果:



文件操作

此工程由一个main.cpp文件组成。

main.cpp如下:

//main.cpp

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>

using namespace std;

int main(int argc, const char* argv[])
{
	//输出
	ofstream of("data.txt");
	of << "Hello everybody\n";
	of.close();


	//读入
	ifstream inf("data.txt");
	//读取第一个字符
	char c;//如果第一个是整形就是int
	inf >> c;
	std::cout << c << "\n";

	printf("-----------------\n");
	//读取全部
	stringbuf sb;
	inf >> &sb;
	std::cout << sb.str() << "\n";


	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值