1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trouble with Friend Functions

Discussion in 'C++' started by golf_girl32, Nov 19, 2010.

  1. golf_girl32

    golf_girl32 New Member

    Joined:
    Sep 28, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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
    
    
    }
    
    [FONT=&quot]
    [/FONT]
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
  3. golf_girl32

    golf_girl32 New Member

    Joined:
    Sep 28, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    Is the above code the current code?

    Have you made any changes?

    What questions are unanswered?


    Jim
     
  5. golf_girl32

    golf_girl32 New Member

    Joined:
    Sep 28, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  6. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    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
     
    shabbir likes this.

Share This Page