c++期末考试题目总结

Ps:  一个知识点一道题 (主要是要通过题目记住语法格式)

考试范围:

  •  一. 单继承
  •  二. 多继承与虚拟继承
  •  三. 虚函数与多态
  •  四. 运算符重载
  •  五. 函数模板与类模板

 


 

一、单继承

       1.   圆和圆柱体计算(单继承)              

#include<bits/stdc++.h>
using namespace std;

class cpoint
{
protected:
    int x,y;
public:
    cpoint(int xval,int yval):x(xval),y(yval)
    {

    }
};

class ccircle:public cpoint
{
protected:
    int r;
public:
    ccircle(int xval,int yval,int rval):cpoint(xval,yval),r(rval)
    {

    }
    double getarea()
    {
        return r*r*3.14;
    }
    void display1()
    {
        cout << "Circle:"<<"("<<x<<","<<y<<")"<<","<<r<<endl	;
    }
};

class ccylinder:public ccircle
{
protected:
    int h;
public:
    ccylinder(int xval,int yval,int rval,int hval):ccircle(xval,yval,rval),h(hval)
    {

    }
    double getv()
    {
        return r*r*3.14*h;
    }
    void display2()
    {
        cout << "Cylinder:"<<"("<<x<<","<<y<<")"<<","<<r<<","<<h<<endl;
    }
};

int main()
{
    int x1,y1,r1;
    cin >> x1>>y1>>r1;
    ccircle a(x1,y1,r1);
    a.display1();
    double p=a.getarea();
    cout <<"Area:"<<p<<endl;
    int x2,y2,r2,h2;
    cin >> x2>>y2>>r2>>h2;
    ccylinder b(x2,y2,r2,h2);
    b.display2();
    double q=b.getv();
    cout <<"Volume:"<<q<<endl;
}

        2. 时钟模拟(单继承)

#include<bits/stdc++.h>
using namespace std;

class cnt
{
protected:
    int value;
public:
    cnt(int val):value(val)
    {

    }
    void increment()
    {
        value++;
    }
};

class whilecnt:public cnt
{
protected:
    int min;
    int max;
    int flag=0;
public:
    whilecnt(int val,int mi,int ma):cnt(val),min(mi),max(ma)
    {

    }
    void increment(int t)
    {
        while(t--)
        {

            if(value+1>max)
            {
                value=min;
                flag++;
            }
            else
                value++;
        }
    }
    int getvalue()
    {
        return value;
    }
    int getflag()
    {
        return flag;
    }
};

class cclock
{
private:
    whilecnt hour;
    whilecnt minute;
    whilecnt sec;
public:
    cclock(int val1,int val2,int val3):hour(val1,0,23),minute(val2,0,59),sec(val3,0,59)
    {

    }
    void time(int s)
    {
        sec.increment(s);
        minute.increment(sec.getflag());
        hour.increment(minute.getflag());
    }
    void display()
    {
        cout<<hour.getvalue()<<":"<<minute.getvalue()<<":"<<sec.getvalue()<<endl;
    }
};

int main()

{
    int t;
    cin >> t;
    while(t--)
    {
        int h;
        int m;
        int s;
        cin>> h>>m>>s;
        cclock p(h,m,s);
        int num;
        cin >> num;
        p.time(num);
        p.display();
    }
}

        3. 存折与信用卡(单继承)

#include<bits/stdc++.h>
using namespace std;

class account
{
protected:
    char id[20];
    char name[20];
    float balance;
public:
    account(char idval[],char nval[],float b)
    {
        strcpy(id,idval);
        strcpy(name,nval);
        balance=b;
    }
    void display()
    {
        cout<<"balance is "<<balance<<endl;
    }
    void save(int num1)
    {
        cout <<"saving ok!"<<endl;
        balance=balance+num1;
        cout <<"balance is "<<balance<<endl;
    }
    void draw1(int num2)
    {
        if(num2>balance)
        {
            cout<<"sorry! over balance!"<<endl;
            cout<<"balance is "<<balance<<endl;
        }
        if(num2<=balance)
        {
            cout <<"withdraw ok!"<<endl;
            balance=balance-num2;
            cout <<"balance is "<<balance<<endl;
        }
    }
};

class credit:public account
{
protected:
    float limit;
public:
    credit(char idval[],char nval[],float b,float l):account(idval,nval,b),limit(l)
    {

    }
    void draw2(int num2)
    {
        if(num2>balance+limit)
        {
            cout <<"sorry! over limit!"<<endl;
            cout<<"balance is "<<balance;
        }
        if(num2<=balance+limit)
        {
            cout <<"withdraw ok!"<<endl;
            balance=balance-num2;
            cout <<"balance is "<<balance;
        }
    }
};

int main()
{
    char i[20];
    char n[20];
    float ba;
    float li;
    int n1;
    int n2;
    cin >> i >> n >> ba;
    account p(i,n,ba);
    p.display();
    cin >> n1;
    p.save(n1);
    cin >> n2;
    p.draw1(n2);
    cin >> i >> n >> ba >> li;
    credit q(i,n,ba,li);
    q.display();
    cin>> n1;
    q.save(n1);
    cin >> n2;
    q.draw2(n2);
}

        4.点距离计算(单继承)

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//假设点坐标均为整数 
class Point {
protected:
	int x;
public:
	Point(int);
	void distance(); //计算当前点到原点的距离,并输出结果信息
};

class Point_2D: public Point {
protected:
	int y;
public:
	Point_2D(int, int); //有参构造,设置二维平面的x\y坐标
	void distance(); //计算当前点到原点的距离,并输出结果信息
};

class Point_3D: public Point_2D {
protected:
	int z;
public:
	Point_3D(int, int, int); //有参构造,设置三维平面的x\y\z坐标
	void distance();//计算当前点到原点的距离,并输出结果信息
	//三维点到原点距离,等于x\y\z坐标平方的总和的开方
};

//完成三个类实现的填空 
Point::Point(int xval):x(xval)
{

}
void Point::distance()
{
    float dist1=x*x;
    cout<<"Distance between ["<<x<<"] and [0] = "<<fixed<<setprecision(2)<<sqrt(dist1)<<endl;
}

Point_2D::Point_2D(int xs,int yval):Point(xs)
{
    y=yval;
}
void Point_2D::distance()
{
    float dist2=x*x+y*y;
    cout<<"Distance between ["<<x<<", "<<y<<"] and [0, 0] = "<<fixed<<setprecision(2)<<sqrt(dist2)<<endl;
}

Point_3D::Point_3D(int xss,int yss,int zval):Point_2D(xss,yss)
{
    z=zval;
}
void Point_3D::distance()
{
    float dist3=x*x+y*y+z*z;
    cout<<"Distance between ["<<x<<", "<<y<<", "<<z<<"] and [0, 0, 0] = "<<fixed<<setprecision(2)<<sqrt(dist3)<<endl;
}
//主函数如下 
int main()
{	int num,tx, ty, tz;
	cin>>num;
	while (num)
	{	switch (num) {
		case 1: 
			{	cin>>tx;
				Point p1(tx);
				p1.distance();
				break;
			}
			case 2:
			{	cin>>tx>>ty;
				Point_2D p2(tx, ty);
				p2.distance();
				break;
			}
			case 3:
			{	cin>>tx>>ty>>tz;
				Point_3D p3(tx, ty, tz);
				p3.distance();
				break;
			}
		}
		cin>>num;
	}
	return 0;

}

        5.三维空间的点(单继承)

#include<bits/stdc++.h>
using namespace std;

class p2d
{
protected:
    double x;
    double y;
public:
    p2d()
    {

    }
    p2d(double xval,double yval):x(xval),y(yval)
    {

    }
    double getdistance1()
    {
        return sqrt(x*x+y*y);
    }
};

class p3d:public p2d
{
protected:
    double z;
public:
    p3d()
    {

    }
    p3d(double xval,double yval,double zval):p2d(xval,yval),z(zval)
    {

    }
    double getdistance2()
    {
        return sqrt(x*x+y*y+z*z);
    }
};

int main()
{
    int x1,y1;
    int x2,y2,z2;
    int x3,y3,z3;
    cin >> x1 >> y1;
    p2d p1(x1,y1);
    cout << p1.getdistance1()<<endl;
    cin >> x2 >> y2 >> z2;
    p3d p2(x2,y2,z2);
    cout << p2.getdistance2()<<endl;
    cin >> x3 >> y3 >> z3;
    p3d p3(x3,y3,z3);
    cout << p3.getdistance2()<<endl;
    p2d p4;
    p4=p3;
    cout << p4.getdistance1();
}

        6.OOP驾考预约(单继承)

#include<bits/stdc++.h>
using namespace std;

class cperson
{
protected:
    char name[20];
    string tell;
public:
    cperson()
    {

    }
    cperson(char nval[],string tval):tell(tval)
    {
        strcpy(name,nval);
    }
};

class f:public cperson
{
protected:
    int atime[12];
public:
    f()
    {

    }
    f(char nval[],string tval,int a[]):cperson(nval,tval)
    {
        int i;
        for(i=0; i<12; i++)
            atime[i]=a[i];
    }
    void cheak1()
    {
        int i;
        int cnt=0;
        for(i=0; i<12; i++)
            if(atime[i]>=60)
                cnt++;
        if(cnt>=10)
            cout <<name<<"达到学时要求可以预约!电话"<<tell<<endl;
        else
            cout <<name<<"未达到学时要求不能预约!电话"<<tell<<endl;
    }
};

class v:public f
{
protected:
    int btime[12];
public:
    v()
    {

    }
    v(char nval[],string tval,int a[],int b[]):f(nval,tval,a)
    {
        int i;
        for(i=0; i<12; i++)
            btime[i]=b[i];
    }
    void cheak2()
    {
        int i;
        int cnt=0;
        for(i=0; i<12; i++)
        {
            if(atime[i]>=60)
                cnt++;
            if(btime[i]>=60&&btime[i]<120)
                cnt++;
            if(btime[i]>=120)
                cnt+=2;
        }
        //cout << cnt<<endl;
        if(cnt>=10)
            cout <<name<<"达到学时要求可以预约!电话"<<tell<<endl;
        else
            cout <<name<<"未达到学时要求不能预约!电话"<<tell<<endl;
    }
};

