簡體   English   中英

為什么函數返回垃圾值?

[英]Why the function is returning garbage value?

這是我的完整程序:

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

首先,當我在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
}

在您的代碼中,您將創建一個Score實例, scoreObj和一個Curve1實例curve1Obj scoreObjcurve1Obj的數據是獨立的。 修改scoreObj的數據不會更改curve1Obj的數據,反之亦然。

希望這對您有所幫助。

在我的程序中,實際的問題是創建curve1Obj實例的Curve1會導致創建另一組具有默認值(即垃圾值)的iSizefAverage 以前的值iSizefAverage被定義scoreObj ,不能被訪問curve1Obj 因此,我發現沒有必要使用scoreObj ,因為scoreObj可以訪問curve1Obj 感謝您的貢獻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM