【问题标题】:Why the function is returning garbage value?为什么函数返回垃圾值?
【发布时间】:2014-06-06 07:36:47
【问题描述】:

这是我的完整程序:

score.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

score.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;
}

curve1.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;  
}

ma​​in.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 语句。请帮忙!!!

【问题讨论】:

  • 它们是两个不同的对象?
  • 是的,Curve1 对象是curve1Obj,Score 对象是scoreObj
  • 因为它们是遗传的……也许吧??
  • 这并不意味着Curve1 类型的对象知道您对Score 类型的对象所做的任何事情。只有Curve1 继承了Score 的某些函数和变量。
  • 我不知道我是不是错了但是fAverageiSize中的值不是已经设置了吗?并且 Curve1 只是调用 getAveragegetSize 来输出已经设置的值?对象在其中的作用是什么?对我来说,该对象只是调用curve 函数,该函数调用getSizegetAverage。如果我错了,请告诉我。

标签: c++ function class inheritance return


【解决方案1】:

在我的程序中,实际的问题是创建 Curve1 实例的 curve1Obj 会导致创建另一组 iSizefAverage 并使用默认值,即 garbage 值。 iSizefAverage 的先前值由 scoreObj 定义,curve1Obj 无法访问。所以我发现不需要scoreObj,因为curve1Obj 可以访问所有内容。感谢你们的贡献。

【讨论】:

    【解决方案2】:

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

    说你有,

    // 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的数据,反之亦然。

    希望这能让你明白一点。

    【讨论】:

    • @R Sahu 谢谢你的帮助。我知道我做错了什么。 :)
    • @SilverFalcon 我很高兴能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2023-04-01
    • 1970-01-01
    • 2023-02-06
    相关资源
    最近更新 更多