int main()
{
    int t;
    int i;
    cin >> t;
    while(t--)
    {
        char as;
        cin >> as;
        //char ch=getchar();
        if(as=='F')
        {
            char na[20];
            string phone;
            int as[12];
            cin >> na >> phone;
            for(i=0; i<12; i++)
                cin >> as[i];
            f p(na,phone,as);
            p.cheak1();
        }
        if(as=='V')
        {
            char na[20];
            string phone;
            int as[12];
            int bs[12];
            cin >> na >> phone;
            for(i=0; i<12; i++)
                cin >> as[i];
            for(i=0; i<12; i++)
                cin >> bs[i];
            v q(na,phone,as,bs);
            q.cheak2();
        }
    }
}

        7.OOP多边形周长计算(单继承)

/*给出下面的多边形基类框架:
class polygon
{
protected:
   int number;//边数,最多不超过100条边
private:
   int side_length[100];//边长数组
public:
   polygon();//构造函数根据需要重载
   int perimeter();//计算多边形边长
   void display();//输出多边形边数和周长
}

建立一个派生类rectangle(矩形),增加以下数据成员:
  int height;
  int width;

增加以下成员函数:
 rectangle类的无参和参数化构造函数
 int perimeter();//计算矩形边长
 void display();//输出多边形边数和周长

建立一个派生类equal_polygon(等边多边形),增加以下数据成员:
 int side_len;

增加以下成员函数:
// equal_polygon类的无参和参数化构造函数
 int perimeter();//计算等边多边形边长
 void display();//输出多边形边数和周长

生成上述类并编写主函数,根据输入的多边形信息,相应建立一个多边形类对象或矩形类对象或等边多边形类对象,计算每一个多边形的周长并且输出其边数和周长。
 */



#include<bits/stdc++.h>
using namespace std;

class polygon
{
protected:
    int number;//边数,最多不超过100条边
private:
    int side_length[100];//边长数组
public:
    polygon(int n,int s[]):number(n)
    {
        int i;
        for(i=0; i<n; i++)
            side_length[i]=s[i];
    }
    int perimeter()
    {
        return number;
    }
    void display1()
    {
        int sum=0;
        int i;
        for(i=0; i<number; i++)
            sum+=side_length[i];
        cout << number << " " << sum <<endl;
    }
};

class rectangle:public polygon
{
protected:
    int height;
    int width;
public:
    /*rectangle()
    {

    }*/
    rectangle(int n,int s[],int hval,int wval):polygon(n,s),height(hval),width(wval)
    {

    }
    void display2()
    {
        cout << "4"<< " " << height*2+width*2 <<endl;
    }
};

class equal_polygon:public rectangle
{
protected:
    int sidelegth;
public:
    /*equal_polygon()
    {

    }*/
    equal_polygon(int n,int s[],int hval,int wval,int si):rectangle(n,s,hval,wval),sidelegth(si)
    {

    }
    void display3()
    {
        cout << number << " " << number*sidelegth<<endl;
    }
};

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int num;
        cin >> num;
        if(num==0)
        {
            int a[100];
            int i;
            for(i=0; i<100; i++)
            {
                cin >> a[i];
                if(a[i]==-1)
                    break;
            }
            polygon s1(i,a);
            s1.display1();
        }
        if(num==1)
        {
            int b[100]= {0};
            int l;
            int w;
            cin >> w >> l ;
            rectangle p2(4,b,w,l);
            p2.display2();
        }
        if(num==2)
        {
            int b[100]= {0};
            int num1;
            int num2;
            cin >>num1 >> num2 ;
            equal_polygon p3(num1,b,0,0,num2);
            p3.display3();
        }
    }
}

        8.OOP书籍信息记录(单继承)

#include<bits/stdc++.h>
using namespace std;

class document
{
protected:
    string name;
public:
    document(string nval):name(nval)
    {
        cout<<"Create Document Class"<<endl;
    }
    ~document()
    {
        cout<<"Delete Document Class"<<endl;
    }
    void print()
    {
        cout <<"Document Name is "<<name<<endl;
    }
};

class book:public document
{
protected:
    int page;
public:
    book(string nval,int p):document(nval),page(p)
    {
        cout<<"Create Book Class"<<endl;
    }
    ~book()
    {
        cout<<"Delete Book Class"<<endl;
    }
    void print()
    {
        cout <<"Document Name is "<<name<<endl;
        cout <<"PageCount is "<<page<<endl;
    }
};

int main()
{
    string na;
    int pa;
    cin >> na >> pa;
    book p(na,pa);
    p.print();
}

        9.班级学生平均成绩计算(单继承)

#include<bits/stdc++.h>
using namespace std;

class cnt
{
protected:
    int value;
public:
    cnt(int val):value(val)
    {

    }
    void increment()
    {
        value++;
    }
};

class whilecnt:public cnt
{
protected:
    int min;
    int max;
    int flag=0;
public:
    whilecnt(int val,int mi,int ma):cnt(val),min(mi),max(ma)
    {

    }
    void increment(int t)
    {
        while(t--)
        {

            if(value+1>max)
            {
                value=min;
                flag++;
            }
            else
                value++;
        }
    }
    int getvalue()
    {
        return value;
    }
    int getflag()
    {
        return flag;
    }
};

class cclock
{
private:
    whilecnt hour;
    whilecnt minute;
    whilecnt sec;
public:
    cclock(int val1,int val2,int val3):hour(val1,0,23),minute(val2,0,59),sec(val3,0,59)
    {

    }
    void time(int s)
    {
        sec.increment(s);
        minute.increment(sec.getflag());
        hour.increment(minute.getflag());
    }
    void display()
    {
        cout<<hour.getvalue()<<":"<<minute.getvalue()<<":"<<sec.getvalue()<<endl;
    }
};

int main()

{
    int t;
    cin >> t;
    while(t--)
    {
        int h;
        int m;
        int s;
        cin>> h>>m>>s;
        cclock p(h,m,s);
        int num;
        cin >> num;
        p.time(num);
        p.display();
    }
}

   

二、多重继承与虚继承

    1.OOP教师与干部(多重继承)

#include<bits/stdc++.h>
using namespace std;

class teacher
{
protected:
    string name;
    int age;
    char sex;
    string address;
    string tel;
    string title;
public:
    teacher()
    {

    }
    teacher(string nval,int aval,char sval,string add,string te,string ti):name(nval),age(aval),sex(sval),address(add),tel(te),title(ti)
    {

    }
};

class cader
{
protected:
    string name;
    int age;
    char sex;
    string address;
    string tel;
    string post;
public:
    cader()
    {

    }
    cader(string nval,int aval,char sval,string add,string te,string po):name(nval),age(aval),sex(sval),address(add),tel(te),post(po)
    {

    }
};

class tc:public teacher,public cader
{
protected:
    double wage;
public:
    tc(string nval,int aval,char sval,string ti,string add,string po,string te,double w):cader(nval,aval,sval,add,te,po),teacher(nval,aval,sval,add,te,ti)
    {
        wage=w;
    }
    void display()
    {
        cout<<"name:"<<teacher::name<<endl;
        cout<<"age:"<<teacher::age<<endl;
        cout<<"sex:"<<teacher::sex<<endl;
        cout<<"title:" <<title<<endl;
        cout<<"address:"<<teacher::address<<endl;
        cout<<"tel:"<<teacher::tel<<endl;
        cout<<"post:"<<post<<endl;
        cout<<"wages:"<<wage;
    }
};

int main()
{
    string name;
    int age;
    char sex;
    string title;
    string post;
    string address;
    string telephone;
    float wages;
    cin >> name >> age >> sex >> title >> post;
    cin.ignore();
    getline(cin, address);
    cin >> telephone >> wages;
    tc o(name, age, sex, title, address, post, telephone, wages);
    o.display();
}

        2.日程安排(多继承+友元函数)

#include<bits/stdc++.h>
using namespace std;

class date
{
protected:
    int year;
    int month;
    int day;
public:
    date(int yval,int mval,int dval):year(yval),month(mval),day(dval)
    {

    }
};

class times
{
protected:
    int hour;
    int min;
    int sec;
public:
    times(int hval,int mival,int sval):hour(hval),min(mival),sec(sval)
    {

    }
};

class schedual:public date,public times
{
protected:
    int id;
public:
    schedual(int yval,int mval,int dval,int hval,int mival,int sval,int idval):date(yval,mval,dval),times(hval,mival,sval),id(idval)
    {

    }
    friend bool before(const schedual &r1,const schedual &r2);
    int gety()
    {
        return year;
    }
    int getm()
    {
        return month;
    }
    int getd()
    {
        return day;
    }
    int geth()
    {
        return hour;
    }
    int getmi()
    {
        return min;
    }
    int gets()
    {
        return sec;
    }
    int geti()
    {
        return id;
    }
    void display()
    {
        cout<<"The urgent schedule is No."<<id<<": "<<year<<"/";
        cout<<setfill('0')<<setw(2)<<month<<"/"<<setfill('0')<<setw(2)<<day;
        cout<<" "<<setfill('0')<<setw(2)<<hour<<":"<<setfill('0')<<setw(2)<<min<<":"<<setfill('0')<<setw(2)<<sec<<endl;
    }
};

bool before(const schedual &r1,const schedual &r2)
{
    double sum1,sum2;
    sum1=r1.year*10000+r1.month*100+r1.day+r1.hour*0.36+r1.min*0.006+r1.sec*0.0001;
    sum2=r2.year*10000+r2.month*100+r2.day+r2.hour*0.36+r2.min*0.006+r2.sec*0.0001;
    if(sum1<sum2)
        return true;
    else
        return false;
}

