当前位置: 首页 > article >正文

期末速成C++【大题汇总完】

目录

1.单例模式

2.RTTI

3.删除小于平均数的元素-无效迭代器

4.排序

5.函数/运算符重载声明

6.复数类

7.点和三角形

总结


1.单例模式

#include<iostream>
using namespace std;

//单例模式
class Foo {
public:
	//类的构造析构函数(略)
	//针对name GetName-return SetName-name=str
	string GetName() { return name; }
	void SetName(string str) { name = str; }//注意!列表初始化是构造函数特有的(:),域操作符(::)

	//针对静态变量_instance-static
	static Foo* GetInstance() 
	{
		if (_instance == nullptr)
		{
			_instance = new Foo;
		}
		return _instance;
	}

private:
	Foo() {};//私有构造函数
	static Foo* _instance;
	string name;
};

//_instance初始化为nullptr
Foo* Foo::_instance = nullptr;

//类外_instance
//Foo* Foo::GetInstance()
//{
//	if (_instance == nullptr)
//	{
//		_instance = new Foo;
//	}
//	return _instance;
//}

//域外实现
//string Foo::GetName()
//{
//	return name;
//}

//void Foo::SetName(string str)
//{
//	name = str;
//}

void Fun()
{
	Foo* q = Foo::GetInstance();
	q->SetName("Alice");
}
int main()
{
	Foo* p = Foo::GetInstance();//Foo::
	p->SetName("Bob");
	Fun();
	cout << p->GetName() << endl;
}

2.RTTI

#include<iostream>
#include<vector>
#include<list>
#include<fstream>
using namespace std;
//RTTI
//animal动物-Bird鸟-Dog狗-Cat猫
//1.动物都会吃饭
//2.动物都会叫,鸟-叽叽,狗-汪汪
//3.鸟-飞,狗-跑

//1.容器指针
//2.文件读写入-ifs
// 3.重载
//4.遍历RTTI

class Animal {
public:
	Animal(string str):name(str){}
	virtual ~Animal(){}
	//1.共同的
	void Eat() { cout << "吃饭饭" << endl; }
	//2.共同-方式不同的-纯虚析构函数
	virtual void shout() = 0;
private:
	string name;
};

//域外-构造析构
//Animal::Animal(string str) :name(str){}
//Animal::~Animal() {}//在类外实现的时候,虚析构函数是不带virtual关键字
//void Animal::Eat()
//{
//	cout << "吃饭饭" << endl;
//}

//鸟
class Bird :public Animal {
public:
	Bird(string str):Animal(str){}
	~Bird(){}
	//2.
	void shout() { cout << "吱吱" << endl; }
	//3.
	void Fly() { cout << "飞" << endl; }
};

//void Bird::shout() { cout << "吱吱" << endl; }
//void Bird::Fly() { cout << "飞" << endl; }

class Dog :public Animal {
public:
	Dog(string str):Animal(str){}
	~Dog(){}
	//2.
	void shout() { cout << "汪汪" << endl; }
	//3.
	void Run() { cout << "跑" << endl; }

};

//void Dog::shout() { cout << "汪汪" << endl; }
//void Dog::Run() { cout << "跑" << endl; }


//重载
ifstream& operator>>(ifstream& ifs, vector<Animal*>& all)
{
	string type,name;
	ifs >> type >> name;

	if (type == "狗")
	{
		all.push_back(new Dog(name));
	}
	if (type == "鸟")
	{
		all.push_back(new Bird(name));
	}
	return ifs;
}

int main()
{
	vector<Animal*>allAnimal;//容器里面是类指针一个一个的狗、鸟

	//文件操作ios:in-以读的方式从date文件输出ifs
	ifstream ifs("data.txt", ios::in);
	while (!ifs.eof())//ifs流输出
	{
		ifs >> allAnimal;
	}
	ifs.close();

	//遍历
	for (Animal* p : allAnimal)
	{
		p->Eat();
		p->shout();

		//3.
		if (typeid(*p) == typeid(Dog))
		{
			Dog* pd = dynamic_cast<Dog*>(p);
			pd->Run();
		}
		if (typeid(*p) == typeid(Bird))
		{
			Bird* pb = dynamic_cast<Bird*>(p);
			pb->Fly();
		}
	}
	//释放
	for (Animal* pt : allAnimal)
	{
		delete pt;
	}
}

3.删除小于平均数的元素-无效迭代器

//删除小平平均数的元素-无效迭代器
//1.插入10个随机数-打印显示出来
//2.计算平均数-输出平均数
//3.for循环遍历:删除小平平均数的元素-打印出来
//拼写正确

