Problem with functions in inherited classes

Here is what I'm trying to do.

I have 3 classes:

base
|
derived1
|
derived2

In the main program, I call a function within the base class that sets 2 variables. In the derived class, I have a function that calls on a function within the base class to retrieve and return those variables.

The problem I'm having is that it is not working right, or I'm doing something wrong. What seems to be happening, is that when the function in the derived class calls the base class function, rather than just running, it first seems to run the default constructor, which zeros out the stored variables. Then the function returns the zeros.

Does that make sense? I'm new to C++ so I may be missing something, but from what I've read, in theory, this should work.

Why is it calling the default constructor first?

Any help would be greatly appreciated.

Thanks.
Hmm can you post the code you have?

It's weird that is happening but could you not write the derived class constructor, to call the base class constructor and set the base class members that way? Then you wouldn't have the problem of it calling the default constructor because you would be specifying what base class constructor to use when you write you're derived class constructor.

derived-constructor(arg-list) : base-constructor(arg-list) {
body of derived constructor
}
I tried to keep the code I'm posting as limited as possible. I'm posting the base class and one of the derived classed as defined in the header files, the default constructor for the base class and the one function in the derived class that is calling on the base class function.

base class header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class pointType
{
public:
	  static void setPoints(double pointX, double pointY);
	    
	  void print() const;
	    
	  double getX() const;
	    
	  double getY() const;
	    
	  pointType();
	    
	  pointType(double pointX, double pointY);
	    
private:
	  static double x, y;
};


Base class constructor:
1
2
3
4
5
pointType::pointType()
{
	x = 0;
	y = 0;
}


derived class header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class circleType: public pointType
{

public:
		static void setRadius(double cRadius);

		void print() const;

		double getRadius() const;

		double calcArea() const;

		double calcCir() const;

		double calcDia() const;

		circleType ();

		circleType(double cRradius);

private:
		static double radius;
};


derived class function:
1
2
3
4
5
6
7
8
9
void circleType::print() const     //Prints the radius, diameter, area and circumference
{
	cout << "The radius of the circle is: " << getRadius() << endl;
	cout << "The diameter of the circle is: " << calcDia() << endl;
	cout << "The area of the circle is: " << calcArea() << endl;
	cout << "The circumference of the circle is: " << calcCir() << endl;
	cout << fixed << setprecision(2) << "The x and y coordinates are: ";
	cout << getX() << "," << getY() << "." << endl;
}



Thanks.
Did you mean to make pointType::x and pointType::y static on purpose? Do you know that static makes data members shared by all instances of the class?
I had initially made them static because I thought that the stored data was being lost/reset. It was just something I had tried and just hadn't removed yet.
That's probably what's causing it.
Ok, I removed all of the "statics" and I removed the =0 from the default constructors.

And it is still returning zero when I call the getX and getY functions.

I'm going to apologize in advance if this is too much, but I am going to post all of my code. I'm getting desperate.

Main .cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "pointType.h"
#include "circleType.h"
#include "cylinderType.h"
#include <iostream>

using namespace std;

int main()
{

        pointType point;

        double x, y;

	cout << "Enter the x-coordinate: ";
	cin >> x;

	cout << "Enter the y-coordinate: ";
	cin >> y;

	point.setPoints(x, y);
	point.print();

	circleType circle;

        double r;

	cout << "Enter the radius of the circle: ";
	cin >> r;

        circle.setRadius(r);
	circle.print();

	cylinderType cylinder;

	double h;

	cout << "Enter the height of the cylinder: ";
	cin >> h;

	cylinder.setHeight(h);
	cylinder.print();

    return 0;
}


base class .h
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
#ifndef H_PointType
#define H_PointType

using namespace std;

class pointType
{
public:
  
	  void setPoints(double pointX, double pointY);

	  void print() const;

	  double getX() const;

	  double getY() const;

	  pointType();

	  pointType(double pointX, double pointY);

private:

	  double x, y;

};

#endif 


base class .cpp
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
32
33
34
35
36
37
38
39
#include "pointType.h"
#include <iostream>
#include <iomanip>

using namespace std;

void pointType::setPoints(double pointX, double pointY)
{
	x = pointX;
	y = pointY;
}

void pointType::print() const
{
	cout << fixed << setprecision(2) << "The x and y coordinates are: ";
	cout << getX() << "," << getY() << "." << endl;
}

double pointType::getX() const
{
	return x;
}

double pointType::getY() const
{
	return y;
}

pointType::pointType()
{
	x;
	y;
}

pointType::pointType(double pointX, double pointY)
{
	x = pointX;
	y = pointY;
}


derived class 1 .h file
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
32
33
34
35
#ifndef H_CircleType
#define H_CircleType

#include "pointType.h"