int main()
{
    int num;
    int y;
    int m;
    int d;
    int h;
    int mi;
    int sec;
    int i=0;
    int j;
    int flag=0;
    schedual maxsd(10000000,1000000,100000,10000,10000,10000,9);
    schedual *p[100];
    while(cin>>num)
    {
        if(num==0)
            break;
        else
        {
            cin>>y>>m>>d>>h>>mi>>sec;
            schedual u(y,m,d,h,mi,sec,num);
            if(before(u,maxsd)==true)
                maxsd=u;
        }
    }
    maxsd.display();
}

        3.交通工具(多继承中的虚继承)

#include<bits/stdc++.h>
using namespace std;

class teacher
{
protected:
    string name;
    int age;
    char sex;
    string address;
    string tel;
    string title;
public:
    teacher()
    {

    }
    teacher(string nval,int aval,char sval,string add,string te,string ti):name(nval),age(aval),sex(sval),address(add),tel(te),title(ti)
    {

    }
};

class cader
{
protected:
    string name;
    int age;
    char sex;
    string address;
    string tel;
    string post;
public:
    cader()
    {

    }
    cader(string nval,int aval,char sval,string add,string te,string po):name(nval),age(aval),sex(sval),address(add),tel(te),post(po)
    {

    }
};

class tc:public teacher,public cader
{
protected:
    double wage;
public:
    tc(string nval,int aval,char sval,string ti,string add,string po,string te,double w):cader(nval,aval,sval,add,te,po),teacher(nval,aval,sval,add,te,ti)
    {
        wage=w;
    }
    void display()
    {
        cout<<"name:"<<teacher::name<<endl;
        cout<<"age:"<<teacher::age<<endl;
        cout<<"sex:"<<teacher::sex<<endl;
        cout<<"title:" <<title<<endl;
        cout<<"address:"<<teacher::address<<endl;
        cout<<"tel:"<<teacher::tel<<endl;
        cout<<"post:"<<post<<endl;
        cout<<"wages:"<<wage;
    }
};

int main()
{
    string name;
    int age;
    char sex;
    string title;
    string post;
    string address;
    string telephone;
    float wages;
    cin >> name >> age >> sex >> title >> post;
    cin.ignore();
    getline(cin, address);
    cin >> telephone >> wages;
    tc o(name, age, sex, title, address, post, telephone, wages);
    o.display();
}

        4.在职研究生(多继承中的虚继承)

#include<bits/stdc++.h>
using namespace std;

class people
{
protected:
    string name;
    string sex;
    int age;
public:
    people(string nval,string sval,int aval):name(nval),sex(sval),age(aval)
    {

    }
    void print()
    {
        cout<<"People:"<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Sex: "<<sex<<endl;
        cout<<"Age: "<<age<<endl;
    }
};

class student:virtual public people
{
protected:
    string no;
    double score;
public:
    student(string nval,string sval,int aval,string noval,double scval):people(nval,sval,aval),no(noval),score(scval)
    {

    }
    void print()
    {
        cout<<"Student:"<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Sex: "<<sex<<endl;
        cout<<"Age: "<<age<<endl;
        cout<<"No.: "<<no<<endl;
        cout<<"Score: "<<score<<endl;
    }
};

class teacher:virtual public people
{
protected:
    string positions;
    string departments;
public:
    teacher(string nval,string sval,int aval,string pval,string dval):people(nval,sval,aval),positions(pval),departments(dval)
    {

    }
    void print()
    {
        cout<<"Teacher:"<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Sex: "<<sex<<endl;
        cout<<"Age: "<<age<<endl;
        cout<<"Position: "<<positions<<endl;
        cout<<"Department: "<<departments<<endl;
    }
};

class gradonwork:public student,public teacher
{
private:
    string direction;
    string tutor;
public:
    gradonwork(string nval,string sval,int aval,string noval,double scval,string pval,string dval,string dival,string tuval):people(nval,sval,aval),student(nval,sval,aval,noval,scval),teacher(nval,sval,aval,pval,dval)
    {
        direction=dival;
        tutor=tuval;
    }
    void print()
    {
        cout<<"GradOnWork:"<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Sex: "<<sex<<endl;
        cout<<"Age: "<<age<<endl;
        cout<<"No.: "<<no<<endl;
        cout<<"Score: "<<score<<endl;
        cout<<"Position: "<<positions<<endl;
        cout<<"Department: "<<departments<<endl;
        cout<<"Direction: "<<direction<<endl;
        cout<<"Tutor: "<<tutor<<endl;
    }
};

int main()
{
    string na;
    string se;
    int ag;
    string nov;
    double sco;
    string posi;
    string depart;
    string dire;
    string tuto;
    cin >> na >> se >> ag;
    cin >> nov >> sco;
    cin >> posi >> depart;
    cin >> dire >> tuto;
    people p(na,se,ag);
    p.print();
    cout << endl;
    student q(na,se,ag,nov,sco);
    q.print();
    cout << endl;
    teacher o(na,se,ag,posi,depart);
    o.print();
    cout << endl;
    gradonwork u(na,se,ag,nov,sco,posi,depart,dire,tuto);
    u.print();
}

        5.OOP 飞马(多继承中的虚继承)

#include<bits/stdc++.h>
using namespace std;

class animal
{
protected:
    int weight;
public:
    animal(int wval):weight(wval)
    {

    }
    void display()
    {
        cout<<"Animal:"<<endl;
        cout<<"weight:"<<weight<<endl;
    }
};

class horse:virtual public animal
{
protected:
    int distance;
public:
    horse(int wval,int dval):animal(wval),distance(dval)
    {

    }
    void display()
    {
        cout<<"Horse:"<<endl;
        cout<<"weight:"<<weight<<endl;
        cout<<"distance:"<<distance<<endl;
    }
};

class bird:virtual public animal
{
protected:
    int height;
public:
    bird(int wval,int hval):animal(wval),height(hval)
    {

    }
    void display()
    {
        cout<<"Bird:"<<endl;
        cout<<"weight:"<<weight<<endl;
        cout<<"Height:"<<height<<endl;
    }
};

class pegasus:public horse,public bird
{
private:
    bool state;
public:
    pegasus(int wval,int dval,int hval,bool sval):animal(wval),horse(wval,dval),bird(wval,hval),state(sval)
    {

    }
    void display()
    {
        cout<<"Pegasus:"<<endl;
        cout<<"weight:"<<weight<<endl;
        cout<<"distance:"<<distance<<endl;
        cout<<"Height:"<<height<<endl;
        if(state==true)
        {
            cout<<"In the air?"<<endl;
            cout<<"true"<<endl;
            cout<<"On land?"<<endl;
            cout<<"false";
        }
        if(state==false)
        {
            cout<<"In the air?"<<endl;
            cout<<"false"<<endl;
            cout<<"On land?"<<endl;
            cout<<"true";

        }
    }
};

int main()
{
    int w;
    int d;
    int h;
    bool s;
    cin >> w >> d >> h >> s;
    animal p(w);
    p.display();
    cout << endl;
    horse q(w,d);
    q.display();
    cout << endl;
    bird o(w,h);
    o.display();
    cout << endl;
    pegasus u(w,d,h,s);
    u.display();
}

        6.OOP 谁是名人堂球员(多继承中的虚继承)

#include<bits/stdc++.h>
using namespace std;

class player
{
protected:
    string name;
    int height;
    int weight;
public:
    player(string nval,int hval,int wval):name(nval),height(hval),weight(wval)
    {

    }
    void display()
    {
        cout<<"Player:"<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"height:"<<height<<endl;
        cout<<"weight:"<<weight<<endl;
    }
};

class mvp:virtual public player
{
protected:
    int year1;
public:
    mvp(string nval,int hval,int wval,int y1):player(nval,hval,wval),year1(y1)
    {

    }
    void display()
    {
        cout<<"MVP:"<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"height:"<<height<<endl;
        cout<<"weight:"<<weight<<endl;
        cout<<"reward:win the MVP reward in "<<year1<<endl;
    }
};

class dpoy:virtual public player
{
protected:
    int year2;
public:
    dpoy(string nval,int hval,int wval,int y2):player(nval,hval,wval),year2(y2)
    {

    }
    void display()
    {
        cout<<"DPOY:"<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"height:"<<height<<endl;
        cout<<"weight:"<<weight<<endl;
        cout<<"reward:win the DPOY reward in "<<year2<<endl;
    }
};

class halloffame:public mvp,public dpoy
{

public:
    halloffame(string nval,int hval,int wval,int y1,int y2):player(nval,hval,wval),mvp(nval,hval,wval,y1),dpoy(nval,hval,wval,y2)
    {

    }
    void display()
    {
        cout<<"Hall of fame:"<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"height:"<<height<<endl;
        cout<<"weight:"<<weight<<endl;
        cout<<"reward1:win the MVP reward in "<<year1<<endl;
        cout<<"reward2:win the DPOY reward in "<<year2;
    }
};

int main()
{
    string na;
    int hei;
    int wei;
    int ye1;
    int ye2;
    cin>>na>>hei>>wei;
    cin>>ye1;
    cin>>ye2;
    player p(na,hei,wei);
    p.display();
    cout<<endl;
    mvp q(na,hei,wei,ye1);
    q.display();
    cout<<endl;
    dpoy o(na,hei,wei,ye2);
    o.display();
    cout<<endl;
    halloffame u(na,hei,wei,ye1,ye2);
    u.display();
}

        7.OOP双人决斗(多继承中的虚继承)

#include<bits/stdc++.h>
using namespace std;

class node2d
{
protected:
    string location;
public:
    node2d(string lval):location(lval)
    {

    }
};

class body:virtual public node2d
{
protected:
    int health;
    int defense;
public:
    body(string lval,int hval,int dval):node2d(lval),health(hval),defense(dval)
    {

    }
};

class weapon:virtual public node2d
{
protected:
    string wname;
    int damage;
public:
    weapon(string lval,string wval,int ddval):node2d(lval),wname(wval),damage(ddval)
    {

    }
};

