乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C -入門語法(五)

       印度阿三17 2019-10-26
      仿函數(shù)(函數(shù)對象)
      • 仿函數(shù):將一個對象當(dāng)作一個函數(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 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
      • 有些運算符只能重載為成員函數(shù),比如
        • 賦值運算符:=
        • 下標(biāo)運算符:[ ]
        • 函數(shù)運算符:( )
        • 指針訪問成員:->
      模板(template)
      • 泛型,是一種將類型參數(shù)化以達(dá)到代碼復(fù)用的技術(shù),C 中使用模板來實現(xiàn)泛型
      • 模板的使用格式如下
        • template <typename\class T>
        • typename和class是等價的
      • 模板沒有被使用時,是不會被實例化出來的
      • 模板的聲明和實現(xiàn)如果分離到.h和.cpp中,會導(dǎo)致鏈接錯誤
      • 一般將模板的聲明和實現(xiàn)統(tǒng)一放到一個.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"
      
      
      
      //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 once
      
      template <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個類型轉(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./content-1-524301.html

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多