关于构造函数和继承的解释
Explanation about Constructors and Inheritence
最近,我开始使用 classes,今天开始使用 class 继承。我创建了一个简单的程序来扩展我对继承的理解。该程序计算 class 的平均成绩。我理解我编写的绝大多数代码,但也有一些例外(列在代码下方)。我们将不胜感激。
代码
#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);
调用构造函数?或者,他们自己就是构造函数?
还有,我的所有评论都正确吗?
I think that this
CAverage(int);
and this
CGroup(int, int);
call the constructors? Or, are they themselves the constructors?
你的第二个假设是正确的,都是构造函数。
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
。否则,您的评论是正确的。
最近,我开始使用 classes,今天开始使用 class 继承。我创建了一个简单的程序来扩展我对继承的理解。该程序计算 class 的平均成绩。我理解我编写的绝大多数代码,但也有一些例外(列在代码下方)。我们将不胜感激。
代码
#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);
调用构造函数?或者,他们自己就是构造函数?
还有,我的所有评论都正确吗?
I think that this
CAverage(int);
and this
CGroup(int, int);
call the constructors? Or, are they themselves the constructors?
你的第二个假设是正确的,都是构造函数。
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
。否则,您的评论是正确的。