class player:public body,public weapon
{
protected:
    string name;
    int flag;
public:
    player(string lval,string nval,int hval,int dval,string wval,int ddval):node2d(lval),body(lval,hval,dval),weapon(lval,wval,ddval),name(nval)
    {
        flag=1;
    }
    string getname()
    {
        return name;
    }
    int gethealth()
    {
        return health;
    }
    int getde()
    {
        return defense;
    }
    void changehealth(int num)
    {
        health=health-num;
    }
    int getflag()
    {
        return flag;
    }
    void attack(player &p2)
    {
        cout<<name<<" deal "<<damage-p2.getde()<<" damage to "<<p2.getname()<<endl;
        p2.changehealth(damage-p2.getde());
        if(p2.gethealth()>0)
            cout<<p2.getname()<<" still have "<<p2.gethealth()<<" health"<<endl;
        if(p2.gethealth()<=0)
        {
            cout<<name<<" defeated "<<p2.getname()<<" by "<<wname<<" in "<<location<<endl;
            flag=0;
        }
        cout<<endl;
    }
};

int main()
{
    string lo;
    string nam1;
    string nam2;
    int he1;
    int he2;
    int de1;
    int de2;
    string w1;
    string w2;
    int dm1;
    int dm2;
    cin>>lo;
    cin >>nam1 >> he1 >> de1 >> w1 >> dm1;
    player pd1(lo,nam1,he1,de1,w1,dm1);
    cin >>nam2 >> he2 >> de2 >> w2 >> dm2;
    player pd2(lo,nam2,he2,de2,w2,dm2);
    while(pd2.getflag()&&pd2.getflag())
    {
        pd1.attack(pd2);
        pd2.attack(pd1);
    }
}

      三、虚函数与多态

         1.员工工资(虚函数与多态)

#include<bits/stdc++.h>
using namespace std;

class employee
{
protected:
    string name;
    string title;
    int grade;
    int year;
public:
    employee()
    {

    }
    employee(string nval,string tval,int gval,int yval):name(nval),title(tval),grade(gval),year(yval)
    {

    }
    virtual void salary()
    {
        int sum;
        sum=1000+500*grade+50*year;
        cout<<name<<":"<<title<<","<<"Salary:"<<sum<<endl;
    }
};

class teamleader:public employee
{
public:
    teamleader(string nval,string tval,int gval,int yval):employee(nval,tval,gval,yval)
    {

    }
    virtual void salary()
    {
        int sum;
        sum=3000+800*grade+100*year;
        cout<<name<<":"<<title<<","<<"Salary:"<<sum<<endl;
    }
};

class manager:public employee
{
public:
    manager(string nval,string tval,int gval,int yval):employee(nval,tval,gval,yval)
    {

    }
    virtual void salary()
    {
        int sum;
        sum=5000+1000*(grade+year);
        cout<<name<<":"<<title<<","<<"Salary:"<<sum<<endl;
    }
};

int main()
{
    int t;
    cin >> t;
    employee *p;
    while(t--)
    {
        string na;
        string ti;
        int gr;
        int ye;
        cin >> na >> ti >> gr >> ye;
        if(ti!="Employee"&&ti!="Teamleader"&&ti!="Manager")
        {
            cout <<"error position."<<endl;
            continue;
        }
        else if(gr<0||ye<0)
        {
            cout<<"error grade or year."<<endl;
        }
        else if(ti=="Employee")
        {
            employee q(na,ti,gr,ye);
            p=&q;
            p->salary();
        }
        else if(ti=="Teamleader")
        {
            teamleader s(na,ti,gr,ye);
            p=&s;
            p->salary();
        }
        else if(ti=="Manager")
        {
            manager u(na,ti,gr,ye);
            p=&u;
            p->salary();
        }
    }
}

        2.支票账户(虚函数与多态)

#include<bits/stdc++.h>
using namespace std;
class ba
{
protected:
    string name;
    char account[20];
    int balance;
public:
    ba(string nval,char aval[],int bval):name(nval),balance(bval)
    {
        strcpy(account,aval);
    }
    void deposit(int n1)
    {
        balance=balance+n1;
    }
    virtual void withdraw(int n2)
    {
        if(balance<n2)
            cout<<"insufficient"<<endl;
        else
            balance=balance-n2;
    }
    virtual void display()
    {
        cout<<name<<" "<<account<<" Balance:"<<balance<<endl;
    }

};
class bp:public ba
{
private:
    int limit;
    int limitsum;
public:
    bp(string nval,char aval[],int bval):ba(nval,aval,bval)
    {
        limit=5000;
        limitsum=0;
    }
    virtual void withdraw(int n3)
    {
        if(balance<-5000+n3)
        {
            cout<<"insufficient"<<endl;
        }
        else
        {
            balance=balance-n3;
            if(balance<0)
            {
                limit=5000+balance;
                limitsum=balance;
            }
        }
    }
    virtual void display()
    {
        cout<<name<<" "<<account;
        if(balance>=0)
        {
            cout<<" Balance:"<<balance;
            cout<<" limit:"<<limit<<endl;
        }
        else
        {
            cout<<" Balance:0";
            cout<<" limit:"<<limit<<endl;
        }
    }
};


int main()
{
    int test;
    int bala;
    string name;
    char acc[20];
    int num1;
    int num2;
    int num3;
    cin>>test;
    ba *p;
    while(test--)
    {
        cin>>name>>acc>>bala;
        if(acc[1]=='A')
        {
            ba  q(name,acc,bala);
            cin>>num1>>num2;
            p=&q;
            p->deposit(num1);
            p->withdraw(num2);
            cin>>num1>>num2;
            p->deposit(num1);
            p->withdraw(num2);
            p->display();
        }
        else if(acc[1]=='P')
        {
            bp o(name,acc,bala);
            cin>>num1>>num3;
            p=&o;
            p->deposit(num1);
            p->withdraw(num3);
            cin>>num1>>num3;
            p->deposit(num1);
            p->withdraw(num3);
            p->display();
        }
    }
}

        3.汽车收费(虚函数和多态)

#include<bits/stdc++.h>
using namespace std;

class vehicle
{
protected:
    string no;
public:
    vehicle()
    {

    }
    vehicle(string nval):no(nval)
    {

    }
    virtual void display()=0;
};

class car:public vehicle
{
private:
    int number1;
    int weight1;
public:
    car(string nval,int num1,int wei1):vehicle(nval),number1(num1),weight1(wei1)
    {

    }
    virtual void display()
    {
        cout << no <<" "<<number1*8+weight1*2<<endl;
    }
};

class truck:public vehicle
{
private:
    int weight2;
public:
    truck(string nval,int wei2):vehicle(nval),weight2(wei2)
    {

    }
    virtual void display()
    {
        cout << no <<" "<<weight2*5<<endl;
    }
};

class bus:public vehicle
{
private:
    int number3;
public:
    bus(string nval,int num3):vehicle(nval),number3(num3)
    {

    }
    virtual void display()
    {
        cout << no <<" "<<number3*3<<endl;
    }
};

int main()
{
    int t;
    cin >>  t;
    vehicle *pv;
    while(t--)
    {
        int num;
        cin >> num;
        if(num==1)
        {
            string nv;
            int numbe1;
            int weigh1;
            cin >> nv >> numbe1 >> weigh1;
            car a(nv,numbe1,weigh1);
            pv=&a;
            pv->display();
        }
        if(num==2)
        {
            string nv;
            int weigh2;
            cin >> nv >> weigh2;
            truck b(nv,weigh2);
            pv=&b;
            pv->display();
        }
        if(num==3)
        {
            string nv;
            int numbe3;
            cin >> nv >> numbe3;
            bus c(nv,numbe3);
            pv=&c;
            pv->display();
        }
    }
}

        4.计算学生成绩等级(虚函数和多态)

#include<bits/stdc++.h>
using namespace std;

class student
{
protected:
    string name;
    int type;
    int courses[3];
    char coursegrade[100];
public:
    student()
    {

    }
    student(string n,int t,int a1,int a2,int a3):name(n),type(t)
    {
        courses[0]=a1;
        courses[1]=a2;
        courses[2]=a3;
    }
    virtual void calculateGrade()=0;
    void print()
    {
        cout << name << ",";
        if(type==1)
            cout << "本科生"<<",";
        if(type==2)
            cout << "研究生"<<",";
        cout << coursegrade<<endl;
    }
};

class undergraduate:public student
{
public:
    undergraduate(string n,int t,int a1,int a2,int a3):student(n,t,a1,a2,a3)
    {

    }
    virtual void calculateGrade()
    {
        int sum=(courses[0]+courses[1]+courses[2])/3;
        if(sum>=80&&sum<=100)
            strcpy(coursegrade,"优秀");
        if(sum>=70&&sum<80)
            strcpy(coursegrade,"良好");
        if(sum>=60&&sum<70)
            strcpy(coursegrade,"一般");
        if(sum>=50&&sum<60)
            strcpy(coursegrade,"及格");
        if(sum>=0&&sum<50)
            strcpy(coursegrade,"不及格");
    }
};

class postgraduate:public student
{
public:
    postgraduate(string n,int t,int a1,int a2,int a3):student(n,t,a1,a2,a3)
    {

    }
    virtual void calculateGrade()
    {
        int sum=(courses[0]+courses[1]+courses[2])/3;
        if(sum>=90&&sum<=100)
            strcpy(coursegrade,"优秀");
        if(sum>=80&&sum<90)
            strcpy(coursegrade,"良好");
        if(sum>=70&&sum<80)
            strcpy(coursegrade,"一般");
        if(sum>=60&&sum<70)
            strcpy(coursegrade,"及格");
        if(sum>=0&&sum<60)
            strcpy(coursegrade,"不及格");
    }
};

