《面向对象的程序设计》OJC++类与对象的学习

1.最胖的加菲(类与对象+数组)

题目描述

有一群猫猫,每只猫都有自己的名称和体重。

用类来描述猫,名称和体重都是私有属性,要求加入属性的get方法。其他函数根据需要自己定义

创建一个动态的猫对象数组,存储各只猫的名称和体重

根据猫的体重对数组做升序排序,并输出排序后每只猫的名称

题目涉及的数值均用整数处理

输入

第一行输入n表示有n只猫

第二行输入一只猫的名称和体重

依次输入n行

输出

输出一行,输出排序后的猫的名称

输入样例

4

chocolate 1500

water 400

cheese 3000

vegetable 200

输出样例

vegetable water chocolate cheese

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
class Cat
{
private:
    string name;
    int weight;
public:
    void getcat(string Name, int Weight)
    {
        name = Name;
        weight = Weight;
    }
    int getweight()
    {
        return weight;
    }
    string getname()
    {
        return name;
    }
};
//sort的时候保证是根据体重升序的
bool cmp(Cat& a, Cat& b)
{
    return a.getweight() < b.getweight();
}
//引用保证对主函数中的数据直接修改
void sortcat(Cat*& acat, int n)
{
    sort(acat, acat + n, cmp);
}
//Cat*acat相当于一个calss Cat类型的数组(注意*在&之前,因为&acat表引用acat)
void print(Cat*& acat, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << acat[i].getname();
        if (i == n - 1)cout << endl;
        cout << " ";
    }
}
int main()
{
    int n;
    cin >> n;
    Cat* acat = new Cat[n];//开一个calss Cat类型的数组
    for (int i = 0; i < n; i++)
    {
        string Name;
        int Weight;
        cin >> Name >> Weight;
        acat[i].getcat(Name, Weight);//一一赋值
    }
    sortcat(acat, n);
    print(acat, n);
    delete[]acat;//清除
    return 0;
}

2.存折类定义(类与对象)

题目描述

定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。

编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。

输入

第一个存折的账号、姓名、余额

存款金额

取款金额

第二个存折的账号、姓名、余额

存款金额

取款金额

输出

第一个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

第二个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

输入样例

9111 Tom 1000

500

1000

92220 John 500

500

1500

输出样例

Tom's balance is 1000

saving ok!

Tom's balance is 1500

withdraw ok!

Tom's balance is 500

John's balance is 500

saving ok!

John's balance is 1000

sorry! over limit!

John's balance is 1000

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
class CAccount
{
private:
    long account;
    string name;
    float balance;
public:
    void deposit(float in)
    {
        balance += in;
        puts("saving ok!");
    }
    void withdraw(float out)
    {
        if (out > balance)puts("sorry! over limit!");
        else
        {
            balance -= out;
            puts("withdraw ok!");
        }
    }
    void check()
    {
        cout << name << "'s balance is " << (int)balance << endl;
    }
    void get(long Account,string Name,float Balance)
    {
        account = Account;
        name = Name;
        balance = Balance;
    }
};
int main()
{
    CAccount* consumer = new CAccount[2];
    for (int i = 0; i <= 1; i++)
    {
        long aa;
        string nn;
        float bb;
        cin >> aa >> nn >> bb;
        consumer[i].get(aa, nn, bb);
        consumer[i].check();
        float in, out;
        cin >> in;
        consumer[i].deposit(in);
        consumer[i].check();
        cin >> out;
        consumer[i].withdraw(out);
        consumer[i].check();
    }
    return 0;
}

3.【程序填空】日期类外定义(类和对象)

题目描述

已知日期类TDate声明如下,完成以下程序填空

输入

第一行输入t,表示有t个日期

接着每行输入3个参数,对应年月日

输出

输出t行,每行输出当前日期和第二天日期,具体格式看样例

输入样例

4

2022 3 21

2020 2 28

2021 2 28

2022 12 31

输出样例

Today-20220321 Tomorrow-20220322

Today-20200228 Tomorrow-20200229

Today-20210228 Tomorrow-20210301

Today-20221231 Tomorrow-20230101

AC代码:

//头文件和日期类声明
#include<iostream>
#include<iomanip>
using namespace std;

class TDate {
private:
    int year, month, day;
public:
    int getYear() { return year; }
    int getMonth() { return month; }
    int getDay() { return day; }
    void set(int y, int m, int d);
    void print();
    void addOneDay();
};

//----在以下区域完成部分成员函数的类外定义----
/********** Write your code here! **********/
void TDate::set(int y, int m, int d)//记得加<calss名::函数名>就好了
{
    year = y;
    month = m;
    day = d;
}
void TDate::print()
{
    cout << year;
    if (month < 10)cout << 0;//输出前导0
    cout << month;
    if (day < 10)cout << 0;
    cout << day;
}
void TDate::addOneDay()
{
    if (month == 2 && day == 28)//判断闰年以及2月
    {
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            day += 1;
        else
        {
            day = 1;
            month += 1;
        }
    }
    else if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 30)//判断小月4,6,9,11
    {
        day = 1;
        month += 1;
    }
    else if (day == 31 && month == 12)//判断新的一年
    {
        day = 1;
        month = 1;
        year += 1;
    }
    else if (day == 31)//大月
    {
        month += 1;
        day = 1;
    }
    else day += 1;//不是月底都加一就好了
}
/*******************************************/
//主函数如下
int main()
{
    int t, y, m, d;
    cin >> t;
    while (t--)
    {
        TDate d1;
        cin >> y >> m >> d;
        d1.set(y, m, d);
        cout << "Today-";
        d1.print();
        d1.addOneDay();
        cout << " Tomorrow-";
        d1.print();
        cout << endl;
    }
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值