#include <iostream> 
#include <list>
#include<vector>
#include <algorithm> 
#include <numeric>   
using namespace std;

int main()
{
	vector<int> all;
	//1.
	generate_n(back_inserter(all), 10, []() {return rand() % 100; });
	copy(all.begin(), all.end(), ostream_iterator<int>(cout, " "));

	//2.
	float ave = static_cast<float>(accumulate(all.begin(), all.end(), 0)) / all.size();
	cout << "ave:" << ave << endl;

	//3.
	vector<int>::iterator itr;//指针
	for (auto itr = all.begin(); itr != all.end(); )
	{
		if (*itr < ave)
		{
			itr = all.erase(itr);
		}
		else
		{
			itr++;
		}
	}
}

4.排序

//排序
//1.默认
//2.greate
//3.二元谓词cop1
//4.lambda表达式
//5.重载cop2类

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool cop1(float f1, float f2)
{
	return ((int)f1 % 10) > ((int)f2 % 10);
}

class cop2 {
public:
	cop2(int cen):center(cen){}
	bool operator()(float f1, float f2)
	{
		return (abs(f1 - center)) < (abs(f2 - center));
	}
private:
	int center;
};
int main()
{
	vector<float> vc;
	sort(vc.begin(), vc.end());//默认升序
	sort(vc.begin(), vc.end(), greater<float>());//默认降序greater<float>()
	sort(vc.begin(), vc.end(), cop1);//二元谓词-不传参-降序
	int cen = 99;
	sort(vc.begin(), vc.end(), [cen](float f1, float f2) { return (abs(f1 - cen)) > (abs(f2 = cen)); });//lambda表达式
	sort(vc.begin(), vc.end(), cop2(cen));
}

5.函数/运算符重载声明

//重载声明
//1.无参构造函数
//2.拷贝构造函数
//3.类型转换构造函数
//4.析构函数
//5.默认赋值运算符=
//6.++ --
//7.+=
//8.下标运算符a[1]='X' a[1]=6
//9.+ a+b=c
//10.类型转换函数
//11.输出输入友元
#include<iostream>
using namespace std;

class Bar {
	friend Bar& operator<<(iostream& ios, const Bar&);
	friend Bar& operator>>(ifstream& ifs, Bar&);
public:
	Bar();
	Bar(const Bar& other);
	Bar(const string& other);
	~Bar();
	Bar& operator=(const Bar& other);
	Bar& operator++();
	Bar operator--(int);//后置int
	char& operator[](int);
	Bar operator+(const Bar& other);
	operator string();
	//static_cast
	//dynamic_cast
};

6.复数类

//#include<iostream>
#include<iostream>
#include<algorithm>
using namespace std;

//复数类
class Complex {

	friend ostream& operator<<(ostream& o, const Complex& other);
	friend Complex operator+(const Complex& A, const Complex& B)
	{
		return Complex(A.r + B.r, A.i + B.i);
	}
public:
	Complex(int a = 0, int b = 0) :r(a), i(b) {}

	int& operator[](char c)
	{
		if (c == 'r')
			return r;
		if (c == 'i')
			return i;
	}

	Complex operator++(int)
	{
		Complex temp = *this;
		r++, i++;
		return temp;
	}
	explicit operator float()
	{
		return sqrt(r * r + i * i);
	}
private:
	int r, i;//int
};

int main()
{
	Complex a, b;
	//a = ++b;
	a = b++;		// 后置++,参数列表中多一个无用的int 前置++可以有引用
	a = 10 + b;		// 1.自定义全局函数重载 2.C++自己定义的int相加(默认类型转换-类型转换运算符)-不能默认转换只能强制转换,显示调用
	a['r'] = 1, a['i'] = 2;	  // 下标运算符
	float f = static_cast<float>(a); //❗c++静态和动态类型转换都要考❗
	//float f = (float)a;
}

7.点和三角形

//点和三角形
//浮点数,列表初始化,运算符的重载
 
#include<ostream>
#include<array>
class Point {
	friend class Triangle;//声明友元类❗
	
	//输出运算符的重载
	friend ostream& operator<<(ostream& o, const Point& p)//❗
	{
		o << "[" << p. x << "," << p.y <<"]";
		return o;//考❗
	}
 
public:
	Point(int xx,int yy):x(xx),y(yy){} //构造函数列表初始化❗
	~Point();//析构函数
 
	//计算两点距离
	float GetDistance(const Point& other) {
		return sqrt((x - other.x) * (x - other.x) + (y - other.y) * ((y - other.y)));
	}
 
