为什么函数返回垃圾值?

时间:2014-04-22 05:16:39

标签: c++ function class inheritance return

这是我的完整计划:

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

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

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

2 个答案:

答案 0 :(得分: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
}

现在创建一个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的数据,反之亦然。

希望能让你有点清楚。

答案 1 :(得分:0)

在我的计划中,实际问题是创建curve1Obj Curve1个实例会导致创建另一组iSizefAverage,默认情况下为垃圾值。 以前iSizefAverage的值由scoreObj定义,curve1Obj无法访问。所以我发现不需要scoreObj,因为curve1Obj可以访问所有内容。感谢您的贡献。