How to transfer values set in privates of base class by an object of one derived class to an object of another derived class?

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?
Last edited on
Please reply anyone... Duoas, helios ???
Not enough code to comment on.

You haven't shown the constructor of either Score or Curve2, so no way to tell if you're initializing your variables correctly.

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

Correct. cuirve1Obj and curve2obj are separate instances. Each instance has its own member variables.
with garbage values.

How did curve2Obj's variables get initialized? If they're garbage, I'm guessing you didn't initialize them.

How can I cause it to return the old values that are set in curve1.cpp?

You have to set the variables in curve2Obj somehow. Either through an explicit constructor, a copy constructor or an assignment operator.


@AbstractionAnon Thanks for replying... No, I am forced not to use the constructor. Well, the variables are initialized in score.cpp file.

I can show the whole code if you want...

How to set the variables using assignment operator? If I show you the whole code, can you tell me?
Topic archived. No new replies allowed.