gpt4 book ai didi

c++ - 为什么函数返回垃圾值?

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:23 25 4
gpt4 key购买 nike

这是我的完整程序:

分数.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 getSize();

void setScore();
int* getScore();

float getAverage();
};

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

曲线1.h

#ifndef CURVE1_H
#define CURVE1_H

#include "score.h"

class Curve1: public Score
{

public:

Curve1();

void curve();

};

#endif

curve1.cpp

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

main.cpp

#include <iostream>

#include "curve1.h"

using namespace std;

int main()
{
Score scoreObj;
Curve1 curve1Obj;

scoreObj.enterScores();

scoreObj.calcAverage();

scoreObj.output();

curve1Obj.curve();

return 0;
}

首先,当我在 score.cpp 中输出 iSizefAverage 时,它们显示了正确的值。但是当我在 curve1.cpp 中输出它们时,它们显示为垃圾。 :( 为什么会这样?此外,当在 main 函数中调用曲线对象时,不会显示 size cout 语句。请帮忙!!!

最佳答案

您似乎对类和类的实例感到困惑。

假设你有,

// 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
}

现在创建一个 B 类,它是 A 的子类。

struct B : public A
{
B() : A(), bVal(0) {}
int bVal;
};

void bar()
{
// Create an instance of A and in instance of B
A a1;
B b1;

std::cout << a1.aVal << std::endl; // 0
std::cout << b1.aVal << std::endl; // 0
std::cout << b1.bVal << std::endl; // 0

// Change the value of a1. Values of b1 remain unchanged.
a1.aVal = 20;
std::cout << a1.aVal << std::endl; // 20
std::cout << b1.aVal << std::endl; // Still 0
std::cout << b1.bVal << std::endl; // Still 0

// Change the values of b1. Value of a1 remain unchanged.
b1.aVal = 30;
b1.bVal = 40;

std::cout << a1.aVal << std::endl; // Still 20
std::cout << b1.aVal << std::endl; // 30
std::cout << b1.bVal << std::endl; // 40
}

在您的代码中,您创建了一个 ScorescoreObj 的实例,以及一个 Curve1curve1Obj 的实例. scoreObjcurve1Obj的数据是独立的。修改 scoreObj 的数据不会更改 curve1Obj 的数据,反之亦然。

希望这能让您清楚一些。

关于c++ - 为什么函数返回垃圾值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23210941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com