I am not sure if I am doing this assignment correctly, here is what it asks and the code that I have. I am not sure how to do the main to meet the assignments requirements. Any suggestions/advice are appreciated.

a. Create a function template called average(). It will accept two arguments of the same type and computes the arithmetic average, returning a double.
Overload the average() function to work correctly with three arguments.

b. Create a class called CollegeCourse with fields for:
the course ID (for example, "INF 114''),
your grade (for example, 'A'),
and the credits earned for the CollegeCourse (for example, 2).
The CollegeCourse constructor accepts values for these fields as arguments.
Calculate a fourth field named honorPoints. This field is the product of the grade points (4 for an A, 3 for a B, 2 for a C, and 1 for a D) and the credits.

c. 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 <<operator() to display the details of a CollegeCourse.

d. Write a main() program that declares several integers, doubles, and CollegeCourse objects.
Instantiate your CollegeCourse objects by creating an array of objects.
Show your CollegeCourses using the +operator() and /operator() to compute the average honorPoints for the CollegeCourses.
Demonstrate that both versions of average() work correctly with integers, doubles, and CollegeCourse objects.
Your input might look like this:
int a=7, b = 26, c = 100;
double d = 39.25, e = 2.01,f = 4.2;
double avg;
[You will also have three CollegeCourse objects containing information like:
"ENG101", 'A', 3
"PSY251", 'B', 3
"HIS301", 'D', 4
(filled via your array of objects).]
and your output would look like this:
The average of 7 and 26 is 16
The average of 39.25 and 2.01 is 20.63

The average of
ENG101 Grade: A Credits: 3 Honor points: 12
PSY251 Grade: B Credits: 3 Honor points: 9
is 10.5

The average of 7, 26 and 100 is 44
The average of 39.25, 2.01 and 4.2 is 15.1533

The average of
ENG101 Grade: A Credits: 3 Honor points: 12
PSY251 Grade: B Credits: 3 Honor points: 9
HIS301 Grade: D Credits: 4 Honor points: 4
is 8.33333

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

template <typename T>
T average(T a, T b, T c)
{
 return (a + b + c) / 3;
}

int main()
{
 int a=7, b = 26, c = 100;
 double d = 39.25, e = 2.01, f = 4.2;  
 double avg;
 int k=average<int>(a,b,c);  
 double n=average<double>(d,e,f); 

 cout << "The average of " << a << ", " << b << ", " << c << " is: " << k << '\n';
 cout << "The average of " << d << ", " << e << ", " << f << " is: " <<n << '\n';
 return 0;
}

__________________collegeCourse.h______________________

#include <string>
#include <iostream>
using namespace std;

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

collegeCourse::collegeCourse(const std::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;
}


std::ostream& operator<<(std::ostream& out, const collegeCourse& course) // no ';' here
{

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

  return out; 
}


collegeCourse operator+(const collegeCourse& l, const collegeCourse& r)
    {
        int honorPoints;  
        collegeCourse hnrPoints;

        return hnrPoints;
    }


collegeCourse operator/(const collegeCourse& l, const collegeCourse& r)
    {
        int honorPoints;  
       collegeCourse hnrPoints;

       return hnrPoints;
    }

Recommended Answers

All 2 Replies

Please use [[I][/I]co[I][/I]de[I][/I]] tags.

average
Your average template function itself is fine, but you did not overload it. You need to write another average function that takes only two arguments.

You don't typically need to specify the type when you call a templated function, unless you have to. For example: cout << average( 12, 14, 15 ) << endl; produces 13, since the arguments are taken as ints. You can force double by cout << average<double>( 12, 14, 15 ) << endl; and get 13.6667.

college course
Looks pretty good. One error and some suggestions:

  1. Your << operator writes to cout instead of out.
  2. Your + operator doesn't add l and r's honor points. You should do that, then set the new hnrPoints object's honor points to the sum before returning it.
  3. Your / operator should have the prototype: friend double operator / ( collegeCourse &cc, int divisor );

Hope this helps.

> Create a function template called average(). It will accept two arguments of the same type and
> computes the arithmetic average, returning a double.
returning a double?
a. what should be the average of two complex numbers? a double or a complex?
b. what should be the average of two integers? an int or a double?
there are solutions in which a. returns a complex, but b. returns a double. but average is perhaps not the ideal function to write for someone just starting to learn templates.

template <typename T> T average( T a, T b, T c )
{ return (a + b + c) / 3; }

it would be better to pass the parameters by const reference; T may not be inexpensive to copy and destroy

template <typename T> T average( const T& a, const T& b, const T& c )
{ return ( a + b + c ) / 3 ; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.