5 Replies - 660 Views - Last Post: 16 March 2009 - 04:15 PM Rate Topic: -----

#1 erin0201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-December 08

Class object array with data passed to class with overloaded operators

Post icon  Posted 16 March 2009 - 01:38 PM

I don't have my code right now as I am not at home, but I was just wondering if anyone could post an example of a short simple program with a class where you store data for your class Object in an array and then overload the + and / operators to add and divide information from that array.

I can create and fill an array of a class object inside of the main program, but I can't figure out how to work with the array data in the class and in the overloaded operators + and /. Also, the / operator has to be divided by an integer rather than another part of the array. Its calculating the average of the totaled numbers in the array.

Any example that specifically uses an array with overloaded operators would be wonderful so that I can see a good example of what works and apply it to my program and see where I'm going wrong. I can't seem to find any examples with both arrays and overloaded operators used in the same program/class.

Thanks for your help!
Erin0201

Is This A Good Question/Topic? 0
  • +

Replies To: Class object array with data passed to class with overloaded operators

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Class object array with data passed to class with overloaded operators

Posted 16 March 2009 - 01:43 PM

An example.

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 erin0201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-December 08

Re: Class object array with data passed to class with overloaded operators

Posted 16 March 2009 - 01:54 PM

I definitely understand the policy and I will post the code when I get home. I'm really not expecting someone to write my code for me at all.

I just need an example to look at so that I can see how an operator should be overloaded for use with a class object filled by an array. If you can't help until I post the code, I understand, but if you can think of any examples out there that specifically deal with a class with an array for the data and overloaded operators adding and dividing that data, that would be great if you could point me to them.

My book's examples don't include classes with arrays and classes with overloaded operators in the same example and I struggled for 15+ hours to even get it working without the operators.

Again, I do understand the policy with posting code. I'm not asking for someone to do my homework for me at all. I don't even like admitting that I can't figure this out on my own. ;)
I just need an example to compare to what I have or some advice on it. I will definitely post my program when I get home, though, and hopefully I'm just missing something simple. I'm not getting any errors right now because I made the program work without the operators being utilized for the time being.

Thanks!
Erin0201
Was This Post Helpful? 0
  • +
  • -

#4 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Class object array with data passed to class with overloaded operators

Posted 16 March 2009 - 02:02 PM

If you followed the link, you'd see an example of operator overloading.
The whole "policy" is just a macro (rules in brackets like anything else).

Didn't think you were being honest. You and I are in the same boat (in a sense). :D
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Class object array with data passed to class with overloaded operators

Posted 16 March 2009 - 03:18 PM

Which operator are you looking for? Also, do you mean an array as a private class member, like:

class Cat {
private:
   int paws[4]; //or something similar?
};


Was This Post Helpful? 0
  • +
  • -

#6 erin0201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-December 08

Re: Class object array with data passed to class with overloaded operators

Posted 16 March 2009 - 04:15 PM

View PostKYA, on 16 Mar, 2009 - 02:18 PM, said:

Which operator are you looking for? Also, do you mean an array as a private class member, like:

class Cat {
private:
   int paws[4]; //or something similar?
};



Kya: Thanks for the reply!
I'm not sure whether the array needs to be in the main program or inside of the class as you have it. I just need to be able to overload the operators +, /, and << to add the items in the array as a sum and then to divide by the number of items in the array, and to output the results, all using the operators rather than the methods I used in the main program.

Here is the code that I have which outputs what the instructor is asking for. I also have a function template of average that I was able to use to produce the results too. However, I don't think we're supposed to use that template since we're supposed to be achieving the results by overloading the operators.

Hyper: Thanks for that link. Unfortunately, no matter how many overloaded operator examples I get, I can't seem to find one that deals with arrays and my brain just isn't clicking around that concept. :)

Thanks for any advice on this. :) The project is due tonight and I'm probably going to turn it in as is, but I would still like to solve the problem and get it working even if its after the due date so that I can see what I needed to do and what I was missing. :)

#include <iostream>
#include "college2.h"

using namespace std;

template <class Type>
Type average(Type x, Type y);

template <class Type>
Type average(Type x, Type y, Type z);

