C++作业缓存

本文通过四个C++程序实例介绍了面向对象编程的基本概念,包括类的定义、继承、多态、异常处理及文件操作等内容。展示了如何创建类、实现构造与析构函数、重载运算符、使用继承与虚基类、处理异常并进行对象数组的排序及文件读写。

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

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

class student
{
public:
    student(){}
    student(string n,int ids,int x,int y,int z):name(n),id(ids),a_grade(x),b_grade(y),c_grade(z)
    {
        cout << "构造中……" << endl;
    }
    ~ student()
    {
        cout << "析构中……" << endl;
    }
    friend istream& operator >> (istream &,student &);
    friend ostream& operator << (ostream &,student &);
private:
    string name;
    int id;
    int a_grade,b_grade,c_grade;
};
istream& operator >> (istream &in,student &c)
{
    in >> c.name >> c.id >> c.a_grade >> c.b_grade >> c.c_grade;
    return in;
}
ostream& operator << (ostream &out,student &c)
{
    out <<"姓名:"<< c.name << "学号:" << c.id << "线代成绩: " << c.a_grade << "高学成绩: " << c.b_grade << "概率成绩: " << c.c_grade << endl;
    return out;
}

int main()
{
    student stu1("ZhuangBiTang",1507064227,12,32,23),stu2;
    cout << stu1;
    cin >> stu2;
    cout << stu2;
    return 0;
}


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

class animal
{
public:
    animal(){}
    animal(int x):a(x){}
    ~animal(){}
protected:
    int a;
};
class cat1 : virtual public animal
{
public:
    cat1(int x,int y):b(x),animal(y){}
    cat1(){}
    ~cat1(){}
protected:
    int b;
};

class cat2 : virtual public animal
{
public:
    cat2(){}
    cat2(int x,int y):c(x),animal(y){}
    ~cat2(){}

protected:
    int c;
};
class cat3: public cat1,public cat2
{
public:
    cat3(){}
    cat3(int x,int y,int z,int k):d(x),cat1(y,k),cat2(z,k),animal(k){}
    ~cat3(){}
    display()
    {
        cout << a << " " << b << " " << c << " " << d << endl;
    }
protected:
    int d;
};
int main()
{
    cat3 x(1,2,3,4);
    x.display();
    animal y(1);
    return 0;
}

3.
#include<bits/stdc++.h>
using namespace std;
namespace d1
{
    class is_error//自定义异常类
    {
    public:
        is_error():s("y == 0 can't be divide !"){}
        string s;
    };
    void work(double x,double y)//x / y º¯Êý
    {
        try
        {
            if(!y)
                throw is_error();
            cout << " x / y = " << x/y << endl;
        }
        catch(is_error & x)
        {
            cout << x.s << endl;
        }
    }
}
namespace d2
{
    class is_error
    {
    public:
        is_error():s("y == 0 can't be divide !"){}
        string s;
    };
    void work(double x,double y)//x / y º¯Êý
    {
        try
        {
            if(!y)
                throw is_error();
            cout << " x / y = " << x/y << endl;
        }
        catch(is_error & x)
        {
            cout << x.s << endl;
        }
    }
}
int main()
{
    int x,y;
    scanf("%d%d",&x,&y);
    d1 :: work(x,y);
    d2 :: work(y,x);
    return 0;
}

4.
#include<bits/stdc++.h>
using namespace std;
class student
{
public:
    student(){}
    student(int x,int y):a_grade(x),b_grade(y){}
    ~ student(){}
    int a_grade,b_grade;
};
bool cmp(const student &a,const student &b)
{
    if(a.a_grade == b.a_grade)
        return a.b_grade < b.b_grade;
    return a.a_grade < b.a_grade;
}
int main()
{

    vector<int>v;
    for(int i = 1; i <= 10; i ++)
    {
        int x;
        scanf("%d",&x);
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    for(int i = 0; i < v.size(); i ++)
    printf("%d%c",v[i],i < v.size() - 1 ? ' ':'\n');

    vector<student>vs;
    for(int i = 1; i <= 5; i ++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        vs.push_back(student(x,y));
    }
    sort(vs.begin(),vs.end(),cmp);

    ofstream outfile;
    outfile.open("out.txt",ios::out);
    if(!outfile)
        cout << " open failed !" << endl;
    for(int i = 0; i < vs.size(); i ++)
        outfile << vs[i].a_grade << " " << vs[i].b_grade << endl;
    outfile.close();

    ifstream infile;
    infile.open("out.txt",ios::in);
    for(int i = 0; i < vs.size(); i ++)
        infile >> vs[i].a_grade >> vs[i].b_grade;
    for(int i = 0; i < vs.size(); i ++)
        cout << vs[i].a_grade << " " << vs[i].b_grade << endl;
    infile.close();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值