Getting -9.25596e+061 passing paremeters inheritance

I've been struggling a heck of a lot with this bug I just don't know how to fix.

I've got two classes, one inheriting from the other. I pass parameters from the main function, radius, into the class' variable newRadius, and then return the area of a circle from a class which inherits the newRadius variable and calculates a variable called area as well included in the parent class.

There's nothing wrong for me, a beginner programmer.

Any help will be much appreciated!

http://gyazo.com/55ee6c224333303331090db4ef326ba2

Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "iostream"

#include "AreaC.h"

using namespace std;

double radius, area;

int main()
{
	adquireData Get;
	
	cout << "Input the cicrcle's radius: ";
	cin >> radius;
	Get.setNewRadius(radius);
	
	cout << endl;

	cout << "Circle's area is: " << Get.getArea();

	return 0;
}


AreaC.h (Where classes are)

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
#include "iostream"

using namespace std;

#ifndef AREAC_H
#define AREAC_H

class adquireData
{
public:

	double getArea();

	void setNewRadius(double);

protected:

	double newRadius, area;

};

class computeData :public adquireData
{
public:
	
	void computeArea();

};

#endif 


AreaC.cpp (where classes' method definitions are)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "AreaC.h"

// InAdquireData

double adquireData::getArea()
{
	return area;
}

void adquireData::setNewRadius(double radius)
{
	newRadius = radius;
}

// InComputeArea

void computeData::computeArea()
{
	area = (newRadius * newRadius) * 3.1416;
}
Last edited on
There are a few reasons why what you are trying to do won't work. Why don't you just put the computeArea() function in the adquireData class? That would make everything much easier. Then, your main() would work like you want it to.
Well, I am supposed to implement inheritance into this.
OK, that can be done. Can you be a little bit more specific about what you're
supposed
to do? Given what you've presented, there is absolutely no reason for using inheritance.
Practice? I mean, maybe. You just can or cannot help me find what's the problem, nobody has helped me, my teacher tried but he just couldn't.
Like I said, there is more than just one problem with your code. Perhaps I can help, or perhaps not; I will try. Inheritances is somewhat complicated, especially for beginners. I do not know your level or your understanding, so I have to make assumptions. Forgive me if I get those assumptions wrong.

Now, I assume your skill level is low as is your understanding. So, I'll do my best to help you with your code. If you want more let me know.

You declare an adquiredData Get. Then you call Get.setNewRadius(radius);. This works fine. In your class adquiredData, you have a function called setNewRadius.

Then, you try to use Get to "get" the area with Get.getArea(); The problem is that adquiredData class has no method getArea(). There is no link between your adquiredData class and your computeData class.

Furthermore, there is no easy way to link your two classes, which is why I asked for clarification as to what you are "supposed" to do. I can show you inheritance examples, but I'm not sure it will help you with your problem.
Yes it does
Last edited on
> The problem is that adquiredData class has no method getArea()
yes, it does.
1
2
3
4
5
6
7
8
9
class adquireData
{
public:
	double getArea(); //<- here
	void setNewRadius(double);

protected:
	double newRadius, area;
};



> and calculates a variable called area
you never do that, you never call `computeArea();'
so you are returning an uninitialized variable.
I called it, same results. Any ideas?. Mybe I'am not calling it correctly
@ne555, you're right, but that's not the root of problem here. The problem is that the area is never updated, like you said, between the two classes. There is no inheritance used here.

@Suppresed, this is probably a terrible example, but it does what you want. Please forgive me, I've been drinking.

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
#include <iostream>

class Shape
{
public:
    Shape(){};
    void setRadius(double rad)
    {
        radius = rad;
    }
protected:
    double area, radius;
};

class Circle : public Shape
{
public:
    Circle(){};
    double getArea()
    {
        return (radius * radius) * 3.14;
    }
};

int main()
{
    Circle c;
    c.setRadius(5);
    std::cout << c.getArea() << std::endl;
    return 0;
}


Does this make sense to you?
Yes, i put return (radius * radius) * 3.14; to my original code it worked too so the problem is somewhere in between computing area and using newRadius
But in your original code you are never computing the area, only getting it.
Topic archived. No new replies allowed.