int main()
{
    int t;
    cin >> t;
    student *p;
    while(t--)
    {
        string na;
        int ty;
        int num1;
        int num2;
        int num3;
        cin >> na >> ty >> num1 >> num2 >> num3;
        if(ty==1)
        {
            undergraduate a(na,ty,num1,num2,num3);
            p=&a;
            p->calculateGrade();
            p->print();
        }
        if(ty==2)
        {
            postgraduate b(na,ty,num1,num2,num3);
            p=&b;
            p->calculateGrade();
            p->print();
        }
    }
}

        5.宠物的生长(虚函数和多态)

#include<bits/stdc++.h>
using namespace std;

class cdate
{
private:
    int year;
    int month;
    int day;
public:
    cdate()
    {

    }
    cdate(int yval,int mval,int dval):year(yval),month(mval),day(dval)
    {

    }
    int gety()
    {
        return year;
    }
    int getm()
    {
        return month;
    }
    int getd()
    {
        return day;
    }
    int leap(int ys)
    {
        int xc;
        if((ys%4==0&&ys%100!=0)||(ys%100==0&&ys%400==0))
            xc=1;
        else
            xc=0;
        return xc;
    }
    int dayssum(cdate cf)
    {
        int sum;
        int j;
        int i;
        int days[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
        int daysleap[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(leap(cf.year))
        {
            sum=days[cf.month]-cf.day;
            for(i=cf.month+1; i<13; i++)
                sum+=days[i];
        }
        else
        {
            sum=daysleap[cf.month]-cf.day;
            for(i=cf.month+1; i<13; i++)
                sum+=daysleap[i];
        }
        return sum;
    }
    int datex(cdate cf1,cdate cf2)
    {
        int y;
        int days;
        if(cf1.year!=cf2.year)
        {
            days=dayssum(cf1);
            for(y=cf1.year+1; y<=cf2.year; y++)
            {
                if(leap(y))
                    days=days+366;
                else
                    days=days+365;
            }
            days=days-dayssum(cf2);
        }
        else if(cf1.year==cf2.year&&cf1.month!=cf2.month)
            days=dayssum(cf1)-dayssum(cf2);
        else if(cf1.year==cf2.year&&cf1.month==cf2.month&&cf1.day!=cf2.day)
            days=cf2.day-cf1.day;

        return days;
    }
};

class pet
{
protected:
    string name;
    float length;
    float weight;
    cdate current;
public:
    pet()
    {

    }
    pet(string nval,float l,float w,int yval,int mval,int dval):current(yval,mval,dval),name(nval),length(l),weight(w)
    {

    }
    virtual void display(cdate day)=0;
};

class cat:public pet
{
public:
    cat(string nval,float l,float w,int yval,int mval,int dval):pet(nval,l,w,yval,mval,dval)
    {

    }
    virtual void display(cdate day)
    {
        int sum;
        sum=current.datex(current,day);
        if(sum>0)
            cout<<name<<" after "<<sum<<" day: length="<<fixed<<setprecision(2)<<length+sum*0.1<<",weight="<<fixed<<setprecision(2)<<weight+sum*0.2<<endl;
        if(sum<0)
            cout<<name<<" after "<<-sum<<" day: length="<<fixed<<setprecision(2)<<length-sum*0.1<<",weight="<<fixed<<setprecision(2)<<weight-sum*0.2<<endl;
    }
};

class dog:public pet
{
public:
    dog(string nval,float l,float w,int yval,int mval,int dval):pet(nval,l,w,yval,mval,dval)
    {

    }
    virtual void display(cdate day)
    {
        int sum;
        sum=current.datex(day,current);
        if(sum>0)
            cout<<name<<" after "<<sum<<" day: length="<<fixed<<setprecision(2)<<length+sum*0.2<<",weight="<<fixed<<setprecision(2)<<weight+sum*0.1<<endl;
        if(sum<0)
            cout<<name<<" after "<<-sum<<" day: length="<<fixed<<setprecision(2)<<length-sum*0.2<<",weight="<<fixed<<setprecision(2)<<weight-sum*0.1<<endl;
    }
};

int main()
{
    int t;
    cin >> t;
    int y;
    int m;
    int d;
    cin >> y >> m >> d;
    cdate da(y,m,d);
    pet *p;
    while(t--)
    {
        int num;
        cin >> num;
        if(num==1)
        {
            string na;
            double leng;
            double weig;
            int ye;
            int mo;
            int dada;
            cin >> na >> leng >> weig >> ye >> mo >> dada;
            if(ye<y||(ye==y&&mo<m)||(ye==y&&mo==m&&dada<d))
                cout << "error"<<endl;
            else
            {
                cat a(na,leng,weig,ye,mo,dada);
                p=&a;
                p->display(da);
            }
        }
        if(num==2)
        {
            string na;
            double leng;
            double weig;
            int ye;
            int mo;
            int dada;
            cin >> na >> leng >> weig >> ye >> mo >> dada;
            if(ye<y||(ye==y&&mo<m)||(ye==y&&mo==m&&dada<d))
                cout << "error"<<endl;
            else
            {
                dog b(na,leng,weig,ye,mo,dada);
                p=&b;
                p->display(da);
            }
        }
    }
}

        6.进位与借位(虚函数和多态)

#include<bits/stdc++.h>
using namespace std;

class group
{
public:
    group()
    {

    }
    virtual int add(int x, int y)=0;
    virtual int sub(int x, int y)=0;
};

class group1:public group
{
public:
    group1():group()
    {

    }
    virtual int add(int x, int y)
    {
        return x+y;
    }
    virtual int sub(int x, int y)
    {
        return x-y;
    }
};

class group2:public group
{
public:
    group2():group()
    {

    }
    virtual int add(int x, int y)
    {
        return x+y;
    }
    virtual int sub(int x, int y)
    {
        int res;
        int a1,b1,c1,a2,b2,c2,a3,b3,c3;
        a1=x/100;
        b1=x%100/10;
        c1=x%10;
        a2=y/100;
        b2=y%100/10;
        c2=y%10;
        if(a1<a2&&a1!=0)
        {
            a1+=10;
            a3=a1-a2;
        }
        else
            a3=a1-a2;
        if(b1<b2)
        {
            b1+=10;
            b3=b1-b2;
        }
        else
            b3=b1-b2;
        if(c1<c2)
        {
            c1+=10;
            c3=c1-c2;
        }
        else
            c3=c1-c2;
        res=a3*100+b3*10+c3;
        return res;
    }
};

class group3:public group
{
public:
    group3():group()
    {

    }
    virtual int add(int x, int y)
    {
        int res;
        int a1,b1,c1,a2,b2,c2,a3,b3,c3;
        a1=x/100;
        b1=x%100/10;
        c1=x%10;
        a2=y/100;
        b2=y%100/10;
        c2=y%10;
        a3=(a1+a2)%10;
        b3=(b1+b2)%10;
        c3=(c1+c2)%10;
        res=a3*100+b3*10+c3;
        return res;
    }
    virtual int sub(int x, int y)
    {
        int res;
        int a1,b1,c1,a2,b2,c2,a3,b3,c3;
        a1=x/100;
        b1=x%100/10;
        c1=x%10;
        a2=y/100;
        b2=y%100/10;
        c2=y%10;
        if(a1<a2&&a1!=0)
        {
            a1+=10;
            a3=a1-a2;
        }
        else
            a3=a1-a2;
        if(b1<b2)
        {
            b1+=10;
            b3=b1-b2;
        }
        else
            b3=b1-b2;
        if(c1<c2)
        {
            c1+=10;
            c3=c1-c2;
        }
        else
            c3=c1-c2;
        res=a3*100+b3*10+c3;
        return res;
    }
};

int main()
{
    int t;
    cin >> t;
    group *p;
    while(t--)
    {
        int num;
        cin >> num;
        if(num==1)
        {
            group1 a;
            p=&a;
            int xval;
            int yval;
            char ch;
            cin >> xval >> ch >> yval;
            if(ch=='+')
                cout<<p->add(xval,yval)<<endl;
            if(ch=='-')
                cout<<p->sub(xval,yval)<<endl;
        }
        if(num==2)
        {
            group2 b;
            p=&b;
            int xval;
            int yval;
            char ch;
            cin >> xval >> ch >> yval;
            if(ch=='+')
                cout<<p->add(xval,yval)<<endl;
            if(ch=='-')
                cout<<p->sub(xval,yval)<<endl;
        }
        if(num==3)
        {
            group3 c;
            p=&c;
            int xval;
            int yval;
            char ch;
            cin >> xval >> ch >> yval;
            if(ch=='+')
                cout<<p->add(xval,yval)<<endl;
            if(ch=='-')
                cout<<p->sub(xval,yval)<<endl;
        }
    }
}

        7.求最大面积(虚函数和多态)

#include<bits/stdc++.h>
using namespace std;

class geometry
{
public:
    geometry()
    {

    }
    virtual double getarea()=0;
};

class rect:public geometry
{
protected:
    double length;
    double width;
public:
    rect(double lval,double wval):geometry(),length(lval),width(wval)
    {

    }
    virtual double getarea()
    {
        return length*width;
    }
};

class circle:public geometry
{
protected:
    double r;
public:
    circle(double rval):geometry(),r(rval)
    {

    }
    virtual double getarea()
    {
        return 3.14*r*r;
    }
};

class totalarea
{
private:
    static double maxsumc;
public:
    totalarea()
    {

    }
    static void computerMaxArea(geometry **t,int n)
    {
        int j;
        for(j=0; j<n; j++)
        {
            if(t[j]->getarea()>maxsumc)
                maxsumc=t[j]->getarea();
        }
    }
    void display()
    {
        cout<<"最大面积="<<fixed<<setprecision(2)<<maxsumc<<endl;
    }
};

double totalarea::maxsumc=0;

int main()
{
    int test;
    cin>>test;
    int i;
    int num;
    double ldength1,wdidth1;
    double rdadius1;
    geometry **p=new geometry*[test];
    for(i=0; i<test; i++)
    {
        cin>>num;
        if(num==1)
        {
            cin>>ldength1>>wdidth1;
            p[i]=new rect(ldength1,wdidth1);
        }
        if(num==2)
        {
            cin>>rdadius1;
            p[i]=new circle(rdadius1);
        }
    }
    totalarea q;
    q.computerMaxArea(p,test);
    q.display();
}

      四、运算符重载

       1.分数的加减乘除(运算符重载)

#include<bits/stdc++.h>
using namespace std;

class fraction
{
private:
    int fm;
    int fz;
public:
    fraction()
    {

    }
    fraction(int mval,int nval):fz(mval),fm(nval)
    {

    }
    fraction operator+(fraction &rhs)
    {
        fraction p1;
        p1.fz=fz*rhs.fm+rhs.fz*fm;
        p1.fm=fm*rhs.fm;
        return p1;
    }
    fraction operator-(fraction &rhs)
    {
        fraction p2;
        p2.fz=fz*rhs.fm-rhs.fz*fm;
        p2.fm=fm*rhs.fm;
        return p2;
    }
    fraction operator*(fraction &rhs)
    {
        fraction p3;
        p3.fz=fz*rhs.fz;
        p3.fm=fm*rhs.fm;
        return p3;
    }
    fraction operator/(fraction &rhs)
    {
        fraction p4;
        p4.fz=fz*rhs.fm;
        p4.fm=fm*rhs.fz;
        return p4;
    }
    void display()
    {
        cout << "fraction="<<fz <<"/"<<fm;
    }
};

int main()
{
    int x1,y1,x2,y2;
    cin >> x1 >> y1 >> x2 >> y2;
    fraction a(x1,y1);
    fraction b(x2,y2);
    fraction c(0,1);
    c=a+b;
    c.display();
    cout<<endl;
    c=a-b;
    c.display();
    cout<<endl;
    c=a*b;
    c.display();
    cout<<endl;
    c=a/b;
    c.display();
}

        2.四进制加法(运算符重载)

#include<bits/stdc++.h>
using namespace std;
class four
{
private:
    int num;
public:
    four()
    {
        num=0;
    }
    four(int nval):num(nval)
    {

    }
    void set(int b)
    {
        num=b;
    }
    four operator+(const four &rhs)
    {
        int sum;
        int ad;
        int nx=0,summ=0;
        int i;
        sum=num+rhs.num;
        ad=sum;
        while(ad)
        {
            ad/=10;
            nx++;
        }
        for(i=0; i<nx+1; i++)
        {
            if(sum%10>=4)
                sum=(sum/10+1)*10+sum%10-4;
            summ=summ+sum%10*pow(10,i);
            sum=sum/10;
        }
        four vsc(summ);
        return vsc;
    }
    void display();
};
void four::display()
{
    cout<<num;
}

int main()
{
    int t;
    int ac;
    int i;
    cin>>t;
    four *p;
    four res;
    p=new four[t];
    for(i=0; i<t; i++)
    {
        cin>>ac;
        p[i].set(ac);
    }
    for(i=0; i<t; i++)
    {
        res=res+p[i];
    }
    res.display();
}

        3.人民币输出(输出运算符重载)

#include<bits/stdc++.h>
using namespace std;

class money
{
private:
    int yuan;
    int jiao;
    int fen;
public:
    money()
    {

    }
    money(double num)
    {
        yuan=(int)num;
        jiao=((int)(num*10))%10;
        fen=(((int)(num*100))%100)%10;
    }
    friend ostream &operator <<(ostream &a,money &p);
};

ostream &operator <<(ostream &a,money &p)
{
    a<<"yuan="<<p.yuan<<" "<<"jiao="<<p.jiao<<" "<<"fen="<<p.fen<<endl;
    return a;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        double q;
        cin >> q;
        q=q+0.005;
        money m(q);
        cout << m;
    }
}

