954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Am I creating/using the objects correctly?

Hello all, I am trying to finish this program, but am running into some trouble with the main() (I think).

It will output the courseID, but no grade, points, honorPoints, or averages

I think that I have everything overloaded correctly, but am messing up with the array of objects part of it, Am I completely off base here?
any advice or suggestions would be great. Here is directions for the part I am messing up.


Overload the +operator() so that honor points for courses can be summed to create a summary CollegeCourse object.
Overload the /operator() so that a CollegeCourse object's honorPoints can be divided by an integer; this function should also return a double.
Overload the <#include <string>
#include <iostream>
using namespace std;

class collegeCourse
{
public:
collegeCourse(const string& id, int credits, char score);
collegeCourse() { honorPoints = 0; creditsEarned = 0; grade = 'F';};
friend ostream& operator<<(ostream& out, const collegeCourse& course);
friend collegeCourse operator+(const collegeCourse& l, const collegeCourse& r);
friend collegeCourse operator/(const collegeCourse& l, const collegeCourse& r);
friend double operator / ( collegeCourse &cc, int divisor );
private:
string courseID;
int honorPoints;
int creditsEarned;
char grade;
};

collegeCourse::collegeCourse(const string& id, int credits, char score)
{
courseID = id;
creditsEarned = credits;
grade = score;


int points;

switch (grade)
{
case 'A': points = 4;
break;
case 'B': points = 3;
break;
case 'C': points = 2;
break;
default: points = 1;
}

honorPoints = points * creditsEarned;
}


ostream& operator<<(ostream& out, const collegeCourse& course)
{

return out << course.courseID << ": " << course.creditsEarned
<< " -- " << course.grade << " -- " << course.honorPoints;

return out;
}



collegeCourse operator+(const collegeCourse& l, const collegeCourse& r)
{
collegeCourse honorPoints;
honorPoints.courseID = l.courseID + r.courseID;


return honorPoints;
}


collegeCourse operator/(const collegeCourse& l, const collegeCourse& r)

{
collegeCourse honorPoints;
honorPoints.creditsEarned = l.creditsEarned/ r.creditsEarned;


return honorPoints;
}


main.cpp

#include <iostream>
#include "CollegeCourse.h"
using namespace std;

int main()
{
    collegeCourse course[3]={collegeCourse("ENG 101", 'A', 3), collegeCourse("PSY251", 'B', 3), collegeCourse("HIS301", 'D', 4)};

    cout << "The average of " << endl;
    cout << course[0] << endl;
    cout << course[1] << endl;
    cout << " is " << endl;    

    cout << "The average of " << endl;
    cout << course[0] << endl;
    cout << course[1] << endl;
    cout << course[2] << endl;
    cout << " is " << endl;
        
return 0;
}
hezfast2
Light Poster
32 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Reread overloading specifications then have a look at your overloaded operators.
Overloaded operator + must return CollegeCurse (what's a strange idea ;)) and overloaded operator / must return double (why "also"?)...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You