Data not being passed properly?

Okay, everything is compiling and running. It seems that my problem is that circleType.h isn't being passed to cylinderType.h since the main isn't producing proper output for the cylinder functions. Here is all of the code. Does anyone see why this isn't being pass properly? Thanks so much!

circleType.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
29
#ifndef H_CircleType
#define H_CircleType


class circleType
{

public:

		void setRadius(double);

		void print() const;

		double getRadius() const;

		double calcArea() const;

		double calcCir() const;
	
		circleType();

	
private:

		double radius;

};

#endif  


circleTypeImpp.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
#include "circleType.h"
#include <iostream>
#include <cmath>

using namespace std;

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

void circleType::print() const
{
	cout << "The radius of the circle is: " << getRadius() << endl;
	cout << "The area of the circle is: " << calcArea() << endl;
	cout << "The circumference of the circle is: " << calcCir() << 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 (radius * 2 * pi);
}


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



cylinderType.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_CylinderType
#define H_CylinderType

#include "circleType.h"

class cylinderType: public circleType
{

public:

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

	double getHeight() const;

	double calcVolume()const;

	double calcSurface() const;
	
	cylinderType();

private:

	double height;

};
#endif 



cylinderTypeImpp.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
#include "circleType.h"
#include "cylinderType.h"
#include <iostream>
#include <cmath>


using namespace std;

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

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;
}

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()  //default constructor
{
	height = 0;
}



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
#include "circleType.h"
#include "cylinderType.h"
#include <iostream>

using namespace std;

int main()
{
	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;
}
Your problem is that circle and cylinder are completely separate objects that have nothing shared between them.
So setting the values for circle has absolutely no effect whatsoever on the values of the data members for cylinder.

Instead of declaring a circleType and a cylinderType, try making just one cylinderType object and using just that.
So move line 19 up to replace line 9 (and adjust lines 16-17 accordingly).
(Since cylinderType derives from circleType, it'll have all of the same member functions that circleType has.)
Ahhhhhh. That makes sense... That solved the problem! Thank you!
Last edited on
Topic archived. No new replies allowed.