        4.日期比较(运算符重载之类型转换)

#include<bits/stdc++.h>
using namespace std;

class CDate
{
private:
    int year;
    int month;
    int day;
public:
    CDate()
    {

    }
    CDate(int num)
    {
        year=num/10000;
        month=(num-year*10000)/100;
        day=num-year*10000-month*100;
    }
    void Print()
    {
        if(month<10&&day<10)
            cout<<year<<"年"<<"0"<<month<<"月"<<"0"<<day<<"日"<<endl;
        if(month<10&&day>=10)
            cout<<year<<"年"<<"0"<<month<<"月"<<day<<"日"<<endl;
        if(month>=10&&day<10)
            cout<<year<<"年"<<month<<"月"<<"0"<<day<<"日"<<endl;
        if(month>=10&&day>=10)
            cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
    }
    operator int()
    {
        int sum=0;
        sum=year*10000+month*100+day;
        return sum;
    }
};

int main()

{
    int t, t1, t2;
    CDate C1, C2;
    cin>>t;
    while (t--)
    {
        cin>>t1>>t2;
        C1 = t1;
        C2 = t2;
        ((C1>C2)?C1:C2).Print();
    }
    return 0;
}

        5.字符串比较(运算符重载)

#include<bits/stdc++.h>
using namespace std;

class str
{
private:
    char *p;
public:
    str()
    {

    }
    str(char *s)
    {
        p=s;
    }
    friend bool operator >(str &lhs,str &rhs);
    friend bool operator <(str &lhs,str &rhs);
    friend bool operator ==(str &lhs,str &rhs);
    void show()
    {
        cout << p;
    }
};

bool operator >(str &lhs,str &rhs)
{
    if(strlen(lhs.p)>strlen(rhs.p))
        return true;
    else
        return false;
}

bool operator <(str &lhs,str &rhs)
{
    if(strlen(lhs.p)<strlen(rhs.p))
        return true;
    else
        return false;
}

bool operator ==(str &lhs,str &rhs)
{
    if(strlen(lhs.p)==strlen(rhs.p))
        return true;
    else
        return false;
}

int main()
{
    char ap[100];
    char bp[100];
    char cp[100];
    cin.getline(ap,100);
    cin.getline(bp,100);
    cin.getline(cp,100);
    str a(ap);
    str b(bp);
    str c(cp);
    a>b?b.show():a.show();
    cout<<endl;
    a<c?c.show():a.show();
    cout<<endl;
    b==c?b.show():c.show();
}

