免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
C -入門語法(五)
仿函數(shù)(函數(shù)對象)
  • 仿函數(shù):將一個(gè)對象當(dāng)作一個(gè)函數(shù)一樣來使用
  • 對比普通函數(shù),它作為對象可以保存狀態(tài)
#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 streamostream &operator<<(ostream &cout, const Point &point) {	return cout << "(" << point.m_x << ", " << point.m_y << ")";}// input streamistream &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;}
運(yùn)算符重載注意點(diǎn)
  • 有些運(yùn)算符不可以被重載,比如
    • 對象成員訪問運(yùn)算符:
    • 域運(yùn)算符:::
    • 三目運(yùn)算符:?:
    • sizeof
  • 有些運(yùn)算符只能重載為成員函數(shù),比如
    • 賦值運(yùn)算符:=
    • 下標(biāo)運(yùn)算符:[ ]
    • 函數(shù)運(yùn)算符:( )
    • 指針訪問成員:->
模板(template)
  • 泛型,是一種將類型參數(shù)化以達(dá)到代碼復(fù)用的技術(shù),C 中使用模板來實(shí)現(xiàn)泛型
  • 模板的使用格式如下
    • template <typename\class T>
    • typename和class是等價(jià)的
  • 模板沒有被使用時(shí),是不會(huì)被實(shí)例化出來的
  • 模板的聲明和實(shí)現(xiàn)如果分離到.h和.cpp中,會(huì)導(dǎo)致鏈接錯(cuò)誤
  • 一般將模板的聲明和實(shí)現(xiàn)統(tǒng)一放到一個(gè).hpp文件中
模板-Array
#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) {		// 擴(kuò)容		cout << "數(shù)組已滿" << 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 << "]";}
#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) {//			// 擴(kuò)容//			cout << "數(shù)組已滿" << 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;}
#include "Swap.h"http://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;//}
#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);
#pragma oncetemplate <class T> void swapValues(T &v1, T &v2) {	T tmp = v1;	v1 = v2;	v2 = tmp;}
#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;}
類型轉(zhuǎn)換
  • C語言風(fēng)格的類型轉(zhuǎn)換符
    • (type)expression
    • type(expression)
  • C 中有4個(gè)類型轉(zhuǎn)換符
    • static_cast
    • dynamic_cast
    • reinterpret_cast
    • const_cast
    • 使用格式:xx_cast(expression)
#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;}
來源:https://www.icode9.com/content-1-524301.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C 技巧:二維動(dòng)態(tài)數(shù)組類模板
c++中運(yùn)算符的一些不同的使用 歐洲的有些需要這樣
71、STL迭代器技術(shù)
運(yùn)算符重載
C++運(yùn)算符重載(2)
C++模板類中友元函數(shù)重載輸出運(yùn)算符
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服