	//+重载 两点相加没有& ❗
	Point operator+ (const Point& other) {
		return Point(x + other.x, y + other.y);
	}
 
private:
	int x, y;//float
 
};
 
class Triangle {
 
	//输出运算符的重载
	friend ostream& operator<<(ostream& o, const Triangle& t)
	{
		o << "[" << t.all[0] << "," << t.all[1] << ","<<t.all[2]<<"}";
		return o;//考❗
	}
 
public:
 
	//三个点-用数组元素 去列表初始化❗
	Triangle(const Point& p1, const Point& p2, const Point& p3) :
	all{p1, p2, p3}{}  //构造函数-列表初始化
 
	//三个边-根据三个点初始化❗
	Triangle(float a, float b, float c) :
	all{ Point{ 0,0 }, Point{ a,0 }, Point{ 0,0 } } 
	{
		all[2].x = ........;
		all[2].y = ........;
 
	}
 
	//计算面积
	float GetArea() {
		//变长
		float a = all[0].GetDistance(all[1]);❗
		float b = all[1].GetDistance(all[2]);
		float c = all[2].GetDistance(all[0]);
 
		float p = (a + b + c) / 2.0f;
		return sqrt(....);
	}
 
	//Triangle t;
	// t[0]=Point{1,2};
	
	//下标运算符重载❗
	Point& operator[](int index) { return all[index]; }
 
private:
	//表示定义了一个名为 all 的数组,该数组存储了3个 Point 类型的元素。
	array<Point, 3> all; //考<>❗
};

总结

1月5号复习,明天考试今天再敲遍代码。老徐一切顺利!
//单例模式

#include<iostream>
using namespace std;
class Foo {
public:
	void SetName(string str) { name = str; }
	string GetName() { return name; }
	static Foo* GetInstance() //❗类外实现不必加static
	{
		if (_instance == nullptr)
		{
			_instance = new Foo;
		}
		return _instance;
	}
private:
	Foo() {};//❗私有构造函数
	static Foo* _instance;
	string name;
};

Foo* Foo::_instance = nullptr;

void Fun()
{
	Foo* pd = Foo::GetInstance();
	pd->SetName("Alice");
}

int main()
{
	Foo* pf = Foo::GetInstance();//❗调用必须加上::
	pf->SetName("Bob");
	Fun();
	cout << pf->GetName() << endl;
}

//RTTI
//人:男生和女生
//吃饭
//体育课-打篮球和跳绳
//玩游戏和逛街
#include<iostream>
#include<list>
#include<fstream>
using namespace std;

class Person {
public:
	Person(string str):name(str){}
	virtual ~Person(){}
	void Eat() { cout << "吃饭" << endl; }
	virtual void PE() = 0;
protected:
	string name;
};

class Boy:public Person //❗继承是:符号
{
public:
	Boy(string str):Person(str){}
	~Boy(){}
	void PE() { cout << "打篮球" << endl; }
	void Game() { cout << "打游戏" << endl; }
};

class Girl :public Person
{
public:
	Girl(string str):Person(str){}
	~Girl() {};
	void PE() { cout << "跳绳" << endl; }
	void Shopping() { cout << "购物" << endl; }
};
ifstream& operator>>(ifstream& ifs, list<Person*>& li)//❗
{
	string type, name;
	ifs >> type >> name;
	if (type == "男生")
	{
		li.push_back(new Boy(name));//❗
	}
	if (type == "女生")
	{
		li.push_back(new Girl(name));
	}
	return ifs;
}
int main()
{
	list<Person*> li;

	ifstream ifs("data.txt", ios::in);//:: ❗
	while (!ifs.eof())
	{
		ifs >> li;
	}
	ifs.close();

	for (Person* p : li)
	{
		p->Eat();
		p->PE();
		if (typeid(*p) == typeid(Boy))//*p❗
		{
			Boy* pb = dynamic_cast<Boy*>(p);
			pb->Game();
		}
		if (typeid(*p) == typeid(Girl))
		{
			Girl* pg = dynamic_cast<Girl*>(p);
			pg->Shopping();
		}
	}

	for (Person* pt : li)
	{
		delete pt;
	}
}

//无效迭代器-删除小于平均数的元素
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
#include<numeric>

using namespace std;
int main()
{
	vector<int> vc;
	generate_n(back_inserter(vc), 10, []() {return rand() % 100; });
	copy(vc.begin(), vc.end(), ostream_iterator<int>(cout, " "));
	float ave = static_cast<float>(accumulate(vc.begin(), vc.end(), 0) )/ vc.size();
	//vector<int>::iterator itr
	for (auto itr = vc.begin(); itr != vc.end(); )
	{
		if (*itr < ave)
		{
			itr = vc.erase(itr);
		}
		else
		{
			itr++;
		}
	}

}