        6.矩形关系(运算符重载)

#include<bits/stdc++.h>
using namespace std;

class cpoint
{
    int x,y;
public:
    cpoint(int xval,int yval):x(xval),y(yval)
    {

    }
    int getx()
    {
        return x;
    }
    int gety();
};

int cpoint::gety()
{
    return y;
}

class crectangle
{
private:
    cpoint left, right;
public:
    friend ostream& operator<<(ostream& osd, crectangle& px);
public:
    crectangle(int x1,int y1,int x2,int y2):left(x1,y1),right(x2,y2)
    {

    }
    bool operator>(cpoint& cc)
    {
        if (cc.getx() >= this->left.getx() && cc.getx() <= this->right.getx() && cc.gety() >= this->right.gety() && cc.gety() <= this->left.gety())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator>(crectangle& cd)
    {
        if ((cd.left.getx() >= this->left.getx() && cd.left.getx() <= this->right.getx() && cd.left.gety() >= this->right.gety() && cd.left.gety() <= this->left.gety())
                &&(cd.right.getx() >= this->left.getx() && cd.right.getx() <= this->right.getx() && cd.right.gety() >= this->right.gety() && cd.right.gety() <= this->left.gety()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator==(crectangle& cf)
    {
        if (cf.left.getx() == this->left.getx() && cf.left.gety() == this->left.gety() && cf.right.getx() == this->right.getx() && cf.right.gety() == this->right.gety())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    operator int()
    {
        return (left.gety() - right.gety()) * (right.getx() - left.getx());
    }
    bool operator*(crectangle& ct)
    {
        if (ct.left.getx()>=this->left.getx()&&ct.left.getx()<=this->right.getx()||ct.left.gety()>=this->right.gety()&&ct.left.gety()<=this->left.gety())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};

ostream& operator<<(ostream& osd, crectangle& px)
{
    osd << px.left.getx() << " " << px.left.gety() << " " << px.right.getx() << " " << px.right.gety() ;
    return osd;
}

int main()
{
    int test, x1, x2, y1, y2;
    cin >> test;
    while (test--)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        crectangle rect1(x1, y1, x2, y2);
        cin >> x1 >> y1 >> x2 >> y2;
        crectangle rect2(x1, y1, x2, y2);
        cout << "矩形1:" << rect1 << " " << (int)rect1 << endl;
        cout << "矩形2:" << rect2 << " " << (int)rect2 << endl;
        if (rect1 == rect2)
        {
            cout << "矩形1和矩形2相等"<<endl;
        }
        else if (rect2 > rect1)
        {
            cout << "矩形2包含矩形1" << endl;
        }
        else if (rect1 > rect2)
        {
            cout << "矩形1包含矩形2" << endl;
        }
        else if (rect1 * rect2)
        {
            cout << "矩形1和矩形2相交" << endl;
        }
        else
        {
            cout << "矩形1和矩形2不相交" << endl;
        }
        cout << endl;
    }
}

        7.矩阵(运算符重载)

#include<bits/stdc++.h>
using namespace std;
class carray
{
private:
		int n,m;
		int **data;
public:
		carray()
		{
			
		}
		carray(int nval,int mval):n(nval),m(mval)
		{
			int i;
			data=new int*[n];
			for(i=0;i<n;i++)
				data[i]=new int[m];
		}
		carray(carray &as)
		{
			int i;
			int j;
			n=as.n;
			m=as.m;
			data=new int*[n];
			for(i=0;i<n;i++)
				data[i]=new int[m];
			for(i=0;i<n;++i)
				for(j=0;j<m;j++)
					data[i][j]=as.data[i][j];
		}
		carray &operator =(carray &as)
		{
		    int i;
			if(data!=NULL)
       		 {
           		 for (i=0; i<n; i++)
               		 delete []data[i];
            	 delete []data;
       		 }
			n=as.n;
			m=as.m;
			data=new int*[n];
			for(i=0;i<n;i++)
				data[i]=new int[m];
			for(i=0;i<n;++i)
				for(int j=0;j<m;j++)
					data[i][j]=as.data[i][j];
			return *this;
		}
	
		int *const operator [](int kcc)
		{
			return data[kcc];
		}
		int operator () (int ixc,int jxc)
		{
			return data[ixc][jxc];
		}
};


int main()
{
	int test;
	int n,m;
	int i,j;
	cin>>test;
	while(test--)
	{
		cin>>n>>m;
		carray matrixA(n,m);
		for(i=0;i<n;++i)
			for(j=0;j<m;j++)
				cin>>matrixA[i][j];
		cout<<"MatrixA:"<<endl;
		for(i=0;i<n;++i)
		{
			for(j=0;j<m;j++)
			{
				cout<<matrixA(i,j)<<" ";
			}
			cout<<endl;
		}
		cout<<"MatrixB:"<<endl;
		carray matrixB;
		matrixB=matrixA;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				cout<<matrixB[i][j]<<" ";
			}
			cout<<endl;
		}
	}
}

        8.X的放大与缩小(运算符重载)

#include<bits/stdc++.h>
using namespace std;

class cxgraph
{
private:
    int num;
public:
    cxgraph()
    {

    }
    cxgraph(int nval):num(nval)
    {

    }
    friend ostream &operator <<(ostream &p,cxgraph const &a);
    friend cxgraph operator ++ (cxgraph &ap);
    friend cxgraph operator ++ (cxgraph &ap,int);
    friend cxgraph operator -- (cxgraph &ap);
    friend cxgraph operator -- (cxgraph &ap,int);
};

cxgraph operator ++ (cxgraph &ap)
{
    if(ap.num!=21)
    {
        ap.num++;
        ap.num++;
    }
    return ap;
}

cxgraph operator ++ (cxgraph &ap,int)
{
    cxgraph fc=ap;
    if(ap.num!=21)
    {
        ap.num++;
        ap.num++;
    }
    return fc;
}

cxgraph operator -- (cxgraph &ap)
{
    if(ap.num!=1)
    {
        ap.num--;
        ap.num--;
    }
    return ap;
}

cxgraph operator -- (cxgraph &ap,int)
{
    cxgraph cf=ap;
    if(ap.num!=1)
    {
        ap.num--;
        ap.num--;
    }

    return cf;
}
ostream &operator << (ostream & p,cxgraph const &a)
{
    int kt=a.num;
    for(int i=0; i<(a.num+1)/2; i++)
    {
        for(int j=0; j<(a.num-kt)/2; j++)
            p<<" ";
        for(int j=kt; j>0; j--)
            p<<"X";
        p<<endl;
        kt=kt-2;
    }
    kt=3;
    for(int i=(a.num+1)/2; i<a.num; i++)
    {
        for(int j=(a.num-kt)/2; j>0; j--)
            p<<" ";
        for(int j=0; j<kt; j++)
            p<<"X";
        p<<endl;
        kt=kt+2;
    }
    return p;
}

int main()
{
    int t;
    int n;
    string command;
    cin >> n;
    cxgraph xgraph(n);
    cin >> t;
    while(t--)
    {
        cin >> command;
        if(command=="show++")
            cout <<xgraph++<<endl;
        else if(command=="++show")
            cout <<++xgraph<<endl;
        else if(command=="show--")
            cout <<xgraph--<<endl;
        else if(command=="--show")
            cout <<--xgraph<<endl;
        if(command=="show")
            cout <<xgraph <<endl;
    }
    return 0;
}

        9.OOP CString字符串拼接(深拷贝与运算符重载之深赋值)

#include<bits/stdc++.h>
using namespace std;
 
class cstring
{
public:
	char* s;
    int n;
	cstring(char* const _s=NULL):s(NULL),n(0)  
	{
		if (!_s) return;
		n=strlen(_s);
		s=new char[n + 1]{};
		strncpy(s,_s,n);
	}
	cstring(const cstring &popo)      
	{
		if(this!=&popo)
		{
			s=new char[n+1];
			strcpy(s,popo.s);
		}
	}

	friend cstring operator+(const cstring &ps1,const cstring &ps2);      
	friend ostream &operator<<(ostream &a,const cstring &pc);        
	cstring operator=(const cstring &dps)   
	{
		/*if(n!=0)
		    delete s;*/
		n=dps.n;
		s=new char[n+100];
		strcpy(s,dps.s);
		return *this;
	}
};

ostream &operator<<(ostream &a,const cstring &pc)        
{
	int i;
	for(i=0;i<pc.n;i++)
	  	a << pc.s[i];
	return a;
}

cstring operator+(const cstring &ps1,const cstring &ps2)     
{
		char *tccemp=new char[ps1.n+ps2.n+1];
		strncpy(tccemp,ps1.s,ps1.n);
        strncpy(tccemp+ps1.n,ps2.s,ps2.n);
        tccemp[ps1.n+ps2.n]='\0';
		cstring fxs(tccemp);
		return fxs;
}
 
int main()
{
	int y;
	char input[150 + 1]{};
	cin >> y;
	while(y--)
	{
		cin >> input;
		cstring s1(input);
		cin >> input;
		cstring s2(input);
		cstring sEmpty;
		delete new cstring(s1=s1+s2);
		cout << s1+sEmpty << endl
			 << sEmpty+s2 << endl
			 << s1+s2 << endl
			 << endl;
	}
    //return 0;
}

      五、函数模板与类模板

       1.倚天屠龙记(函数模板)

#include<bits/stdc++.h>
using namespace std;

template <class T>
void isstream(T a[],int n)
{
    int i;
    for(i=0; i<n; i++)
        cin >> a[i];
}

template <class T>
void display(T a[],T b[],int n)
{
    int i;
    for(i=0; i<n; i++)
        cout << b[i];
    for(i=0; i<n; i++)
        cout << a[i];
    cout << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int num;
        char ch;
        cin >> ch >> num;
        if(ch=='I')
        {
            int *b=new int[num];
            int *c=new int[num];
            isstream(b,num);
            isstream(c,num);
            display(b,c,num);
        }
        if(ch=='D')
        {
            double *b=new double[num];
            double *c=new double[num];
            isstream(b,num);
            isstream(c,num);
            display(b,c,num);
        }
        if(ch=='C')
        {
            char *b=new char[num];
            char *c=new char[num];
            isstream(b,num);
            isstream(c,num);
            display(b,c,num);
        }
    }
}

        2.元素查找(函数模板)

#include<bits/stdc++.h>
using namespace std;

template <class T>
void isstream(T a[],int n)
{
    int i;
    for(i=0; i<n; i++)
        cin >> a[i];
}

template <class T>
void index(T a[],int n,T key)
{
    int i;
    int flag=0;
    for(i=0; i<n; i++)
        if(a[i]==key)
        {
            flag=1;
            cout << i+1 <<endl;
        }
    if(flag==0)
        cout << "0" <<endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int num;
        char ch;
        cin >> ch >> num;
        if(ch=='I')
        {
            int *b=new int[num];
            isstream(b,num);
            int k;
            cin >> k;
            index(b,num,k);
        }
        if(ch=='D')
        {
            double *b=new double[num];
            isstream(b,num);
            double k;
            cin >> k;
            index(b,num,k);
        }
        if(ch=='C')
        {
            char *b=new char[num];
            isstream(b,num);
            char k;
            cin >> k;
            index(b,num,k);
        }
        if(ch=='S')
        {
            string *b=new string[num];
            isstream(b,num);
            string k;
            cin >> k;
            index(b,num,k);
        }
    }
}

        3.谁的票数最高(函数模板)

#include<bits/stdc++.h>
using namespace std;

template <class T>
void isstream(T a[],int n)
{
    int i;
    for(i=0; i<n; i++)
        cin >> a[i];
}

template <class T>
void vote(T a[],int n)
{
    int cnt=0;
    int max=0;
    int flag=0;
    int i,j;
    for(i=0; i<n; i++)
    {
        cnt=0;
        for(j=0; j<n; j++)
            if(a[i]==a[j])
                cnt++;
        if(cnt>max)
        {
            max=cnt;
            flag=i;
        }
    }
    cout << a[flag] << " " << max << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int num;
        char ch;
        cin >> ch >> num;
        if(ch=='I')
        {
            int *b=new int[num];
            isstream(b,num);
            vote(b,num);
        }
        if(ch=='D')
        {
            double *b=new double[num];
            isstream(b,num);
            vote(b,num);
        }
        if(ch=='C')
        {
            char *b=new char[num];
            isstream(b,num);
            vote(b,num);
        }
        if(ch=='S')
        {
            string *b=new string[num];
            isstream(b,num);
            vote(b,num);
        }
    }
}

        4. 排序函数模板(函数模板与运算符重载)

#include<bits/stdc++.h>
using namespace std;

class point
{
private:
    double x;
    double y;
public:
    point()
    {

    }
    point(int xval,int yval):x(xval),y(yval)
    {

    }
    double getx()
    {
        return x;
    }
    double gety()
    {
        return y;
    }
    friend bool operator >(point &right,point &left);
    friend istream&operator >>(istream &a,point &rhs);
    friend ostream&operator <<(ostream &a,point &rhs);
};

bool operator >(point &right,point &left)
{
    if(sqrt(right.getx()*right.getx()+right.gety()*right.gety())>sqrt(left.getx()*left.getx()+left.gety()*left.gety()))
        return true;
    else
        return false;
}

istream&operator >>(istream &a,point &rhs)
{
    a>>rhs.x>>rhs.y;
    return a;
}

ostream&operator <<(ostream &a,point &rhs)
{
    a<<"("<<fixed<<setprecision(1)<<rhs.x<<", "<<fixed<<setprecision(1)<<rhs.y<<")";
    return a;
}

template <class T>
void isstream(T a[],int n)
{
    int i;
    for(i=0; i<n; i++)
        cin >> a[i];
}

template <class T>
void mysort(T a[],int n)
{
    int i;
    int j;
    for(i=0; i<n-1; i++)
        for(j=i+1; j<n; j++)
            if(a[i]>a[j])
                swap(a[i],a[j]);
}

template <class T>
void display(T a[],int n)
{
    int i;
    for(i=0; i<n; i++)
        cout << a[i]<<" ";
    cout << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int num;
        char ch;
        cin >> ch >> num;
        if(ch=='I')
        {
            int *b=new int[num];
            isstream(b,num);
            mysort(b,num);
            display(b,num);
        }
        if(ch=='D')
        {
            double *b=new double[num];
            isstream(b,num);
            mysort(b,num);
            display(b,num);
        }
        if(ch=='P')
        {
            point *b=new point[num];
            isstream(b,num);
            mysort(b,num);
            display(b,num);
        }
        if(ch=='S')
        {
            string *b=new string[num];
            isstream(b,num);
            mysort(b,num);
            display(b,num);
        }
    }
}

        5.对象相加函数模板(函数模板运算符重载)

