Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Trouble with Friend Functions (http://www.go4expert.com/forums/trouble-friend-functions-t23919/)

golf_girl32 19Nov2010 10:05

Trouble with Friend Functions
 
I have a program and I need to print the content of the Student objectís private member variable to the ostream variable and return the ostream variable as function return value. I'm confused on how to do this. I already have most of the program done and I have the friend function. Here is my code...


Code:

#include <iostream>

using namespace std;

void part2(); // declaration for the part2() function

class Student
{
        private:

              int KUID; //Stores the student's KUID number
              double HomeworkAvg; //Stores the student's average on Homework
              double QuizAvg; //Stores the student's average on Quizzes
              double ExamAvg; //Stores the student's average on Exams

        public:

              friend ostream output_friend (ostream& outs, Student person1);

              // Constructors
              Student(); //Default Constructor

              //Overloaded constructor that gives the attributes values passed in.
              Student(int k, double h, double q, double e);

              // Accessors (Get Methods)
              // Remember when we write get methods, they always have the same return
              // type as the attribute they are "getting" and never take any parameters.
              int getKUID();
              double getHomeworkAvg();
              double getQuizAvg();
              double getExamAvg();

              // Mutators (Set Methods)
              // When we write set methods, they always have void as their return type. They
              // have one parameter of the same type of the attribute they are "setting."
              void setKUID(int k);
              void setHomeworkAvg(double h);
              void setQuizAvg(double q);
              void setExamAvg(double e);

              // Additional Functions
              void print();
              void average();

              friend ostream &output_friend(ostream &output_friend, Student &student);

}; //Don't forget your semicolon here!

int main()
{
        int labChoice = 0;

        cout << "Lab 10\n";
        do
        {
              cout << "Which part of the lab are you working on?  " << endl
                << "\t1 - Part 2 (Classes)\n\t2 - Quit\n\n"
                << "Please enter your choice: ";
              cin >> labChoice;

              switch(labChoice)
              {
                      case 1:
                              part2();
                              break;
                      case 2:
                              break;
                      default:
                              cout << "Invalid Selection. Try again!\n";
              }
        } while(labChoice != 2);

        return 0;
}

// ***Write all function definitions outside of any function definition***
Student::Student():KUID(0),HomeworkAvg(0.0),QuizAvg(0.0),ExamAvg(0.0)
{

}
Student::Student(int k, double h, double q, double e)
{
  KUID=k;
  HomeworkAvg = h;
  QuizAvg = q;
  ExamAvg=e;
}
int Student::getKUID()
{
    return KUID;
}

double Student::getHomeworkAvg()
{
    return HomeworkAvg;
}

double Student::getQuizAvg()
{
    return QuizAvg;
}

double Student::getExamAvg()
{
    return ExamAvg;
}

void Student::print()
{
    cout<<"KUID:\t\t"<<KUID<<endl;
    cout<<"HomeworkAvg:\t"<<HomeworkAvg<<endl;
    cout<<"QuizAvg:\t"<<QuizAvg<<endl;
    cout<<"ExamAvg:\t"<<ExamAvg<<endl;
}
void Student:: average()
{
    cout<<"Evenly averaged score is:"<<(HomeworkAvg+QuizAvg+ExamAvg)/3<<endl;
}

// *** PART 2 CODE BELOW
void part2()
{
        // Complete the instruction underneath each comment given below:

        // -------------------------------------------------------------

        // Declare an object of the class 'Student'
        Student st1;

        // Use the overloaded constructor to pass in 84551 for the KUID,
        // 78.5 for the HomeworkAvg, 87.25 for QuizAvg, and 77.0 for the ExamAvg.
        st1 = Student(84551,78.5,87.25,77.0);

        // Have your Student object call the functions average and print


}


jimblumberg 19Nov2010 10:32

Re: Trouble with Friend Functions
 
So was your question answered here "Trouble with friend" or do you still need help?

Jim

golf_girl32 19Nov2010 10:46

Re: Trouble with Friend Functions
 
no my question wasn't answered...it helped me to put the friend function in the program but I don't know how to do the other stuff. No one answered my question about the following parts.

jimblumberg 19Nov2010 11:07

Re: Trouble with Friend Functions
 
Is the above code the current code?

Have you made any changes?

What questions are unanswered?


Jim

golf_girl32 19Nov2010 11:12

Re: Trouble with Friend Functions
 
Yes it is my current code. I haven't made any changes. How do I read four numbers (KUID, etc.) from the istream into the four private member variables of the Student object? And how do I return true, if all the 4 private member functions are equal between the two student objects. False, otherwise? One more question is how do I create a new student object in the function body, copy the four member variables of the Student object passed in into the new Student, pick one of the three ‘Avg’ member variables, and add the second parameter to it return the new Student as function return value

jimblumberg 19Nov2010 11:34

Re: Trouble with Friend Functions
 
Quote:

How do I read four numbers (KUID, etc.) from the istream into the four private member variables of the Student object?
You can do this a couple of ways, one way would be to read in the variables into "temporary" variables, then pass these variables to your class constructor using the "Student::Student(int k, double h, double q, double e)" constructor.

In the following code:

Code:

void part2()
{
        // Complete the instruction underneath each comment given below:

        // -------------------------------------------------------------

        // Declare an object of the class 'Student'
        Student st1;

        // Use the overloaded constructor to pass in 84551 for the KUID,
        // 78.5 for the HomeworkAvg, 87.25 for QuizAvg, and 77.0 for the ExamAvg.
        st1 = Student(84551,78.5,87.25,77.0);

        // Have your Student object call the functions average and print


}

You are first calling the constructor with no parameters and trying to create st1. Then you call the constructor with 4 parameters to create st1. First you can only call one constructor for the same variable name (st1).

But probably the best way would be to create the variable using the constructor with no parameters and create a "set" function in your class which has four parameters and not use your constructor with 4 parameters at all.

Code:

Student st1;
st1.setValues(84551,78.5,87.25,77.0); // You will need to define this in your class and write the method.

Also the following item is not correct. For now just comment it out.

Code:

friend ostream output_friend (ostream& outs, Student person1);
Jim


All times are GMT +5.5. The time now is 05:08.