OJ C++ 类的基础题

一、长方体类

描述

定义一个长方形类Rectangle:

1、具有两个属性

(1)w:宽,int类,公有

(2)h:高,int类,公有

2、具有两个方法

(1)int getCir():返回长方形的周长,公有

(2)int getArea():返回长方形的面积,公有

在主函数中输入长方形的宽和高,调用类的方法输出长方形的周长和面积。

提示说明

针对每组案例,输出长方形的周长和面积,中间隔一个空格。

每组案例输出完都要换行。

2

1 2

3 4

6 2

14 12

主函数中有这几句关键语句

Rectangle r;

cin>>r.w>>r.h;

cout<<r.getCir()<<' '<<r.getArea()<<endl;

#include <iostream>       //  固定头文件
using namespace std;
class Rectangle      //类的声明  【先声明,后调用】
{
public:
    int w;
    int h;
public:
    int getCir()         //返回周长
    {
        return 2 * (w + h);
    }
    int getArea()      //返回面积
    {
        return w * h;
    }
};

int main()        //主函数
{
    int i ;
    int n;          
    Rectangle r;        //函数调用
    cin >> n;
    for (int i = 1; i <= n; i++)   //利用循环实现两组数据的计算
    {
        cin >> r.w >> r.h;        //输入数据
        cout << r.getCir() << '  ' << r.getArea() << endl;   //输出数据
    }   
    return 0;  【主函数必须有返回值】
}

二、学生类

描述

定义一个学生类Student:

1、具有两个属性

(1)name:姓名,string类,私有

(2)score:成绩,int类,私有

2、具有四个方法

(1)void setName(string):根据参数值设置姓名属性,公有

(2)string getName():返回姓名,公有

(3)void setScore(int):根据参数值设置成绩属性,公有

(4)int getScore():返回成绩,公有

在主函数中输入学生的姓名(不会有空格)和成绩(先保存到两个变量中),调用类的2个set方法进行设置。然后调用类的2个get方法输出学生的姓名和成绩。

输入

一个正整数n,表示n组案例。

每组案例由一个字符串和一个整数组成。

输出

针对每组案例,输出学生的姓名和成绩,中间隔一个空格。

每组案例输出完都要换行。

样例输入复制样例 

2

Tom 80

Jerry 100

样例输出

Tom 80

Jerry 100

#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
    string name;
    int score;
public:
    void setName(string x)   
    {
        name = x;
    }
    void setScore(int y)
    {
        score = y;
    }

    string getName()
    {
        return name;  \\返回
    }
    int getScore()
    {
        return score;  \\返回
    }
};
int main()
{
    int k;
    string j;
    int n;
    Student r;
    cin >> n;
    for ( n; n > 0; n--)
    {
        cin >> j >> k;
        r.setName(j);
        r.setScore(k);
        cout << r.getName() << ' ' << r.getScore() << endl;

    }
    return 0;
}

【弄明白set 和get 作用及使用规则】???

三、学生类2

描述

定义一个学生类Student:

1、具有两个属性

(1)name:姓名,string类,私有

(2)score:3门课的成绩,长度为3的int数组,私有

2、具有四个方法

(1)void setName(string):根据参数值设置姓名属性,公有

(2)string getName():返回姓名,公有

(3)void setScore(int[ ]):根据参数值(存放3门课成绩的数组)设置成绩属性,公有

(4)int getScore():返回3门课的总成绩,公有

在主函数中输入学生的姓名(不会有空格)和成绩,调用类的2个set方法进行设置。然后调用类的2个get方法输出学生的姓名和总成绩。

#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
    string name;
    int score[3];
public:
    void setName(string x)
    {
        name = x;
    } 
   string getName()
    {
        return name;
    }
    void setScore(int y[])
    {
        score[0] = y[0];
        score[1] = y[1];
        score[2] = y[2];
    } 
    int getScore()
    {
        return score[0]+score[1]+score[2];
    }
    
   
};
int main()
{
	int i ;
    int k[3];
    string j;
    int n;
    Student r;
    cin >> n;
    for ( n; n>0; n--)
    {
        cin >> j;
        for (i = 0; i < 3; i++)
        {
            cin >> k[i];
        }
        r.setName(j);
        r.setScore(k);
        cout << r.getName() << ' '<< r.getScore() << endl;

    }
    return 0;
}

四、私有成员

描述

定义一个长方形类Rectangle,类的成员包括:

1、width:长方形的宽,double类型,私有

2、height:长方形的高,double类型,私有

3、void setWidth(double)函数:设置长方形的宽,公有

4、void setHeight(double)函数:设置长方形的高,公有

5、double getCir()函数:返回长方形的周长,公有

6、double getArea()函数:返回长方形的面积,公有

规定主函数是如下代码(不可以修改),根据输入输出,设计Rectangle类以满足要求。

int main()
{
 Rectangle r;
 double w, h;
 cin >> w >> h;
 r.setWidth(w);
 r.setHeight(h);
 cout << r.getCir() << endl;
 cout << r.getArea() << endl;
 return 0;
}

#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
    string name;
    int score[3];
public:
    void setName(string x)
    {
        name = x;
    } 
   string getName()
    {
        return name;
    }
    void setScore(int y[])
    {
        score[0] = y[0];
        score[1] = y[1];
        score[2] = y[2];
    } 
    int getScore()
    {
        return score[0]+score[1]+score[2];
    }
};
int main()
{
    int i ;
    int k[3];
    string j;
    int n;
    Student r;
    cin >> n;
    for ( n ; n>0; n--)
    {
        cin >> j;
        for (i = 0; i < 3; i++)
        {
            cin >> k[i];
        }
        r.setName(j);
        r.setScore(k);
        cout << r.getName() << ' '<< r.getScore() << endl;

    }
    return 0;
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值