WUST OJ C++ 参考代码

WUST OJ C++ 参考代码

WUST OJ C++ 面向对象题集参考代码整理,代码块中的 main 测试函数已删去,方便按题目直接复制提交。
复制说明

2025.6.19 补充:现在开始我有 wustoj 的管理权限啦,下面提到的出现问题的题面都已更改,如果你有新的发现,欢迎评论指错~ 由于 wustoj 属于闭源项目,无法看到他人提交代码,故在此记录ac 代码,以便大家共同交流~希望你不是直接 CV 大法!

提交前

不同批次题面可能会调整类名、函数签名或输出格式。提交前最好再对照一次当前题面。

1.类和对象

c++1001 时钟类 Clock 的设计

c++
#include<iostream>
using namespace std;

//你提交的代码在这里
class Clock
{
public:
    Clock()
    {
        hour = 8;
        minute = 16;
        second = 24;
    }
    Clock(int h, int m, int s)
    {
        hour = h;
        minute = m;
        second = s;
    }
    int hour, minute, second;
    void Show()
    {
        cout << hour << ":" << minute << ":" << second << endl;
    }
    void Set(int hh, int mm, int ss)
    {
        hour = hh;
        minute = mm;
        second = ss;
    }
};

c++1002 点类 Point 的设计

c++
#include<iostream>
using namespace std;

//你提交的代码在这里
class Point
{
public:
    Point()
    {
        x = 10;
        y = 16;
    }
    Point(int xx, int yy)
    {
        x = xx;
        y = yy;
    }
    int x, y;
    void Show()
    {
        cout << "(" << x << "," << y << ")" << endl;
    }
    void Set(int xxx, int yyy)
    {
        x = xxx;
        y = yyy;
    }
};


c++1003 圆类 Circle 的设计

c++.1004 矩形类 Rectangle 的设计

c++.1005 复数类 Complex 的设计

c++.1006 日期类 Date 的设计

c++.1007 学生类 Student 的设计

c++.1008 线段类 Line 的设计

c++.1009 国家类 Country 的设计

c++.1010 长期存款类 Fixed_Deposit 的设计

c++.1011 书类 Book 的设计

c++.1012 建筑物类 building 的设计

c++.1013 课程类 Course 的设计

c++.1014 实验室类 Laboratory 的设计

c++.1015 个人 Person 类的设计

2.运算符重载

c++.2001 时间类运算符重载

c++.2002 日期类运算符重载

c++.2003 复数类运算符重载

c++.2004 学生类运算符重载

c++
#include<iostream>
#include<cstring>
using namespace std;

//你提交的代码在这里
class Student
{
public:
    int id;
    string name;
    int score;
    bool operator> (Student s)
    {
        if (this->score == s.score) return this->id > s.id;
        else return this->score > s.score;
    }
};
istream& operator>> (istream& cin, Student& s)
{
    cin >> s.id >> s.name >> s.score;
    return cin;
}
ostream& operator<< (ostream& cout, Student& s)
{
    cout << "id=" << s.id << ",name=" << s.name << ",score=" << s.score;
    return cout;
}

c++.2005 短整数类运算符重载

c++.2006 个人 Person 类运算符重载

c++.2007 圆类运算符重载

c++.2008 分数类运算符重载

c++,2009 整型动态数组类及运算符重载

c++.2010 分数类运算符重载(2)

c++.2011 圆类 运算符重载(2)

c++
#include<iostream>
#include<iomanip>
using namespace std;

//你提交的代码在这里
class Circle {
public:
    double radius;
    Circle& operator= (double r) {
        radius = r;
        return *this;
    }
    bool operator> (Circle a) {
        return radius > a.radius;
    }
};
ostream& operator<< (ostream& cout, Circle& s) {
    cout << "radius=" << s.radius << ",area=" << 3.14159 * s.radius * s.radius;
    return cout;
}

c++.2012 矩形类运算符重载

