Implement StudentRecord class Private member variables: int CWID, double grade I
ID: 3801534 • Letter: I
Question
Implement StudentRecord class
Private member variables: int CWID, double grade
Implement appropriate constructors and accessor/mutator functions
Overload +, == for StudentRecord class
== compares CWID
+ adds up grades for students
Creates new StudentRecord object
New CWID = old CWID1 + old CWID2
Overload either as member or non-member functions
Now, overload the << and >> operators In your main() function, you can now do
Sample input:
Sample output:
code here
#include <iostream>
using namespace std;
class StudentRecord {
public:
StudentRecord() : CWID(0), grade(0) {
}
StudentRecord(int c, double g) : CWID(c), grade(g) {
}
int getCWID() const {return CWID;}
double getGrade() const {return grade;}
void setCWID(int c) {
CWID = c;
}
void setGrade(double g) {
grade = g;
}
const StudentRecord operator +(const StudentRecord& other);
bool operator ==(const StudentRecord& other);
//declare and overload >> and << oerators
private:
int CWID;
double grade;
};
const StudentRecord StudentRecord::operator +(const StudentRecord& other) {
return StudentRecord(CWID + other.getCWID(), grade + other.getGrade());
}
bool StudentRecord::operator ==(const StudentRecord& other) {
if (CWID == other.getCWID()) {
return true;
}
return false;
}
//define and overload >> and << oerators
int main() {
StudentRecord eric, nick;
cin >> eric >> nick;
cout << eric << nick;
StudentRecord enik = eric + nick;
cout << enik;
}
Explanation / Answer
#include <iostream>
using namespace std;
class StudentRecord {
public:
StudentRecord() : CWID(0), grade(0) {
}
StudentRecord(int c, double g) : CWID(c), grade(g) {
}
int getCWID() const {return CWID;}
double getGrade() const {return grade;}
void setCWID(int c) {
CWID = c;
}
void setGrade(double g) {
grade = g;
}
const StudentRecord operator +(const StudentRecord& other);
bool operator ==(const StudentRecord& other);
friend ostream & operator << (ostream &out, const StudentRecord &c);
friend istream & operator >> (istream &in, StudentRecord &c);
private:
int CWID;
double grade;
};
const StudentRecord StudentRecord::operator +(const StudentRecord& other) {
return StudentRecord(CWID + other.getCWID(), grade + other.getGrade());
}
bool StudentRecord::operator ==(const StudentRecord& other) {
if (CWID == other.getCWID()) {
return true;
}
return false;
}
ostream & operator << (ostream &out, const StudentRecord &c)
{
out <<"CWID : "<<c.CWID<<" , Grade : "<<c.grade<<endl;
return out;
}
istream & operator >> (istream &in, StudentRecord &c)
{
cout << "Enter CWID: ";
in >> c.CWID;
cout << "Enter Grade : ";
in >> c.grade;
return in;
}
int main() {
StudentRecord eric, nick;
cin >> eric >> nick;
cout << eric << nick;
StudentRecord enik = eric + nick;
cout << enik;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.