前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++-入门语法(五)

C++-入门语法(五)

作者头像
cwl_java
发布2019-10-28 10:27:30
2920
发布2019-10-28 10:27:30
举报
文章被收录于专栏:cwl_Javacwl_Java

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42528266/article/details/102759184

仿函数(函数对象)
  • 仿函数:将一个对象当作一个函数一样来使用
  • 对比普通函数,它作为对象可以保存状态
代码语言:javascript
复制
#include <iostream>
using namespace std;

//int sum(int a, int b) {
//	return a + b;
//}

class Sum {
	int m_age;
public:
	Sum(int age) :m_age(age) { }
	int operator()(int a, int b) {
		if (this->m_age > 10) {
		}
		else {
		}
		return a + b;
	}
};

class Point {
	friend ostream &operator<<(ostream &, const Point &);
public:
	int m_x;
	int m_y;
	Point(int x, int y) :m_x(x), m_y(y) { }


};

// output stream
ostream &operator<<(ostream &cout, const Point &point) {
	return cout << "(" << point.m_x << ", " << point.m_y << ")";
}

// input stream
istream &operator>>(istream &cin, Point &point) {
	return cin >> point.m_x >> point.m_y;
}

int main() {
	Point p1(10, 20);
	cin >> p1;
	cout << p1 << endl;

	// Sum sum(20);

	// cout << sum(10, 20) << endl;
	// cout << sum.operator()(10, 20) << endl;
	getchar();
	getchar();
	return 0;
}
运算符重载注意点
  • 有些运算符不可以被重载,比如
    • 对象成员访问运算符:
    • 域运算符:::
    • 三目运算符:?:
    • sizeof
  • 有些运算符只能重载为成员函数,比如
    • 赋值运算符:=
    • 下标运算符:[ ]
    • 函数运算符:( )
    • 指针访问成员:->
模板(template)
  • 泛型,是一种将类型参数化以达到代码复用的技术,C++中使用模板来实现泛型
  • 模板的使用格式如下
    • template <typename\class T>
    • typename和class是等价的
  • 模板没有被使用时,是不会被实例化出来的
  • 模板的声明和实现如果分离到.h和.cpp中,会导致链接错误
  • 一般将模板的声明和实现统一放到一个.hpp文件中
模板-Array
代码语言:javascript
复制
#include <iostream>
using namespace std;

template <class Item>
class Array {
	friend ostream &operator<<<>(ostream &, const Array<Item> &);
	int m_size = 0;
	int m_capacity = 0;
	Item *m_data = NULL;
public:
	Array(int capacity);
	~Array();
	void add(Item value);
	Item get(int index);
	int size();
	Item operator[](int index);
	void display();
};

template <class Item>
Array<Item>::Array(int capacity) {
	if (capacity <= 0) return;

	this->m_data = new Item[capacity]{};
	this->m_capacity = capacity;
}

template <class Item>
Array<Item>::~Array() {
	if (!this->m_data) return;

	delete[] this->m_data;
	this->m_data = NULL;
}

template <class Item>
void Array<Item>::add(Item value) {
	if (this->m_size == this->m_capacity) {
		// 扩容
		cout << "数组已满" << endl;
		return;
	}
	this->m_data[this->m_size++] = value;
}

template <class Item>
Item Array<Item>::get(int index) {
	if (index < 0 || index >= this->m_size) return 0;
	return this->m_data[index];
}

template <class Item>
int Array<Item>::size() {
	return this->m_size;
}

template <class Item>
Item Array<Item>::operator[](int index) {
	return get(index);
}

template <class Item>
void Array<Item>::display() {
	cout << "[";
	for (int i = 0; i < this->m_size; i++) {
		cout << this->m_data[i];
		if (i != this->m_size - 1) {
			cout << ", ";
		}
	}
	cout << "]" << endl;
}

template <class Item>
ostream &operator<<<>(ostream &cout, const Array<Item> &array) {
	cout << "[";
	for (int i = 0; i < array.m_size; i++) {
		cout << array.m_data[i];
		if (i != array.m_size - 1) {
			cout << ", ";
		}
	}
	return cout << "]";
}
代码语言:javascript
复制
#include <iostream>
#include "Array.hpp"
using namespace std;

//template <class Item> 
//class Array {
//	// friend ostream &operator<<(ostream &, const Array &);
//	int m_size = 0;
//	int m_capacity = 0;
//	Item *m_data = NULL;
//public:
//	Array(int capacity) {
//		if (capacity <= 0) return;
//
//		this->m_data = new Item[capacity] {};
//		this->m_capacity = capacity;
//	}
//
//	~Array() {
//		if (!this->m_data) return;
//
//		delete[] this->m_data;
//		this->m_data = NULL;
//	}
//
//	void add(Item value) {
//		if (this->m_size == this->m_capacity) {
//			// 扩容
//			cout << "数组已满" << endl;
//			return;
//		}
//		this->m_data[this->m_size++] = value;
//	}
//
//	Item get(int index) {
//		if (index < 0 || index >= this->m_size) return 0;
//		return this->m_data[index];
//	}
//
//	int size() {
//		return this->m_size;
//	}
//
//	Item operator[](int index) {
//		return get(index);
//	}
//
//	void display() {
//		cout << "[";
//		for (int i = 0; i < this->m_size; i++) {
//			cout << this->m_data[i];
//			if (i != this->m_size - 1) {
//				cout << ", ";
//			}
//		}
//		cout << "]" << endl;
//	}
//};