int main()
	{
	 collegeCourse strCourseIDList[4];
	 collegeCourse charGradeList[4];
	 collegeCourse dblCreditsList[4];
	 collegeCourse dblHonorPointsList[4];
	
	 int i;
	   
		strCourseIDList[0].setCourse("ENG101");
		strCourseIDList[1].setCourse("PSY251");
		strCourseIDList[2].setCourse("HIS301");

		charGradeList[0].setGrade('A');
		charGradeList[1].setGrade('B');
		charGradeList[2].setGrade('D');

		dblCreditsList[0].setCredits(3);
		dblCreditsList[1].setCredits(3);
		dblCreditsList[2].setCredits(4);	
		
		dblHonorPointsList[0].setHonorPoints(4 * 3);
		dblHonorPointsList[1].setHonorPoints(3 * 3);
		dblHonorPointsList[2].setHonorPoints(1 * 4);

		double sum1, sum2, sum3, avg, avg2;
		sum1 = dblHonorPointsList[0].getHonorPoints();
		sum2 = dblHonorPointsList[1].getHonorPoints();
		sum3 = dblHonorPointsList[2].getHonorPoints();

		avg = dblHonorPointsList[0].getHonorPoints() + dblHonorPointsList[1].getHonorPoints();
		dblHonorPointsList[3].setHonorPoints(avg);
		avg = dblHonorPointsList[3].getHonorPoints() / 2;

		avg2 = dblHonorPointsList[0].getHonorPoints() + dblHonorPointsList[1].getHonorPoints() + dblHonorPointsList[2].getHonorPoints();
		dblHonorPointsList[3].setHonorPoints(avg2);
		avg2 = dblHonorPointsList[3].getHonorPoints() / 3;
			  

	int a = 7;
	int b = 26;
	int c = 100;
	double d = 39.25;
	double e = 2.01;
	double f = 4.2;

	cout << "The average of " << a << " and " << b << " is "
		<< average<double>(a, b) << endl;

	cout << "The average of " << d << " and " << e << " is "
		<< average<double>(d, e) << endl;
	cout << endl;

	cout << "The average of " << endl;

	for (i = 0; i < 2; i++)
		cout	<< strCourseIDList[i].getCourse() << " Grade: " 
				<< charGradeList[i].getGrade() << " Credits: " 
				<< dblCreditsList[i].getCredits() << " Honor Points: "
				<< dblHonorPointsList[i].getHonorPoints() << endl;
	cout << "is " << avg << endl;
	cout << endl;
	
	cout << "The average of " << a << ", " << b << ", " << c << " is "
		<< average<double>(a, b, c) << endl;

	cout << "The average of " << d << ", " << e << ", " << f << " is "
		<< average<double>(d, e, f) << endl;
	cout << endl;

	for (i = 0; i < 3; i++)
		 cout << strCourseIDList[i].getCourse() << " Grade: " 
				<< charGradeList[i].getGrade() << " Credits: " 
				<< dblCreditsList[i].getCredits() << " Honor Points: "
				<< dblHonorPointsList[i].getHonorPoints() << endl;
	cout << "is " << avg2 << endl;

	return 0;		
	}


template <class Type>		
Type average(Type x, Type y)
	{
	Type avg = 0;
	avg = x + y;
	avg = avg / 2;

	return avg;
	}

template <class Type>		
Type average(Type x, Type y, Type z)
	{
	Type avg = 0;
	avg = x + y + z;
	avg = avg / 3;

	return avg;
	}		 
------ Class Header File-----
#include <iostream>
#include <string>

using namespace std;

class collegeCourse
	{	
	friend ostream& operator<<(ostream&, const collegeCourse &course);
	//Overload the << operator
	
	
	public:
	void setCourse(string cCourseID);
		//Function to set the courseID
	string getCourse();
		//Function to return items of the courseID array
	void setGrade(char cGrade);
		//Function to set the grade
	char getGrade();
		//Function to return items of the grade array	
	void setCredits(double cCredits);
		//Function to set the credits
	double getCredits();
		//Function to return the credits
	void setHonorPoints(double hPoints);
		//Function to set the honor points
	double getHonorPoints();
		//Function to return the honor points

	collegeCourse();
		//Constructor without parameters;
	
	
	collegeCourse operator+(collegeCourse course);
	//Overload the operator +
	collegeCourse operator/(collegeCourse course);
	//Overload the operator /
	

	private:
	
		string courseID; //variable to store the courseID
		char grade; //variable to store the grade
		double credits; //variable to store the credits
		double honorPoints; //variable to store the product of grade points and credits
	};


	collegeCourse::collegeCourse()
		{
		  courseID = " ";
		  grade = 'F';
		  credits = 0;
		  honorPoints = 0;
		}

	void collegeCourse::setCourse(string cCourseID)
		{
		courseID = cCourseID;
		}
	string collegeCourse::getCourse()
		{
		return courseID;
		}

	void collegeCourse::setGrade(char cGrade)
		{
		grade = cGrade;
		}
	char collegeCourse::getGrade()
		{
		return grade;
		}

	void collegeCourse::setCredits(double cCredits)
		{
		credits = cCredits;
		}
	double collegeCourse::getCredits()
		{
		return credits;
		}

	   void collegeCourse::setHonorPoints(double hPoints)
		{			
		honorPoints = hPoints;
		}
	double collegeCourse::getHonorPoints()
		{
		return honorPoints;
		}


	collegeCourse collegeCourse::operator+(collegeCourse course)
		{
		collegeCourse tempCourse;
		tempCourse.honorPoints = honorPoints + course.honorPoints;

		return (tempCourse);
		}
	
	collegeCourse collegeCourse::operator/(collegeCourse course)
		{
		collegeCourse tempCourse;
		tempCourse.honorPoints = honorPoints / course.honorPoints;

		return (tempCourse);
		}

	ostream& operator<<(ostream& out, const collegeCourse& course)
		{
		out << course.courseID << " " << course.grade << " " << course.credits << " " <<  course.honorPoints << endl;
			return (out);
		}		   
			 
			 
  

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1