具有运算符重载的学生类 [英] Student class with operator Overloading

查看:90
本文介绍了具有运算符重载的学生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是这个社区的新手,也是编程新手。我一直负责该程序,我的问题是我不知道运算符超载。那么,任何人都可以帮助我吗?



开发一个包含以下头文件的学生课程:

#ifndef STUDENT_H

#define STUDENT_H

#include

#include

#include

using namespace std;

class学生{

public:

Student();

Student(字符串名称,字符串姓氏,int a1, int a2,int test,int exam);

string getName()const;

string getSurname()const;

int getAssignment1Mark() const;

int getAssignment2Mark()const;

int getLabTestMark()const;

int getExamMark()const;

void setAssignment1Mark(int);

void setAssignment2Mark(int);

void setLabTestMark(int);

void setExamMark(int);

bool传递()const;

string getGrade()const;

朋友ostream&operator<<(ostream&stream,const Student&) ;

bool运算符<(const Student&)const;

private:

string name;

string surname;

int assignment1Mark;

int assignment2Mark;

int labTestMark;

int examMark;

};

#endif





实现类,即开发符合以下规范的实现文件Student.cpp:

(1)数据字段有自我解释标识符及其含义应在简介中明确。

(2)设置功能允许用户在指定的边距内设置评估任务标记,例如setExamMark确保examMark在适当的范围内 - 从0到50.

(3)函数getGrade()返回学生的成绩,按照简介中的规定计算。其余的get函数只返回相应字段的值。

(4)如果这个学生已通过课程,则函数passed()返回true,否则返回false(参见引言)。

(5)朋友ostream&operator<<(ostream&stream,const Student&) - 函数重载<<运算符,它允许以下列格式输出Student对象信息:

Underwood Scott:

作业1 8

作业2 16

实验室测试0

考试34

等级P



(6)bool operator<(const Student&student)const - 函数重载<操作员,允许按字典顺序比较学生的全名。



折叠|复制代码

下面介绍如何计算商标和等级:

实验室测试标记 - 是0到10之间的整数;

Assignment1标记 - 是0到20之间的整数;

Assignment2标记 - 是0到20之间的整数;

期末考试标记 - 是0到50之间的整数;



当且仅当

(Assignment1 Mark + Assignment2 Mark)> = 20和(Lab Test Mark + Final)时,学生才能通过该课程考试标记)> = 30



折叠|复制代码

有5个等级,根据以下规则计算:

F - 如果总分少于40;

MF - 如果(40 <=总标记<50)或(总标记> = 50但学生没有通过);

P - if(50 <= Total Mark< ; 60)和学生通过;

C - 如果(60 <=总标记<70)并且学生通过;

D - 如果(70 <=总计标记< 80)和学生通过;

HD - 如果总标记> = 80;

Hi All,
I am new to this community and new to the programming also. I have been tasked with the program and my problem is that I don''t know the operator overloading. So, anyone can help me ?

Develop a Student class that has the following header file:
#ifndef STUDENT_H
#define STUDENT_H
#include
#include
#include
using namespace std;
class Student{
public:
Student();
Student(string name,string surname,int a1, int a2, int test, int exam);
string getName()const;
string getSurname()const;
int getAssignment1Mark()const;
int getAssignment2Mark()const;
int getLabTestMark()const;
int getExamMark()const;
void setAssignment1Mark(int);
void setAssignment2Mark(int);
void setLabTestMark(int);
void setExamMark(int);
bool passed()const;
string getGrade()const;
friend ostream& operator<<(ostream& stream, const Student &);
bool operator<(const Student &)const;
private:
string name;
string surname;
int assignment1Mark;
int assignment2Mark;
int labTestMark;
int examMark;
};
#endif


implement the class, i.e. to develop the implementation file "Student.cpp" that conforms to the following specifications:
(1) The data fields have self explanatory identifiers, and their meaning should be clear from the Introduction.
(2) The "set" functions allow the user to set the assessment task marks within the specified margins, e.g. setExamMark ensures that the examMark is in the appropriate range – from 0 to 50.
(3) The function getGrade() returns the student’s grade, calculated as specified in the Introduction. The rest of the "get" functions simply return the values of the corresponding fields.
(4) The function passed() returns true if this student , has passed the course, and returns false otherwise (see the Introduction).
(5) friend ostream& operator<<(ostream& stream, const Student &) – the function overloads << operator, which allows a Student object information to be output in the following format:
Underwood Scott:
Assignment 1 8
Assignment 2 16
Lab Test 0
Exam 34
Grade P

(6) bool operator<(const Student &student)const – the function overloads < operator, which allows to compare student’s full names lexicographically.

Collapse | Copy Code
The following describes how to calculate the marks and grade:
Lab Test Mark – is an integer from 0 to 10;
Assignment1 Mark – is an integer from 0 to 20;
Assignment2 Mark – is an integer from 0 to 20;
Final exam Mark – is an integer from 0 to 50;

A student passes the course if and only if
(Assignment1 Mark + Assignment2 Mark) >= 20 and (Lab Test Mark + Final Exam Mark) >= 30

Collapse | Copy Code
There are 5 grades, which are calculated according to the following rules:
F – if the Total Mark is less then 40;
MF – if (40 <= Total Mark < 50) or (Total Mark >= 50 but student didn’t pass);
P – if (50 <= Total Mark < 60) and student passed;
C – if (60 <= Total Mark < 70) and student passed;
D – if (70 <= Total Mark < 80) and student passed;
HD – if Total Mark >= 80;

推荐答案

我不喜欢做其他'的功课但仍然......因为我在上学的时候回到学习C ++的时候遇到了这样的问题所以...



当你实施运营商做一个简单的方法

I don''t like doing other''s homework but still... Since I had such problems while I was learning C++ back in school days so...

Implement the operator as you do to a simple method
//code
bool operator<(const Student &student)
{
    bool ret = false;
    //do check whether greater or less and store it in ret
    return ret;
}


这篇关于具有运算符重载的学生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