//ostream &operator<<(ostream &cout, const Array &array) {
//	cout << "[";
//	for (int i = 0; i < array.m_size; i++) {
//		cout << array.m_data[i];
//		if (i != array.m_size - 1) {
//			cout << ", ";
//		}
//	}
//	return cout << "]";
//}

class Person {
	friend ostream &operator<<(ostream &, const Person &);
	int m_age;
public:
	Person(int age = 0) :m_age(age) { }
};

ostream &operator<<(ostream &cout, const Person &person) {
	return cout << "age=" << person.m_age;
}

int main() {
	/*Array<Person *> array(3);
	array.add(new Person(11));
	array.add(new Person(12));
	array.add(new Person(13));
	array.display();*/

	Array<Person> array(3);
	array.add(Person(11));
	array.add(Person(12));
	array.add(Person(13));
	// array.display();

	cout << array << endl;

	/*Array<int> array(5);
	array.add(11);
	array.add(22);
	array.add(33);
	array.add(44);
	array.add(55);
	array.display();

	Array<double> array2(3);
	array2.add(10.8);
	array2.add(10.9);
	array2.add(10.4);
	array2.display();*/

	// cout << array << endl;

	/*Array array;
	array.add(10);
	array.add(20);
	array.add(11);
	array.add(22);

	array.get(2);
	array[2];

	array.size() == 4;

	array.remove(3);
	array.insert(1, 30);*/

	getchar();
	return 0;
}
代码语言:javascript
复制
#include "Swap.h"



//void swapValues(int &v1, int &v2) {
//	int tmp = v1;
//	v1 = v2;
//	v2 = tmp;
//}
//
//void swapValues(double &v1, double &v2) {
//	double tmp = v1;
//	v1 = v2;
//	v2 = tmp;
//}
代码语言:javascript
复制
#pragma once

// template <class T> void swapValues(T &v1, T &v2);

template <class T> 
void swapValues(T &v1, T &v2) {
	T tmp = v1;
	v1 = v2;
	v2 = tmp;
}

//void swapValues(int &v1, int &v2);
//void swapValues(double &v1, double &v2);
代码语言:javascript
复制
#pragma once

template <class T> void swapValues(T &v1, T &v2) {
	T tmp = v1;
	v1 = v2;
	v2 = tmp;
}
代码语言:javascript
复制
#include <iostream>
#include "Swap.hpp"
using namespace std;

void test() {
	double a = 20.8;
	double b = 30.4;
	swapValues(a, b); 
	cout << "test() a = " << a << ", b = " << b << endl;
}
类型转换
  • C语言风格的类型转换符
    • (type)expression
    • type(expression)
  • C++中有4个类型转换符
    • static_cast
    • dynamic_cast
    • reinterpret_cast
    • const_cast
    • 使用格式:xx_cast(expression)
代码语言:javascript
复制
#include <iostream>
using namespace std;

class Person {
public:
	int m_age;
	virtual void run() { }
};

class Student : public Person {
public:
	int m_score;
};

class Car {

};

void test1const_cast() {
	const Person *p1 = new Person();

	Person *p2 = const_cast<Person *>(p1);
	Person *p3 = (Person *)p1;


	p2->m_age = 20;
	p3->m_age = 30;

	cout << p1->m_age << endl;
}

void test2dynamic_cast() {
	Person *p1 = new Person();
	Person *p2 = new Student();

	/*Student *stu1 = (Student *) p1;
	Student *stu2 = (Student *) p2;
	Car *car = (Car *) p2;*/

	Student *stu1 = dynamic_cast<Student *>(p1);
	Student *stu2 = dynamic_cast<Student *>(p2);
	Car *car = dynamic_cast<Car *>(p2);

	cout << stu1 << endl;
	cout << stu2 << endl;
	cout << car << endl;
}

void test3static_cast() {
	Person *p1 = new Person();
	Person *p2 = new Student();

	Student *stu1 = static_cast<Student *>(p1);
	Student *stu2 = static_cast<Student *>(p2);

	int i = 10;
	double d = i;

	cout << stu1 << endl;
	cout << stu2 << endl;
}

int main() {
	/*int a = 10;

	double d1 = (double) a;
	double d2 = double(a);

	cout << d1 << endl;
	cout << d2 << endl;*/

	/*Person *p1 = new Person();
	Person *p2 = new Student();

	Student *stu1 = reinterpret_cast<Student *>(p1);
	Student *stu2 = reinterpret_cast<Student *>(p2);
	Car *car = reinterpret_cast<Car *>(p2);

	cout << p1 << endl;
	cout << p2 << endl;
	cout << p2 << endl;

	cout << stu1 << endl;
	cout << stu2 << endl;
	cout << car << endl;*/


	//// 0a 00 00 00
	//int i = 10;
	//// 00 00 00 00 00 00 24 40
	//double d = i;

	//double d1 = 10.0;
	//int i1 = d1;

	// 0a 00 00 00
	// int i = 10;
	// 0a 00 00 00 cc cc cc cc 
	// double d = reinterpret_cast<double&>(i);
	// cout << i << endl;
	// cout << d << endl;



	/*int *p = reinterpret_cast<int *>(100);
	int i = reinterpret_cast<int>(p);*/

	//cout << "i = " << i << endl;
	//cout << "d = " << d << endl;

	//cout << sizeof(i) << endl;
	//cout << sizeof(d) << endl;

	getchar();
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 仿函数(函数对象)
  • 运算符重载注意点
  • 模板(template)
  • 模板-Array
  • 类型转换
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档