In my program, the actual problem was that creating curve1Obj
of Curve1
instance would result in creating another set of iSize
and fAverage
with default i.e. garbage values.
Previous values of iSize
and fAverage
were defined by scoreObj
and couldn't be accessed by curve1Obj
. So I figured out that there is no need for scoreObj
as every thing is accessible with curve1Obj
. Thanks for your contribution guys.
Why the function is returning garbage value?
-
07-07-2023 - |
Вопрос
Here is my full program:
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;
}
Firstly when I output the iSize
and fAverage
in score.cpp
, they show the right values. But when I output them in curve1.cpp
, they show garbage. :( Why is this happening?
Also when the curve object is called in the main
function, the size cout statement is not shown. Please help!!!
Решение 2
Другие советы
You seem to be confused with classes and instances of classes.
Say you have,
// 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
}
Now create a class B
that is a sub-class of 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
}
In your code you create an instance of Score
, scoreObj
, and an instance of Curve1
, curve1Obj
. The data of scoreObj
and curve1Obj
are independent. Modifying the data of scoreObj
does not change the data of curve1Obj
and vice versa.
Hope that makes things a little bit clear for you.