c++
#include<iostream>
#include<iomanip>
using namespace std;

//你提交的代码在这里
class Rectangle {
public:
    double width, height;
    bool operator> (Rectangle& a) {
        return (width * height > a.height * a.width);
    }
};
istream& operator>> (istream& cin, Rectangle& a) {
    cin >> a.width >> a.height;
    return cin;
}
ostream& operator<< (ostream& cout, Rectangle& a) {
    cout << "width=" << a.width << " height=" << a.height << " area=" << a.width * a.height;
    return cout;
}

c++.2013 日期类运算符重载(2)

这里的题干。。。。。。。。。

c++.2014 时间类运算符重载(2)

c++.2015 字符串 string 运算符重载

c++.2016 字符串 string 运算符重载(2)

c++.2017 矩形类运算符重载(2)

c++
#include<iostream>
#include<iomanip>
using namespace std;

//你提交的代码在这里
class Rectangle {
public:
    double width, height;
    bool operator> (Rectangle& a) {
        return (width * height > a.height * a.width);
    }
};
double operator+ (double s, Rectangle& a) {
    return (s + a.width * a.height);
}
istream& operator>> (istream& cin, Rectangle& a) {
    cin >> a.width >> a.height;
    return cin;
}
ostream& operator<< (ostream& cout, Rectangle& a) {
    cout << "width=" << a.width << ",height=" << a.height << ",area=" << a.width * a.height;
    return cout;
}

c++.2018 复数类运算符重载(2)

c++.2019 学生类运算符重载(2)

c++
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;

//你提交的代码在这里
class Student
{
public:
    int id;
    string name;
    int score;
    bool operator> (Student s)
    {
        if (this->score == s.score) return this->id > s.id;
        else return this->score > s.score;
    }
};
int operator+ (int a, Student s) {
    return a + s.score;
}
istream& operator>> (istream& cin, Student& s)
{
    cin >> s.id >> s.name >> s.score;
    return cin;
}
ostream& operator<< (ostream& cout, Student& s)
{
    cout << "id=" << s.id << ",name=" << s.name << ",score=" << s.score;
    return cout;
}

c++.2020 个人 Person 类运算符重载(2)

c++
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;

//你提交的代码在这里
class Person {
public:
    string name = "NULL";
    int age = 0;
    bool operator> (Person p) {
        if (age != p.age) {
            return age > p.age;
        }
        return name > p.name;
    }
    Person& operator= (Person p) {
        name = p.name;
        age = p.age;
        return *this;
    }
};
istream& operator>> (istream& cin, Person& s) {
    cin >> s.name >> s.age;
    return cin;
}
ostream& operator<< (ostream& cout, Person& s) {
    cout << "name=" << s.name << ",age=" << s.age;
    return cout;
}

c++.CSTRING 类的设计

3.继承与派生

c++.3001 日期类(由时间类派生)

c++.3002 圆类(由 Point 点类派生)

c++.3003Computer 计算机类(由 CPU 类派生)

c++.3004Province 省类(由 Country 国家类派生)

c++3005Rectangle 矩形类(由 Point 点类派生)

c++.3006Three_dimensional 三维坐标类(由 Point 点类派生)

c++.3007Person 人员类(由 CDate 日期类派生)

c++.3008Teacher 教师类(由 Person 人员类派生)

c++.3009Student 学生类(由 Person 人员类派生)

c++.3010Dog 狗类(由 Mammal 哺乳动物类派生)

c++.3011Sofa 沙发类(由 Furniture 家具类派生)

c++.3012Bed 床类(由 Furniture 家具类派生)

c++.3013AlarmClock 闹钟类(由 Clock 钟表类派生)

c++.3014Doctor 医生类(由 Person 人员类派生)

c++.3015Employee 雇员类(由 Person 人员类派生)

至此!完结撒花~

代码编写者: shoper

2025 | 中国近现代史重点
已抵达博客尽头

评论区

评论加载中...