题源地址
1:长方形类
描述 |
---|
定义一个长方形类Rectangle: 1、具有两个属性 (1)w:宽,int类,公有 (2)h:高,int类,公有 2、具有两个方法 (1)int getCir():返回长方形的周长,公有 (2)int getArea():返回长方形的面积,公有 在主函数中输入长方形的宽和高,调用类的方法输出长方形的周长和面积。 |
输入 |
一个正整数n,表示n组案例。 每组案例由两个正整数w和h组成。 |
输出 |
针对每组案例,输出长方形的周长和面积,中间隔一个空格。 每组案例输出完都要换行。 |
样例输入 |
2 1 2 3 4 |
样例输出 |
6 2 14 12 |
提示说明 |
主函数中有这几句关键语句 Rectangle r; cin>>r.w>>r.h; cout<<r.getCir()<<' '<<r.getArea()<<endl; |
关键点:
学习类的构造及其使用方式。
代码:
/*
主函数中有这几句关键语句
Rectangle r;
cin>>r.w>>r.h;
cout<<r.getCir()<<' '<<r.getArea()<<endl;
*/
#include <iostream>
using namespace std;
// class definition
class Rectangle{
public:
// member variables
int w;
int h;
// member functions
int getCir(){
return 2*(w+h);
}
int getArea(){
return w*h;
}
};
int main()
{
// read the input
int n;
cin >> n;
while(n--){
// create a Rectangle object
Rectangle r;
// read the width and height
cin >> r.w >> r.h;
// print the result
cout << r.getCir() << ' ' << r.getArea() << endl;
}
return 0;
}
2:学生类
描述 |
---|
定义一个学生类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 |
关键点:
你好
代码:
/*
在主函数中输入学生的姓名(不会有空格)和成绩(先保存到两个变量中),
调用类的2个set方法进行设置。
然后调用类的2个get方法输出学生的姓名和成绩。
*/
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
// data members
string name;
int score;
public:
void setName(string name){
this->name = name;
}
string getName(){
return name;
}
void setScore(int score){
this->score = score;
}
int getScore(){
return score;
}
};
int main()
{
// input number of students
int n;
cin >> n;
while(n--){
// input name and score
string name;
int score;
cin >> name >> score;
// create object of Student
Student s;
s.setName(name);
s.setScore(score);
// output name and score
cout << s.getName() << " " << s.getScore() << endl;
}
return 0;
}
3:学生类-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方法输出学生的姓名和总成绩。 |
输入 |
一个正整数n,表示n组案例。 每组案例由一个字符串和三个整数组成。 |
输出 |
针对每组案例,输出学生的姓名和总成绩,中间隔一个空格。 每组案例输出完都要换行。 |
样例输入 |
2 Tom 80 80 80 Jerry 100 100 100 |
样例输出 |
Tom 240 Jerry 300 |
关键点:
编程就要笑着写。
代码:
/*
定义一个学生类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 name)
{
this->name = name;
}
string getName()
{
return name;
}
void setScore(int score[])
{
for (int i = 0; i < 3; i++)
{
this->score[i] = score[i];
}
}
int getScore()
{
int sum = 0;
for (int i = 0; i < 3; i++)
{
sum += score[i];
}
return sum;
}
};
int main()
{
int n;
cin >> n;
while(n--){
string name;
int score[3];
cin >> name;
for (int i = 0; i < 3; i++)
{
cin >> score[i];
}
Student s;
s.setName(name);
s.setScore(score);
cout << s.getName() << " " << s.getScore() << endl;
}
return 0;
}
4:私有成员
描述 |
---|
定义一个长方形类Rectangle,类的成员包括: 1、width:长方形的宽,double类型,私有 2、height:长方形的高,double类型,私有 3、void setWidth(double)函数:设置长方形的宽,公有 4、void setHeight(double)函数:设置长方形的高,公有 5、double getCir()函数:返回长方形的周长,公有 6、double getArea()函数:返回长方形的面积,公有 规定主函数是如下代码(不可以修改),根据输入输出,设计Rectangle类以满足要求。 int main() |
输入 |
只有一组案例。两个浮点数w和h,分别表示长方形的宽和高。 |
输出 |
先输出一个浮点数,表示长方形的周长,换行。 再输出一个浮点数,表示长方形的面积,换行。 |
样例输入 |
5 3.5 |
样例输出 |
17 17.5 |
关键点:
笑点解析:
代码:
/*
定义一个长方形类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>
using namespace std;
class Rectangle
{
private:
double width;
double height;
public:
void setWidth(double w)
{
width = w;
}
void setHeight(double h)
{
height = h;
}
double getCir()
{
return 2 * (width + height);
}
double getArea()
{
return width * height;
}
};
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;
}