PTA复习

本文展示了C++中类的构造与析构函数的使用,包括学生的构造、体育俱乐部的创建、成绩录入、客户机管理等场景。同时,讨论了运算符重载的应用,如输入输出流操作符以及自增运算符。此外,还涉及类模板的实现,如有序数组的类模板设计,以及多重继承在派生类构造中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数

6-1 学生类的构造与析构


#include<bits/stdc++.h>
using namespace std;
class Student
{
    int num;
    string name;
    char sex;
public:
    Student(int n,string nam,char s):num(n),name(nam),sex(s){
        cout<<"Constructor called."<<endl;
    }
    void display(){
        cout<<"num:"<<num<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<endl;
    }
    ~Student(){cout<<"Destructor called."<<endl;}
};

6-2 体育俱乐部I(构造函数)

Club::Club(string n1, int y, string n2, int wr):name(n1),year(y),c(n2,wr)
{}
void Coach::show(){
    cout<<name<<" "<<winRate<<"%"<<endl;
}
void Club::show(){
    cout<<name<<" "<<year<<endl;
    c.show();
}

6-3 学生成绩的快速录入(构造函数)

class Student
{
    int no,score;
public:
    static int count;
    Student(int n,int s):no(n),score(s){count++;}
    Student(const Student& s):no(s.no+1),score(s.score){count++;}
    void display(){
        cout<<no<<" ";
        if(score==1)cout<<"Pass"<<endl;
        else cout<<"Fail"<<endl;
    }
};
int Student::count=0;

6-4 客户机类

#include<iostream>
using namespace std;
class Client
{
    static char ServerName;
    static int ClientNum;
public:
    Client(){ClientNum++;};
    static void changeServerName(char a);//注意没有大括号!!!,只是声明
    static void show();
};
char Client::ServerName='A';
int Client::ClientNum=0;
void Client::changeServerName(char a){
    ServerName=a;
}
void Client::show(){
    cout<<"server name:"<<ServerName<<endl;
    cout<<"num of clients:"<<ClientNum<<endl;
}
//注意都是静态成员函数,在类外定义;

6-5 学生成绩的输入和输出(运算符重载)

class Student
{
    string name;
    int score;
    static int num;//因为需要输出学生序号,可以在输出时++
public:
    friend istream& operator>>(istream& is,Student& s);
    friend ostream& operator<<(ostream& os,Student& s);
};
int Student::num=1;
istream& operator>>(istream& is,Student& s){
    is>>s.name>>s.score;
    return is;
}
ostream& operator<<(ostream& os,Student& s){
    os<<Student::num++<<". "<<s.name<<" ";//num注意前面加上Student::
    if(s.score>=60)cout<<"PASS";
    else cout<<"FAIL";
}

6-6 时钟模拟

#include<bits/stdc++.h>
using namespace std;
class MyTime
{
    int h,m,s;
public:
    MyTime(int h=0,int m=0,int s=0):h(h),m(m),s(s){}
    friend istream& operator>>(istream& is,MyTime& s);
    friend ostream& operator<<(ostream& os,MyTime& s);
    MyTime operator++();
};
istream& operator>>(istream& is,MyTime& s)
{
    is>>s.h>>s.m>>s.s;
    return is;
}
ostream& operator<<(ostream& os,MyTime& s)
{
    os<<s.h<<":"<<s.m<<":"<<s.s;
    return os;
}
MyTime MyTime::operator++()
{
    s++;
    if(s==60){m++,s=0;}
    if(m==60){h++,m=0;}
    h%=24;
    return *this;
}

6-7 使用成员函数重载复数类的运算符+

Complex& Complex::operator+(Complex& com)
{
    com.realPart+=this->realPart;//com.realPart+=realPart
    com.imgPart+=this->imgPart;//com.imgPart+=imgPart;
    return com;
}
// 因为有引用所以错误(指针错误,即段错误)
// Complex& Complex::operator+(Complex& com)
// {
//     Complex c3;
//     c3.realPart=com.realPart+this->realPart;
//     c3.imgPart=com.imgPart+this->imgPart;
//     return c3;
// }

