0

最近,我开始使用类,今天开始使用类继承。我创建了一个简单的程序来扩展我对继承的理解。该程序计算一个班级的平均成绩。我了解我编写的绝大多数代码,但也有一些例外(在代码下方列出)。任何和所有的帮助将不胜感激。

代码

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

using namespace std;

class CAverage {
    private:
        double VSubCount, VAverage, VMark, VSum, VNum;
    public: CAverage(int); // Constructor.
        void MTake_action() {
            MAsk_input(); // Calls the method “MAsk_input()“ within this class.
            MCalculate_average(); // Calls the method “MCalculate_average()“ within 
                                  // this class.
            MPrint_result(); // Calls the method “MPrint_result()“ within this class.
        }

        void MCalculate_average() {
            VAverage = VSum / VNum;
        }

        void MAsk_input() {
            VSum = 0;
            VNum = 0;
            int VNumber;
            for (int i = 0; i < VSubCount; i++) {
                cout << "Enter your " << i + 1 << " mark: ";
                cin >> VNumber;
                if (VNumber > 0) {
                    VMark = VNumber;
                    VSum += VMark;
                    VNum++;
                }
            }
        }

        void MPrint_result()
        {
            system("cls");

            if (((VSum / 3) <= 0) || ((VSum / 3) > 10)) {
                cout << "Incorrect input." << endl;
            } else {
                cout << "Average: " << VAverage << endl;
            }
        }
};

// Creates a child class and makes that this class could view/get public methods,
// variables, etc of “CAverage“.
class CGroup : public CAverage {
    private:
        int VClassMembers;
        void MAsk_input() {
            for (int i = 0; i < VClassMembers; i++) {
                system("cls");

                cout << "[" << i + 1 << " student]" << endl;

                CAverage::MAsk_input(); // Calls the method “MAsk_input()“ within
                                        // the parent class (“CAverage“).
            }
        }
    public: CGroup(int, int);
        void MTake_action() {
            MAsk_input(); // Calls the method “MAsk_input()“ within this class.
            CAverage::MCalculate_average(); // Calls the method “MCalculate_average()“
                                            // within the parent class (“CAverage“).
            CAverage::MPrint_result(); // Calls the method “MPrint_result()“ within the
                                       // parent class (“CAverage“).
        }
};

CAverage::CAverage(int VSubjectCount) {
    VSubCount = VSubjectCount;
}

CGroup::CGroup(int VOther, int VInteger) : CAverage(VOther) {
    VClassMembers = VInteger;
}

int main() {
    CGroup avg(2, 5); // Creates an object, named “avg“.

    avg.MTake_action(); // Calls the child classes' method “MTake_action()“.

    return 0;
}

那么,如何解释这些部分呢?

CAverage::CAverage(int VSubjectCount) {
    VSubCount = VSubjectCount;
}

CGroup::CGroup(int VOther, int VInteger) : CAverage(VOther) {
    VClassMembers = VInteger;
}

我认为这

CAverage(int);

还有这个

CGroup(int, int);

调用构造函数?或者,他们自己是构造函数吗?

而且,我发表的所有评论都是正确的吗?

4

1 回答 1

0

我认为这

CAverage(int);

还有这个

CGroup(int, int);

调用构造函数?或者,他们自己是构造函数吗?

您的第二个假设是正确的,两者都是构造函数。

CAverage::CAverage(int VSubjectCount) {
    VSubCount = VSubjectCount;
}

此代码段初始化VSubCount超类中的变量。

CGroup::CGroup(int VOther, int VInteger) : CAverage(VOther) {
    VClassMembers = VInteger;
}

这有点复杂,并显示了继承的关键概念。

: CAverage(VOther)

正在调用父构造函数,以初始化 CAverage 中的私有成员VSubCount,因为 CGroup 无法访问它。

VClassMembers = VInteger;

初始化VClassMembers子类中的成员。否则,您的评论是正确的。

于 2016-10-20T17:49:35.770 回答