#include<iostream>
using namespace std;

class cclock
{
private:
    int hour;
    int min;
    int sec;
public:
    cclock()
    {

    }
    cclock(int hval,int mval,int sval):hour(hval),min(mval),sec(sval)
    {

    }
    int ghour()
    {
        return hour;
    }
    int gmin()
    {
        return min;
    }
    int gsec()
    {
        return sec;
    }
    cclock operator +(cclock &lhs)
    {
        int as=hour + lhs.ghour();
        int bs=min + lhs.gmin();
        int cs=sec + lhs.gsec();
        if(cs>59)
        {
            cs=cs%10;
            bs++;
        }
        if(bs>59)
        {
            bs=bs%10;
            as++;
        }
        if(as>23)
            as=as%24;
        cclock flag1(as,bs,cs);
        return flag1;
    }
    friend ostream &operator <<(ostream &a,cclock &hs);
};

/*cclock operator +(cclock &rhs,cclock &lhs)
{
	int as=rhs.hour + lhs.hour;
	int bs=rhs.min + lhs.min;
	int cs=rhs.sec + lhs.sec;
	if(cs>59)
	    {
			cs=cs%10;
			bs++;
		}
	if(bs>59)
		{
			bs=bs%10;
			as++;
		}
	if(as>23)
		as=as%24;
		cclock flag(as,bs,cs);
		return flag;
}*/


ostream &operator <<(ostream &a,cclock &hs)
{
    a<<hs.hour <<" "<<hs.min<<" "<<hs.sec;
    return a;
}

class rmb
{
private:
    int yuan;
    int jiao;
    int fen;
public:
    rmb()
    {

    }
    rmb(int yval,int jval,int fval):yuan(yval),jiao(jval),fen(fval)
    {

    }
    int gyuan()
    {
        return yuan;
    }
    int gjiao()
    {
        return jiao;
    }
    int gfen()
    {
        return fen;
    }
    rmb operator +(rmb &lhs)
    {
        int as=yuan + lhs.gyuan();
        int bs=jiao + lhs.gjiao();
        int cs=fen + lhs.gfen();
        if(cs>9)
        {
            cs=cs%10;
            bs++;
        }
        if(bs>9)
        {
            bs=bs%10;
            as++;
        }
        rmb flag2(as,bs,cs);
        return flag2;
    }
    friend ostream &operator <<(ostream &a,rmb &hs);
};

/*rmb operator +(rmb &rhs,rmb &lhs)
{
	int as=rhs.yuan + lhs.yuan;
	int bs=rhs.jiao + lhs.jiao;
	int cs=rhs.fen + lhs.fen;
	if(cs>9)
	    {
			cs=cs%10;
			bs++;
		}
	if(bs>9)
		{
			bs=bs%10;
			as++;
		}
	rmb flag(as,bs,cs);
		return flag;
}*/


ostream &operator <<(ostream &a,rmb &hs)
{
    a<<hs.yuan <<" "<<hs.jiao<<" "<<hs.fen;
    return a;
}

template <class T>
T add(T a,T b,int n)
{
    T temp=a+b;
    return temp;
}

/*template <class T>
    void display(T a)
    {
    	cout << a << endl;
	}*/

int main()
{
    int x1,y1,z1;
    int x2,y2,z2;
    cin >> x1 >> y1 >> z1;
    cclock a(x1,y1,z1);
    cin >> x2 >> y2 >> z2;
    cclock b(x2,y2,z2);
    cclock c=add(a,b,60);
    cout << c <<endl;
    cin >> x1 >> y1 >> z1;
    rmb e(x1,y1,z1);
    cin >> x2 >> y2 >> z2;
    rmb f(x2,y2,z2);
    rmb g=add(e,f,10);
    cout << g << endl;
}

        6.简单类模板(类模板)

#include<bits/stdc++.h>
using namespace std;

template <class T>
class clist
{
private:
    T *data;
    int n;
public:
    clist()
    {

    }
    clist(int num,T a[])
    {
        n=num;
        data=new T[100];
        int j;
        for(j=0; j<num; j++)
            data[j]=-1;
        int i;
        for(i=0; i<num; i++)
            data[i]=a[i];
    }
    void index(int mval,T nval)
    {
        int j;
        for(j=n; j>mval; j--)
            data[j]=data[j-1];
        data[mval]=nval;
        n++;
    }
    void deletee(int mval)
    {
        int j;
        for(j=mval; j<n; j++)
            data[j]=data[j+1];
        n--;
    }
    void display()
    {
        int i;
        for(i=0; i<n-1; i++)
            cout <<data[i]<<" ";
        cout << data[n-1] << endl;
    }
};

int main()
{
    int i;
    int a[100];
    int num;
    cin >> num;
    for(i=0; i<num; i++)
        cin >> a[i];
    clist<int> s1(num,a);
    int m;
    int n;
    cin >> m >> n;
    s1.index(m,n);
    int x;
    cin >> x;
    s1.deletee(x);
    s1.display();
    int j;
    double b[100];
    int numw;
    cin >> numw;
    for(j=0; j<numw; j++)
        cin >> b[j];
    clist<double> s2(numw,b);
    int o;
    double p;
    cin >> o >> p;
    s2.index(o,p);
    int y;
    cin >> y;
    s2.deletee(y);
    s2.display();
}

        7.矩阵类模板(类模板)

#include<bits/stdc++.h>
using namespace std;

template <class T>
class cmartix
{
private:
    T a[100][100];
    int r;
    int c;
public:
    cmartix()
    {

    }
    cmartix(int rval,int cval,T b[][100]):r(rval),c(cval)
    {
        int i;
        int j;
        for(i=0; i<rval; i++)
            for(j=0; j<cval; j++)
                a[i][j]=b[i][j];
    }
    void transport()
    {
        int i,j;
        T p[100][100];
        for(i=0; i<r; i++)
            for(j=0; j<c; j++)
                p[i][j]=a[i][j];
        for(i=0; i<r; i++)
            for(j=0; j<c; j++)
                a[j][i]=p[i][j];
        swap(r,c);
    }
    void print()
    {
        int i;
        int j;
        for(i=0; i<r; i++)
        {
            for(j=0; j<c-1; j++)
                cout<<a[i][j]<<" ";
            cout<<a[i][c-1]<<endl;
        }
    }
};

int main()
{
    int t;
    int m,n;
    int i,j;
    cin >> t;
    while(t--)
    {
        char ch;
        cin >> ch >> m >> n;
        if(ch=='I')
        {
            int id[100][100];
            for(i=0; i<m; i++)
                for(j=0; j<n; j++)
                    cin >> id[i][j];
            cmartix<int> s1(m,n,id);
            s1.transport();
            s1.print();
        }
        if(ch=='C')
        {
            char id[100][100];
            for(i=0; i<m; i++)
                for(j=0; j<n; j++)
                    cin >> id[i][j];
            cmartix<char> s1(m,n,id);
            s1.transport();
            s1.print();
        }
        if(ch=='D')
        {
            double id[100][100];
            for(i=0; i<m; i++)
                for(j=0; j<n; j++)
                    cin >> id[i][j];
            cmartix<double> s1(m,n,id);
            s1.transport();
            s1.print();
        }
    }
}

        8.有界数组模板类(类模板)

#include<bits/stdc++.h>
using namespace std;

template<class T>
class boundarray
{
private:
    T *data;
    int n;
public:
    boundarray()
    {

    }
    boundarray(int nval,T a[])
    {
        n=nval;
        int i;
        data=new T[nval];
        for(i=0; i<nval; i++)
            data[i]=a[i];
    }
    int search(T key)
    {
        int i;
        for(i=0; i<n; i++)
            if(data[i]==key)
                return i;
        return -1;
    }
    void mysort()
    {
        int i;
        int j;
        for(i=0; i<n-1; i++)
            for(j=0; j<n-1-i; j++)
                if(data[j]>data[j+1])
                    swap(data[j],data[j+1]);
    }
    void display()
    {
        int i;
        for(i=0; i<n; i++)
            cout<<data[i]<<" ";
        cout<<endl;
    }
};

int main()
{
    int t;
    int i;
    cin >> t;
    while(t--)
    {
        char ch;
        int num;
        cin >> ch;
        if(ch=='I')
        {
            cin >> num;
            int p[100];
            for(i=0; i<num; i++)
                cin >> p[i];
            int kel;
            cin >> kel;
            boundarray<int> s1(num,p);
            s1.mysort();
            s1.display();
            cout << s1.search(kel)<<endl;
        }
        if(ch=='D')
        {
            cin >> num;
            double p[100];
            for(i=0; i<num; i++)
                cin >> p[i];
            double kel;
            cin >> kel;
            boundarray<double> s1(num,p);
            s1.mysort();
            s1.display();
            cout << s1.search(kel)<<endl;
        }
        if(ch=='C')
        {
            cin >> num;
            char p[100];
            for(i=0; i<num; i++)
                cin >> p[i];
            char kel;
            cin >> kel;
            boundarray<char> s1(num,p);
            s1.mysort();
            s1.display();
            cout << s1.search(kel)<<endl;
        }
    }
    return 0;
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值