//排序
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
bool cop1(float f1, float f2)
{
	return (int)f1 % 10 > (int)f2 % 10;
}

class cop2 {
public:
	cop2(int cen):center(cen){}
	bool operator()(float f1, float f2)//❗
	{
		return (abs(f1 - center)) > (abs(f2 - center));
	}
private:
	int center;
};

int main()
{
	vector<float> vc;
	sort(vc.begin(), vc.end());//升序
	sort(vc.begin(), vc.end(), greater<float>());//❗
	sort(vc.begin(), vc.end(), cop1);
	int cen = 99;
	sort(vc.begin(), vc.end(), [cen](float f1, float f2) {return (abs(f1 - cen)) > (abs(f1 - cen)); });
	sort(vc.begin(), vc.end(), cop2(cen));

}



//复数类+ 后置++无&
//const 拷贝/类型转换/输出
#include<iostream>
using namespace std;
class Complex {
	friend ifstream& operator>>(ifstream& ifs, Complex& other);
	friend ostream& operator<<(ostream& o, const Complex& other);
	friend Complex operator+(const Complex& c1, const Complex& c2)//+= +
	{
		return Complex(c1.i + c2.i, c1.r + c2.r);
	}
public:
	Complex(){}
	~Complex(){}
	Complex(const Complex& other){}
	Complex(const string other){}
	Complex& operator=(const Complex& other){}

	Complex(int a,int b):r(a),i(b){}
	Complex operator++(int)
	{
		Complex temp = *this;
		i++, r++;
		return temp;
	}
	Complex& operator++()
	{
		i++, r++;
		return *this;
	}
	int& operator[](char c)
	{
		if (c == 'r')
		{
			return r;
		}
		if (c == 'i')
		{
			return i;
		}
	}
	explicit operator float()
	{
		return sqrt(r * r + i * i);
	}
private:
	int r, i;
};

int main()
{
	Complex a,b;
	a = b++;
	a = ++b;
	a['r'] = 1;
	a['i'] = 2;
	a = a + b;
	float f = static_cast<float>(a);
	float f = (float)a;
	//cout << a << b << endl;
	//cin>>a>>b
	return 0;
}
static_cast<>()
dynamic_cast<>()
greater<>()
typeid()
[]() {}
copy---ostream_iterator<>()
accumulate(, , 0)
generate_n
back_inserter(vc)//往谁里面插入
vector<>::iterator itr 
auto itr=li.begin()
explicit
cosnt & operator

http://www.kler.cn/a/470797.html

相关文章:

  • OpenCV 4.5至4.10版本更新概述
  • unity学习12:地图相关的一些基础2, 增加layer种草种树
  • 对话|全年HUD前装将超330万台,疆程技术瞄准人机交互“第一屏”
  • PyCharm简单调试
  • 前后端分离架构设计与实现:构建现代Web应用的基石
  • 深入刨析数据结构之排序(上)
  • 【工具推荐】XSS 扫描器-XSStrike
  • 基于fMRI数据计算脑脊液(CSF)与全脑BOLD信号的时间耦合分析
  • 进行电商系统的开发
  • 使用 Nginx 轻松处理跨域请求(CORS)
  • 在vue3中根据需要展示特定国家的国旗
  • Postman + Jenkins + Report 集成测试
  • 在 ASP.NET CORE 中上传、下载文件
  • train_args = TrainingArguments()里面的全部参数使用
  • 中电金信携手华为发布“全链路实时营销解决方案”,重塑金融营销数智新生态
  • 设计模式-结构型-适配器模式
  • flutter 专题二十四 Flutter性能优化在携程酒店的实践
  • 计算机毕业设计Python+Vue.js游戏推荐系统 Steam游戏推荐系统 Django Flask 游 戏可视化 游戏数据分析 游戏大数据 爬虫
  • AI巡检系统在安全生产管理中的创新应用
  • 游戏引擎学习第74天
  • Redis 数据库源码分析
  • Opencv实现Sobel算子、Scharr算子、Laplacian算子、Canny检测图像边缘
  • stm32 移植RTL8201F(正点原子例程为例)
  • Easyexcel-4.0.3读取文件内容时遇到“java.lang.ClassNotFoundException”
  • 《从入门到精通:蓝桥杯编程大赛知识点全攻略》(二)-递归实现组合型枚举、带分数问题
  • libaom 源码分析线程结构