6-8 多重继承派生类构造函数

class Graduate:public Teacher,public Student
{
public:
    Graduate(string nam,int a,char s,string t,float sco,float w)
    :Student(nam,s,sco),Teacher(nam,a,t),wages(w){}
    void show()
    {
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"score:"<<score<<endl;
    cout<<"title:"<<title<<endl;
    cout<<"wages:"<<wages<<endl;
    }
private:
    float wages;
};
//注意不要用在类对象里声明,否则是错误的
//发现上面两个类定义的display()没有用处

6-9 抽象类Shape

class Rectangle:public Shape//对于这道题,不用继承也对,因为源代码中声明基类指针,但是没用到,所以可以不用虚函数
{
    double w,h;
public:
    Rectangle(double w,double h):w(w),h(h){}
    double getArea(){
        return w*h;
    }
    double getPerim(){
       return 2*(w+h);
    }
};
class Circle:public Shape
{
    double r;
public:
    Circle(double r):r(r){}
    double getArea(){
        return 3.14*r*r;
    }
    double getPerim(){
       return 2*3.14*r;
    }
};

6-10 有序数组(类模板)

template <class T>
class MyArray
{
private:
    T *data;//数组类型
    int size;
public:
    MyArray(int n);
    ~MyArray();
    bool check();
    void display();
    void sort();
};

template<class T>
MyArray<T>::MyArray(int n)
{
        size=n;
        data=new T[n];
        for(int i=0; i<n; i++)cin>>data[i];
}

template<class T>
void MyArray<T>::sort()
{
    for(int i=1;i<size;i++)
    {
        for(int j=0;j<size-i;j++)
        {
            if(data[j]>data[j+1])
            {
                T temp=data[j];
                data[j]=data[j+1];
                data[j+1]=temp;
            }
        }
    }
}

template<class T>
void MyArray<T>::display()
{
    for(int i=0; i<size; i++)
    {
        if(i!=0)cout<<" "<<data[i];//注意格式错误,一行后面没有空格
        else cout<<data[i];
    }
     cout<<endl;
}

编程

7-1 字符串替换

#include<bits/stdc++.h>
using namespace std;
void slove(){
    string a;
    getline(cin,a);
    while(1){//a表示总的,b表示每一行的字符串
        a+='\n';
        string b;
        getline(cin,b);
        if(b=="end")break;
        a+=b;
    }
    string c1,c2;
    cin>>c1>>c2;
    int temp=a.find(c1);//找到的是下标
    while(temp!=-1){
        a.replace(temp,c1.size(),c2);
        //用c2替换指定字符串从起始位置temp开始长度为ca.size()的字符 
        temp=a.find(c1,temp+1);
    }
    cout<<a;
}
int main(){
    slove();
}

7-2 类的定义和使用

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    cout<<a+c<<" "<<b+d;
}
// class Point
// {
//     int x,y;
//     int x1,y1;
// public:
//     Point(int a,int b)
//     {
//         x=a;
//         y=b;
//     }
//     ~Point(){}
//     void work(Point p2)
//     {
//         x1=x+p2.x;
//         y1=y+p2.y;
//     }
//     int getx1()
//     {
//         return x1;
//     }
//     int gety1()
//     {
//         return y1;
//     }
// };
// int main()
// {
//     int a,b,c,d;
//     cin>>a>>b>>c>>d;
//     Point p1(a,b),p2(c,d);
//     p1.work(p2);
//     cout<<p1.getx1()<<" "<<p1.gety1();
// }

7-5 2017final函数模板

#include<bits/stdc++.h>
using namespace std;
class Complex
{
    double a,b,c;
public:
    Complex(double a,double b,double c):a(a),b(b),c(c){}
    friend double operator - (Complex a, Complex b);
};

double operator - (Complex a,Complex b){
    return sqrt(pow(a.a-b.a,2)+pow(a.b-b.b,2)+pow(a.c-b.c,2));
}

template <class T>
double dist(T a,T b){
    
    return abs(a-b);
}

