朋友功能奇怪的行为

时间:2014-07-26 14:29:36

标签: c++ oop friend

我自己学习C ++并且我制作的其中一个程序是学校信息系统,它不是我的第一个OOP程序,但这次我遇到了一些朋友功能的奇怪行为。 我有一个派生类Student,它有一个私有成员 - Courses个对象的静态数组,最后是主容器类Class,它有一个成员 - 动态数组Students对象。

正如您所看到的avg student成员是使用Courses类的朋友函数计算的calc_avg收到Courses数组的总和&# 39;分数和按课程数划分),它就像一个魅力。 一旦我尝试计算class_avg对象的平均成绩(Class),问题便开始了 - 我使用相同的逻辑 - 创建Student的朋友函数(class_avarage将有权访问每个学生平均成绩Student对象及其大小,添加每个学生的平均成绩并除以数组大小 - 由于某种原因它接收并返回垃圾值,I& #39;我一直试图修复它3个小时而且我找不到问题,如果你能帮助我 - 这里是相关代码(不包括独立派生类,输出函数最小化):

main //仅创建容器对象和IO函数(包括所有.h个文件)

Class test1;
test1.input_class();
test1.output_class();

Student.h

#include "Person.h"
#include "Courses.h"
#ifndef STUDENT_H
#define STUDENT_H

class Student : public Person
{
public:
    Student();
    void input_student ();
    void output_student ();
    friend float class_avarage (Student* array, int);
    ~Student();
protected:
private:
    Courses my_courses [6];
    float avg;
};

#endif // STUDENT_H

Student.cpp

#include "Student.h"

Student::Student()
{
//ctor
}
void Student::input_student ()
{
cout << "Please enter students info:" << endl;
input_info();
my_courses[0].input_grades ("Math");
my_courses[1].input_grades ("English");
my_courses[2].input_grades ("Science");
my_courses[3].input_grades ("History");
my_courses[4].input_grades ("Art");
my_courses[5].input_grades ("Sports");
}

void Student::output_student ()
{
int i;
cout << "Students info:" << endl;
output_info();
cout << "   Course  Grade" << endl;
for (i = 0; i < 6; i++)
{
    my_courses[i].output_grades ();
}
avg = calc_avg(my_courses, 6);
cout << "   " << "Average grade for student: " << avg << endl;
}
Student::~Student()
{
//dtor
}
float calc_avg (Courses* array, int size)
{
int i;
float sum = 0;
for (i = 0; i < size; i++)
{
    sum += array[i].grade;
}
return sum/size;
}

Courses.h

#include <iostream>
#include <iomanip>
#include <string>
#ifndef COURSES_H
#define COURSES_H

using std::cout;
using std::cin;
using std::endl;
using std::string;

class Courses
{
public:
    Courses();
    void input_grades(string);
    void output_grades();
    friend float calc_avg (Courses*, int);
    ~Courses();
protected:
private:
    string course_name;
    int grade;
};

#endif // COURSES_H

Courses.cpp

#include "Courses.h"

Courses::Courses() : course_name ("Default"), grade (-1)
{
//ctor
}
void Courses::input_grades (string temp_name)
{
course_name = temp_name;
cout << "Enter " << temp_name << " grade: ";
cin >> grade;
cin.get();
}
void Courses::output_grades ()
{
cout << "   " << course_name << ":    " << grade << endl;
}
Courses::~Courses()
{
//dtor
}

Class.h

#include <iostream>
#include <string>
#include "Teacher.h"
#include "Student.h"
#ifndef CLASS_H
#define CLASS_H

using std::cout;
using std::cin;
using std::endl;
using std::string;

class Class
{
public:
    Class();
    void input_class ();
    void output_class ();
    ~Class();
protected:
private:
    string class_name;
    Teacher class_teacher;
    Student* pupils;
    int students_number;
    float class_avg;
};

#endif // CLASS_H

Class.cpp

#include "Class.h"

Class::Class() : class_name ("A"), pupils (NULL), students_number (0), class_avg (0)
{
//ctor
}
void Class::input_class ()
{
int i;
//input class data
cin >> students_number;
cin.get();
pupils = new (std::nothrow) Student [students_number];
if (!pupils)
{
    cout << "Allocation failed!" << endl;
}
else
{
    for (i = 0; i < students_number; i++)
    {
        pupils[i].input_student();
    }
}
class_avg = class_avarage (pupils, students_number);
}
void Class::output_class ()
{
int i;
cout << "   Displaying info of class :" << class_name << endl <<
    "   Class average grade: " << class_avg << endl;
class_teacher.output_teacher();
cout << "   Number of students: " << students_number << endl << "   Students: " << endl;
for (i = 0; i < students_number; i++)
{
    pupils[i].output_student();
}

}
Class::~Class()
{
//dtor
}
float class_avarage (Student* array, int size)
{
cout << size << endl;
int i;
float total = 0;
for (i = 0; i < size; i++)
{
    total += array[i].avg;
}
return total/size;
}

1 个答案:

答案 0 :(得分:-1)

为什么有人想要使用好友功能?只需为Cource.grade成员添加简单的getter,为Student.avg添加lasy getter。这将消除获取垃圾而不是数据的可能性。 另外,使用std :: vector并将它们传递给std :: vector&amp;,而不是数组和指针。