关于构造函数和继承的解释

时间:2016-10-20 17:42:41

标签: c++ class inheritance constructor class-constructors

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

代码

#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);

打电话给构造函数?或者,他们自己是构造者吗?

并且,我所做的所有评论都是正确的吗?

1 个答案:

答案 0 :(得分: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。否则,您的评论是正确的。