int main(){
    int n;
    while(cin>>n){
        //如果用switch语句,一个变量名在多个case中不能使用。不能同用变量名!
        if(n==1){
            int a,b;
            cin>>a>>b;
            cout<<dist(a,b)<<endl;
        }
        else if (n==2){
            double a,b;
            cin>>a>>b;
            cout<<dist(a,b)<<endl;
        }
        else if (n==3){
            double a1,a2,a3,b3,b1,b2;
            cin>>a1>>a2>>a3>>b1>>b2>>b3;
            Complex a(a1,a2,a3),b(b1,b2,b3);
            cout<<dist(a,b)<<endl;
    }}
    return 0;
}
//下面是switch语句
// #include<bits/stdc++.h>
// using namespace std;
// class Complex
// {
//     double a,b,c;
// public:
//     Complex(double a,double b,double c):a(a),b(b),c(c){}
//     friend double operator - (Complex a, Complex b);
// };
// double operator - (Complex a,Complex b){
//     return sqrt(pow(a.a-b.a,2)+pow(a.b-b.b,2)+pow(a.c-b.c,2));
// }
// template <class T>
// double dist(T a,T b){
//     return abs(a-b);
// }
// int main(){
//     int n;
//     while(cin>>n){
//         switch(n){
//             case 1:
//                 int a,b;
//                 cin>>a>>b;
//                 cout<<dist(a,b)<<endl;
//                 break;
//             case 2:
//                 double c,d;
//                 cin>>c>>d;
//                 cout<<dist(c,d)<<endl;
//                 break;
//             case 3:
//                 double a1,a2,a3,b3,b1,b2;
//                 cin>>a1>>a2>>a3>>b1>>b2>>b3;
//                 Complex e(a1,a2,a3),f(b1,b2,b3);
//                 cout<<dist(e,f)<<endl;
//                 break;
//         }
//     }
//     return 0;
// }

7-6 统计单词的数量并输出单词的最后一个字符

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	getline(cin,s,'\n');
	int k=0;
	for(int i=1;i<=s.size();i++)
	{
		if(s[i]==' '&&s[i-1]!=' ')
		{
			cout<<s[i-1];
			k++;
		}
	}
	if(s[s.size()-1]!=' ')//特殊样例,最后输入空空空
	{
		cout<<s[s.size()-1];
		k++;
	}
	cout<<endl;
	cout<<k;
}

7-7 组最大数

#include<bits/stdc++.h>
using namespace std;
bool cmp(const string& a,const string& b)
{
	return (a+b) > (b+a);
}
int main() 
{
	int n;
	while(cin>>n) 
    {
        string s;
        vector<string> v;
		while(n--) 
        {
			cin>>s;
			v.push_back(s);
		}
		sort(v.begin(),v.end(),cmp);
        string res="";
		for(int i=0;i<v.size();i++) res+=v[i];
		cout<<res<<endl;
	}
}

7-8 字符串排序--string类的使用

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        vector<string> v;
        string s;
        getchar();//cin.get()
        while( (n--) && getline(cin,s) && s!="stop" )
             v.push_back(s);
        for(int i=1;i<v.size();i++)
        {
            for(int j=0;j<v.size()-i;j++)
            {
                if(v[j].size()>v[j+1].size())
                {
                    string s=v[j];
                    v[j]=v[j+1];
                    v[j+1]=s;
                }
            }
        }
        for(int i=0,n=v.size();i<n;++i) cout<<v[i]<<endl;
    }
}

//用cpm部分正确,有原因百度
// #include<bits/stdc++.h>
// using namespace std;
// bool cmp(const string& a,const string& b)
// {
//     if(a.size()==b.size())return a.size()>b.size();//如果存在多个字符串长度相同,则按照原始输入顺序输出。
// 	return a.size()<b.size();
// }
// int main()
// {
//     int n;
//     while(cin>>n)
//     {
//         vector<string> v;
//         string s;
//         getchar();//或者cin.get();
//         while( (n--) && getline(cin,s) && s!="stop" )
//              v.push_back(s);
//         sort(v.begin(),v.end(),cmp);
//         for(int i=0,n=v.size();i<n;++i) cout<<v[i]<<endl;
//     }
// }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值