C++ - Classes and Objects

本文介绍了C++中类的定义和对象的创建。通过一个具体的例子展示了如何创建一个名为Student的类,该类包含分数实例变量,输入方法以及计算总分的方法。学生对象可以读取并存储5个考试成绩,然后计算总分。输入和输出格式、约束条件以及样例输入输出均进行了说明。
摘要由CSDN通过智能技术生成

A class defines a blueprint for an object. We use the same syntax to declare objects of a class as we use to declare variables of other basic types. For example:

Box box1;          // Declares variable box1 of type Box
Box box2;          // Declare variable box2 of type Box

Kristen is a contender for valedictorian of her high school. She wants to know how many students (if any) have scored higher than her in the 5 exams given during this semester.

Create a class named Student with the following specifications:

  • An instance variable named scores to hold a student’s 5
    exam scores.
  • A void input() function that reads 5 integers and saves
    them to scores .
  • An int calculateTotalScore() function that returns the sum
    of the student’s scores.

Input Format

Most of the input is handled for you by the locked code in the editor.

In the void Student::input() function, you must read scores from stdin and save them to your instance variable.

Constraints
1 <= n <= 100
0 <= examscore <= 50

Output Format

In the int Student::calculateTotalScore() function, you must return the student’s total grade (the sum of the values in scores ).

The locked code in the editor will determine how many scores are larger than Kristen’s and print that number to the console.

Sample Input

The first line contains , the number of students in Kristen’s class. The subsequent lines contain each student’s exam grades for this semester.

3
30 40 45 10 10
40 40 40 10 10
50 20 30 10 10

Sample Output

1

Explanation

Kristen’s grades are on the first line of grades. Only student scored higher than her.

Solution

class Student {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值