I have my
main.cpp
like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main()
{
Curve1 curve1Obj;
Curve2 curve2Obj;
curve1Obj.enterScores();
curve1Obj.calcAverage();
curve1Obj.output();
curve1Obj.curve();
curve1Obj.output(curve1Obj.new_getAverage1(), curve1Obj.new_getScore1());
curve2Obj.curve();
return 0;
}
|
Base class
Score
has two derived classes
Curve1
and
Curve2
. There are two
curve()
functions, one is in
Curve1
and other in
Curve2
classes.
getSize()
returns the value of
iSize
.
My base class header
score.h
looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#ifndef SCORE_H
#define SCORE_H
class Score
{
private:
int *ipScore;
float fAverage;
int iSize;
public:
Score(
void enterScores();
void calcAverage();
void output();
void output(float, int*);
void setSize();
int getSize();
void setScore();
int *getScore();
float getAverage();
};
#endif
|
You can see that I have used
curve1Obj
to enter scores, calculate average and output. So if I call
getSize()
function with
cuve1Obj
, it gives the right size that I took from user in
enterScores()
function. Also the result is same if I call
getSize()
in
score.cpp
definition file in any of the functions (obviously).
.....
The problem is when I call
curve()
function of
Curve2
class in
main
(line 23) with the object
curve2Obj
, it creates a new set of
ipScore
,
fAverage
and
iSize
(i think?) with garbage values. So when I call
getSize()
in
curve()
definition in
curve2.cpp
, it outputs the garbage.
.....
How can I cause it to return the old values that are set in
curve1.cpp
?
Here is my
curve2.cpp
1 2 3 4 5 6 7 8 9 10
|
#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve()
{
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}
|
Can I use a function to simply put values from old to new variables? If yes then how?