C++ 为什么函数返回垃圾值?
以下是我的完整计划: 得分.hC++ 为什么函数返回垃圾值?,c++,function,class,inheritance,return,C++,Function,Class,Inheritance,Return,以下是我的完整计划: 得分.h #ifndef SCORE_H #define SCORE_H class Score { private: int* ipScore; float fAverage; int iSize; public: Score(); void enterScores(); void calcAverage(); void output(); void setSize(); int ge
#ifndef SCORE_H
#define SCORE_H
class Score
{
private:
int* ipScore;
float fAverage;
int iSize;
public:
Score();
void enterScores();
void calcAverage();
void output();
void setSize();
int getSize();
void setScore();
int* getScore();
float getAverage();
};
#endif
#ifndef CURVE1_H
#define CURVE1_H
#include "score.h"
class Curve1: public Score
{
public:
Curve1();
void curve();
};
#endif
得分.cpp
#include <iostream>
#include "score.h"
using namespace std;
Score::Score()
{
}
void Score::enterScores()
{
cout << "How many test scores needed: ";
setSize();
cout << endl;
setScore();
for (int i = 0; i < getSize(); i++)
{
cout << "Enter score " << i + 1 << ": ";
cin >> ipScore[i];
}
cout << endl;
}
void Score::calcAverage()
{
fAverage = 0;
for (int i = 0; i < getSize(); i++)
{
fAverage = fAverage + ipScore[i];
}
fAverage = fAverage / getSize();
}
void Score::output()
{
int temp;
for (int i = 0; i < getSize(); i++)
{
for (int j = 0; j < (getSize() - 1); j++)
{
if (ipScore[j] > ipScore[j + 1])
{
temp = ipScore[j];
ipScore[j] = ipScore[j + 1];
ipScore[j + 1] = temp;
}
}
}
cout << "Sorted list of data entered is:- " << endl;
for (i = 0; i < getSize(); i++)
{
cout << "Score " << i + 1 << ": " << ipScore[i] << endl;
}
cout << endl;
cout << "The average is: " << fAverage << endl;
cout << endl;
}
void Score::setSize()
{
cin >> iSize;
}
int Score::getSize()
{
return iSize;
}
void Score::setScore()
{
ipScore = new int[getSize()];
}
int* Score::getScore()
{
return ipScore;
}
float Score::getAverage()
{
return fAverage;
}
#include <iostream>
#include "curve1.h"
using namespace std;
Curve1::Curve1(): Score()
{
cout << "Size was: " << getSize() << endl;
}
void Curve1::curve()
{
cout << "Average score was: " << getAverage() << endl;
}
#include <iostream>
#include "curve1.h"
using namespace std;
int main()
{
Score scoreObj;
Curve1 curve1Obj;
scoreObj.enterScores();
scoreObj.calcAverage();
scoreObj.output();
curve1Obj.curve();
return 0;
}
曲线1.cpp
#include <iostream>
#include "score.h"
using namespace std;
Score::Score()
{
}
void Score::enterScores()
{
cout << "How many test scores needed: ";
setSize();
cout << endl;
setScore();
for (int i = 0; i < getSize(); i++)
{
cout << "Enter score " << i + 1 << ": ";
cin >> ipScore[i];
}
cout << endl;
}
void Score::calcAverage()
{
fAverage = 0;
for (int i = 0; i < getSize(); i++)
{
fAverage = fAverage + ipScore[i];
}
fAverage = fAverage / getSize();
}
void Score::output()
{
int temp;
for (int i = 0; i < getSize(); i++)
{
for (int j = 0; j < (getSize() - 1); j++)
{
if (ipScore[j] > ipScore[j + 1])
{
temp = ipScore[j];
ipScore[j] = ipScore[j + 1];
ipScore[j + 1] = temp;
}
}
}
cout << "Sorted list of data entered is:- " << endl;
for (i = 0; i < getSize(); i++)
{
cout << "Score " << i + 1 << ": " << ipScore[i] << endl;
}
cout << endl;
cout << "The average is: " << fAverage << endl;
cout << endl;
}
void Score::setSize()
{
cin >> iSize;
}
int Score::getSize()
{
return iSize;
}
void Score::setScore()
{
ipScore = new int[getSize()];
}
int* Score::getScore()
{
return ipScore;
}
float Score::getAverage()
{
return fAverage;
}
#include <iostream>
#include "curve1.h"
using namespace std;
Curve1::Curve1(): Score()
{
cout << "Size was: " << getSize() << endl;
}
void Curve1::curve()
{
cout << "Average score was: " << getAverage() << endl;
}
#include <iostream>
#include "curve1.h"
using namespace std;
int main()
{
Score scoreObj;
Curve1 curve1Obj;
scoreObj.enterScores();
scoreObj.calcAverage();
scoreObj.output();
curve1Obj.curve();
return 0;
}
#包括
#包括“曲线1.h”
使用名称空间std;
曲线1::曲线1():分数()
{
你似乎把类和类的实例搞混了
说你有
// This just defines a class. It does not create any instances of the class.
struct A
{
A() : aVal(0) {}
int aVal;
};
void foo()
{
A a1; // Create instance of the class
A a2; // Create another instance of the class.
std::cout << a1.aVal << std::endl; // 0
std::cout << a2.aVal << std::endl; // 0
// Change the value of one instance.
// It does not change the value of the other instance.
a1.aVal = 10;
std::cout << a1.aVal << std::endl; // 10
std::cout << a2.aVal << std::endl; // still 0
// Now change the value of the second instance.
// Value of the first instance remains unchanged.
a2.aVal = 20;
std::cout << a1.aVal << std::endl; // Still 10
std::cout << a2.aVal << std::endl; // 20
}
在代码中创建Score
、scoreObj
的实例和Curve1
、curve1Obj
的实例。scoreObj
和curve1Obj
的数据是独立的。修改scoreObj
的数据不会更改curve1Obj
的数据,反之亦然
希望这能让您明白一点。在我的程序中,实际的问题是创建Curve1
实例的curve1Obj
将导致创建另一组带有默认值的iSize
和fAverage
,即垃圾值。
以前的iSize
和fAverage
的值是由scoreObj
定义的,不能通过curve1Obj
访问。所以我发现没有必要使用scoreObj
,因为任何东西都可以通过curve1Obj
访问。感谢你们的贡献。它们是两个不同的对象?是的Curve1对象是curve1Obj
,Score对象是scoreObj
。因为它们是继承的……也许??这并不意味着Curve1
类型的对象知道你对Score
类型的对象所做的任何事情。只有Curve1
从Score继承某些函数和变量ode>。我不知道我是否错了,但是fAverage
和iSize
中的值不是已经设置好了吗?Curve1只是调用getAverage
和getSize
来输出已经设置好的值吗?对象在其中扮演什么角色?对我来说,该对象只是调用curve
函数,调用egetSize
和getAverage
。如果我错了,请告诉我。@R Sahu谢谢你的帮助。我知道我做错了什么。@SilverFalcon我很高兴我能帮上忙。