一、实验目的和要求:
1.理解模板的作用。
2.掌握函数模板的声明方法和模板函数的生成方法。
3.掌握类模板的声明方法和模板类的生成方法。
4.掌握运算符重载的基本方法;
5.掌握c++函数的重载机制;
二、实验内容和要求
1. 编写一求两个数的最大值的函数Max,要求用模板实现对任意数据类型数据都可应用该函数求取结果,在main()函数中分别用整型、实型、字符型数据进行测试。
2.编写一冒泡排序的函数模板,能够对不同类型的数据进行排序
3.试编写一个栈的类模板(包括其成员函数定义),以便为任何类型的对象提供栈结构数据的操作。操作至少包括:入栈和出栈操作。
4.
用运算符重载设计复数类,实现复数的+、-、*、/和<<运算
#include <iostream> using namespace std; template<typename T> T Max(T t1, T t2) { if (t1 >= t2) return t1; else return t2; } int main() { cout <<" Max(2.2, 3.3) "<< Max(2.2, 3.3) << endl; cout << " Max(4, 3) " << Max(4, 3) << endl; cout << " Max('b', 'c') " << Max('b', 'c') << endl; return 0; }
2.
#include "iostream" using namespace std; template<typename T> void Paixu(T array[],int n) { T temp; int i, j; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } int main() { int i = 0; int A[] = { 1,3,2,4,6,5 }; Paixu(A, 6); for (i = 0; i < 6; i++) { cout << A[i] << " "; } cout << endl; double B[] = { 1.2,3.1,2.1, 5.1, 6.2,4.2 }; Paixu(B, 6); for (i = 0; i < 6; i++) { cout << B[i] << " "; } cout << endl; char C[] = { 'a','s','w','y','c','d' }; Paixu(C, 6); for (int i = 0; i < 6; i++) { cout << C[i]<<" "; } cout << endl; return 0; }
3.
#include "iostream" using namespace std; const int depth = 20; template <typename T> class Stack { public: Stack() { sp = 0; } T pop(); void push(T data); private: T stack[depth]; int sp; }; template <typename T> T Stack<T>::pop() { if (sp == 0) { cout << "Stack is empty" << endl; } if (sp > 0) { return stack[--sp]; } return 0; } template <typename T> void Stack<T>::push(T data) { if (sp < depth) { stack[sp++] = data; } else { cout << "Stack is full" << endl; } } int main() { Stack<int>z; int a; z.push(1); z.push(2); z.push(3); z.push(4); for (int i = 0; i < 4; i++) { a = z.pop(); cout << a << " "; } cout << endl; char b; z.push('a'); z.push('b'); z.push('c'); z.push('d'); for (int i = 0; i < 4; i++) { b = z.pop(); cout << b << " "; } cout << endl; return 0; }
4.
#include "iostream" using namespace std; class Complex { public: double real,imag; Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c) { Complex temp; temp.real=real+c.real; temp.imag=imag+c.imag; return temp; } Complex operator-(Complex &c) { Complex temp; temp.real=real-c.real; temp.imag=imag-c.imag; return temp; } Complex operator*(Complex &c) { Complex temp; temp.real=real*c.real-imag*c.imag; temp.imag=imag*c.real+real*c.imag; return temp; } Complex operator/(Complex &c) { Complex temp; temp.real=(real*c.real-imag*c.imag)/(c.real*c.real+c.imag*c.imag); temp.imag=(imag*c.real-real*c.imag)/(c.real*c.real+c.imag*c.imag); return temp; } void show() { if(imag>=0) cout<<real<<"+"<<imag<<"i"<<endl; if(imag<0) cout<<real<<imag<<"i"<<endl; } }; int main() { Complex com(2 , 4); Complex com2(1 ,-3); Complex com3; cout<<"com=2+4i "<<"com3=1-3i"<<endl; com3=com2+com; cout<<"com3=com+com2= "; com3.show(); com3=com-com2; cout<<"com3=com-com2= "; com3.show(); com3=com*com2; cout<<"com3=com*com2= "; com3.show(); com3=com/com2; cout<<"com3=com/com2= "; com3.show(); return 0; }
5.
#include <iostream> using namespace std; class A { private: int x,y; public: A(){} A(int a,int b) { x=a; y=b; } friend istream & operator>>(istream &is,A &obj); //声名为友元函数 friend ostream & operator<<(ostream &os,A &obj); }; ostream &operator <<(ostream &os,A &obj) //输出运算符重载 { os<<"x="<<obj.x<<" y="<<obj.y<<endl; return os; } istream &operator>>(istream &is,A &obj) //输入运算符重载 { is>>obj.x; is>>obj.y; return is; } int main() { A p1(1 ,2); A p2; cout<<p1<<endl; cout<<"input x y="<<endl; cin>>p2; cout<<p2; return 0; }