using namespace std;

class circleType: public pointType
{

public:

		void setRadius(double cRadius);

		void print() const;

		double getRadius() const;

		double calcArea() const;

		double calcCir() const;
	
		double calcDia() const;
	
		circleType ();
	
		circleType(double cRradius);
	
private:

		double radius;

};

#endif 


derived class1 .cpp file
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "circleType.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void circleType::setRadius(double cRadius)
{
	radius = cRadius;
}

void circleType::print() const
{
	cout << "The radius of the circle is: " << getRadius() << endl;
	cout << "The diameter of the circle is: " << calcDia() << endl;
	cout << "The area of the circle is: " << calcArea() << endl;
	cout << "The circumference of the circle is: " << calcCir() << endl;
	cout << fixed << setprecision(2) << "The x and y coordinates are: ";
	cout << getX() << "," << getY() << "." << endl;
}

double circleType::getRadius() const
{
	return radius;
}

double circleType::calcArea() const
{
	double pi = 3.14159;
	return (pi * (radius * radius));
}

double circleType::calcCir() const
{
	double pi = 3.14159;
	return (calcDia() * pi);
}

double circleType::calcDia() const
{
	return radius * 2;
}

circleType::circleType()     //Default construtor
{
	radius;
}

circleType::circleType(double cRadius)
{
	radius = cRadius;
}


derived class2 .h file
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
32
33
#ifndef H_CylinderType
#define H_CylinderType

#include "circleType.h"

using namespace std;

class cylinderType: public circleType
{

public:

	void setHeight(double cHeight);
	
	void print() const;

	double getHeight() const;

	double calcVolume()const;

	double calcSurface() const;

	cylinderType();

	cylinderType(double cHeight);

private:

	double height;

};

#endif 


derived class2 .cpp file
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "cylinderType.h"
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void cylinderType::setHeight(double cHeight)
{
	height = cHeight;
}

void cylinderType::print() const
{
	cout << "The height of the cylinder is: " << getHeight() << endl;
	cout << "The radius of the cylinder base is: " << getRadius() << endl;
	cout << "The volume of the cylinder is: " << calcVolume() << endl;
	cout << "The surface area of the cylinder is: " << calcSurface() << endl;
	cout << fixed << setprecision(2) << "The x and y coordinates are: ";
	cout << getX() << "," << getY() << "." << endl;
}

double cylinderType::getHeight() const 
{
	return height;
}

double cylinderType::calcVolume() const
{
	double pi = 3.14159;
	return (height * pi) * (getRadius() * getRadius());
}

double cylinderType::calcSurface() const
{
	double pi = 3.14159;
	return (2 * calcArea()) + (calcCir() * height);
}

cylinderType::cylinderType()
{
	height;
}

cylinderType::cylinderType(double cHeight)
{
	height = cHeight;
}
Between lines 24 and 32, and 34 and 42 of main.cpp you're not setting x or y.

What's the point of lines 31 and 32 in pointType.cpp, 47 in circleType.cpp, and 42 in cylinderType.cpp?
What's the point of lines 31 and 32 in pointType.cpp, 47 in circleType.cpp, and 42 in cylinderType.cpp?


I was trying something. These are the default constructors. I originally had them set to = 0. I thought that if I removed the zero, it would just initialize the variable and retain its original data. I was kind of grasping at straws there.

Between lines 24 and 32, and 34 and 42 of main.cpp you're not setting x or y.


So I need to set x and y each time? Would I just use the same function call that I used initially?

point.setPoints(x, y);

Thanks.
I added point.setPoints(x, y); between lines 24 and 32 and 34 and 42 of the main.cpp as suggested, but I'm still getting zeros returned.
Um... No...

Here's what I'd do:
1
2
3
4
5
6
7
8
9
10
//pointType.cpp
pointType::pointType(const pointType &b):x(b.x),y(b.y){}

//circleType.cpp
circleType::circleType(const pointType &b):pointType(b),radius(0){}

//main.cpp
circleType circle=point;
//...
circle.setRadius(r);


EDIT: Remember that data isn't shared unless told to be. The assignments you make to one object won't carry over to the next unless you copy them.
Last edited on
Ok, I got it working.

First, thank you everyone for your help and advice. I was not resending the user input for x, y, and the radius with each call.

Here's what I ended up doing.

In the derived class:

1
2
3
4
circleType::circleType(double pointX, double pointY, double cRadius)     //Constructor with parameters
{
	pointType::setPoints(pointX, pointY);
	radius = cRadius;


In main:

circle.setRadius(x, y, r);

There might be a better way of doing this, possibly as helios suggested, but I'm going to stick with this method for now. It works and I found it in my text book...

Thanks again.
Topic archived. No new replies allowed.