Multiple inclusions of a Header File

I'll start with my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef H_cylinderType
#define H_cylinderType

class cylinderType
{
public:
	void getRadius(double radius);
	void getHeight(double height);
	double area();
	double volume();
	cylinderType(double radius = 0, double height = 0);

private:
	double rad;
	double heigh;
};
#endif  


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

using namespace std;


void cylinderType::getRadius(double radius)
{
	rad = radius;
}


void cylinderType::getHeight(double height)
{
	heigh = height;
}

double cylinderType::area()
{
	return (2 * 3.14 * pow(rad, 2) + 2 * 3.14 * rad * heigh);

}

double cylinderType::volume()
{
	return (3.14 * pow(rad, 2) * heigh);
}


1
2
3
4
5
6
7
#include "cylindertype.h"

class conversion: public cylinderType
{
public:
	double convert ();
};


1
2
3
4
5
6
7
8
9
10
#include "StdAfx.h"
#include "conversion.h"


double conversion::convert ()
{
	return cylinderType::area() * 28.32;

}


Main program:
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "cylinderType.h"
#include "conversion.h"

using namespace std;


int main()
{
	double radius;
	double height;
	double shippingCost;
	double pricePrePaint;

	cylinderType column;
	conversion liters;

	cout << fixed << showpoint << setprecision(2);

	cout << "Welcome to Amanda and Tyler's Cylinder Painting and Shipping.\nPlease enter the radius and the height of the cylinder, in feet: " << endl;
	cin >> radius >> height;
	cout << endl;

	column.getRadius(radius);
	column.getHeight(height);

	cout << "The area of your cylinder is: " << column.area() << " cubic feet." << endl;

	cout << "The volume of your cylinder is: " << column.volume() << " cubic feet." << endl;

	cout << "The volume of your cylinder in liters is: " << liters.convert() << " liters." << endl;

	cout << "Please enter the shipping cost, per liter: ";
	cin >> shippingCost;
	cout << endl;

	pricePrePaint = shippingCost * liters.convert();

	cout << "Current Amount due: $" << pricePrePaint
		 << endl;


	return 0;
}


I have read the section in the book but I am still unsure on how to use 2 header files. In my cylinderType header file, am I wanting to include
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef H_cylinderType
#define H_cylinderType
public:
	void getRadius(double radius);
	void getHeight(double height);
	double area();
	double volume();
	cylinderType(double radius = 0, double height = 0);

private:
	double rad;
	double heigh;
in my cylinderType header file? If so, I would assume before the class definition? The book wasn't very elaborate on this. any help is appreciated. Thank you
Last edited on
That's the right idea, but the inclusion guards go around the entire file.
You also need a #endif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef H_cylinderType
#define H_cylinderType

class cylinderType
{
public:
	void getRadius(double radius);
	void getHeight(double height);
	double area();
	double volume();
	cylinderType(double radius = 0, double height = 0);

private:
	double rad;
	double heigh;
};
#endif 



Ok, I fixed that problem ( I edited my code I posted on here as well). But now when I compile I am getting this error, when it is on the generating code part.

Chapter 11 Number 4.obj : error LNK2019: unresolved external symbol "public: __thiscall cylinderType::cylinderType(double,double)" (??0cylinderType@@QAE@NN@Z) referenced in function _main

What's wrong?
You have forgot to define the cylinderType constructor.
Last edited on
In the cylinderTpe.cpp in your original post, I don't see an implementation of the following constructor declared in your header:
 
cylinderType(double radius = 0, double height = 0);


The linker is telling you it can't find that constructor in your object file. Presumably, because it's not in your .cpp file.


I try to teach myself a constructor and how to incorporate it. That is what happens. Thank you much.
I know the topic is wrong. I just don't want to repost for each question I have, unless I need to.

When I run this code, my result of my conversion.convert is HUGE! I had this problem with my area and volume and figured it out with help. This time I am not sure. My only thinking is to re-write the conversion files and have them get the area, then use the convert formula. Any ideas?
Show that constructor that you coded.
Your problem is the same that that time, you don't initialize your variables.

`column' and `liters' are not related in any way, but you seem to assume that their state is the same.
So when you ask `liters' to `convert()' it uses its own `rad' and `heigh', which contain garbage.

Also, you are abusing inheritance there.
Last edited on
I have started re-working my code right now. The more I write on it, the more I realize some mistakes I've made and ways to smooth it out. For instance, I am incorporating my area, volume and measurement conversion all into class cylinderType. Is that what you mean in that I am abusing inheritance?

This is the problem I was given:
Amanda and Tyler opened a business that specializes in shipping liquids, such as milk, juice and water, in cylindrical containers. The shipping charges depend on the amount of the liquid in the container. (For simplicity, you may assume that the container is filled to the top.) They also provide the option to paint the outside of the container for a reasonable amount. Write a program that does the following:

a. Prompts the user to input the dimensions (in feet) of the container (radius of the base and the height).

b. Prompts the user to input the shipping cost per liter.

c. Prompts the user to input the paint cost per square foot. (Assume that the entire container including the top and bottom needs to be painted.)

d. Separately outputs the shipping cost and the cost of painting.

You program must use the class cylinderType to store the radius of the base and the height of the container.


I have since dropped my derived class. The only way I can think of this problem is to do one class for my area, volume and conversion. Would it make sense to use a derived class if the user wants to paint the cylinder?
> I am incorporating my area, volume and measurement conversion all into class cylinderType.
> Is that what you mean in that I am abusing inheritance?
No. A `conversion' should only convert a measure from one unit to the other.
It is not a cylinder.

A cylinder is a solid, ¿why does it care about units? It is not its responsibility to make conversions.
You also could say
1
2
cylinder::cylinder( unit radius, unit height )
unit cylinder::area() const
> Would it make sense to use a derived class if the user wants to paint the cylinder?
Maybe, but would not derive from cylinder.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//warning: some things are omitted
class cylinder{
public:
   value_type area() const;
   value_type volume() const;
};

class article{
private:
   cylinder container;
   string content; //milk, juice, water, etc. (consider other ways)
   money ship_cost() const{
      conversion feet3_litter(28.32);
      return feet3_litter.convert( container.volume() ) * ship_cost_per_litter;
   }

public:
   money total_cost() const;
   void print_detail() const;
};



PS: Tried to post both replies together.
It will just remain in `Sending... Please wait'
Last edited on
Like I said, I was re-working it. This is what I've come up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef H_cylinderType
#define H_cylinderType

class cylinderType
{
public:
	double getRadius() const;
	double getHeight() const;
	double volume() const;
	double literVolume() const;
	void cylinderType::setDimension (double rad, double heigh);
	cylinderType();
	cylinderType(double rad, double heigh);
	
private:
	double radius;
	double height;
};
#endif 

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
double cylinderType::getRadius() const
{
	return radius;
}


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

void cylinderType::setDimension (double rad, double heigh)
{
	if (rad >= 0)
		radius = rad;
	else
		radius = 0;

	if (heigh >= 0)
		height = heigh;
	else
		height = 0;
}

double cylinderType::volume() const
{
	return (3.14 * pow(radius, 2) * height);
}

double cylinderType::literVolume() const
{
	return (28.32 * (radius * radius) * 2 * 3.14 * height);
}

 cylinderType::cylinderType(double rad, double heigh)
 {
	 setDimension (rad, heigh);
 }

 cylinderType::cylinderType()
 {
	 radius = 0;
	 height = 0;
 }

1
2
3
4
5
6
7
#include "cylinderType.h"

class paint: public cylinderType
{
public:
	double area () const;
};

1
2
3
4
5
6
7
8
#include "stdafx.h"
#include "paint.h"

double paint::area() const
{
	return ((2 * 3.14 * (getRadius() * getRadius())) + (2 * 3.14 * getRadius() * getHeight()));

}


Main program:
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
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include "paint.h"
#include "cylinderType.h"

using namespace std;


int main()
{
	double radius;
	double height;
	double shippingCost;
	double amountDue;
	int decision;
	double paintCost;
	
	cylinderType cylinder;
	paint painted;

	cout << fixed << showpoint << setprecision(2);

	cout << "Welcome to Amanda and Tyler's Cylinder Painting and Shipping.\nPlease enter the radius and the height of the cylinder, in feet: " << endl;
	cin >> radius >> height;
	cout << endl;

	cylinder.setDimension(radius, height);

	cout << "The volume of your cylinder is: " << cylinder.volume() << endl;
	cout << "The volume of your cylinder in liters is: " << cylinder.literVolume() << " liters." << endl;

	cout << "How much is the shipping cost, per liter? ";
	cin >> shippingCost;
	cout << endl;

	amountDue = shippingCost * cylinder.literVolume();

	cout << "Your amount due today is: $" << amountDue
		 << " Would you like it painted before shipping? 1 for Yes or 2 for No: ";
	 cin >> decision;
	cout << endl;

	if (decision == 2)
		cout << "Your amount due is still: $" << amountDue
			 << " Thank you for shopping with us today!" << endl;

	else if (decision == 1)
		cout << "The area of your cylinder is: " << painted.area() << endl;
		cout << "How much is the paint cost per square foot?";
		cin >> paintCost;
		cout << endl;

		amountDue = paintCost * painted.area();

		cout << "Your new amount due is: $" << amountDue
			 << " Thank you for shopping with us today!" << endl;
	

	return 0;
}


Yes, I know I could make the paint decision a Boolean value but I am still unsure on that right now. I am mainly wanting to get this program done since I've been workin on it for too long and the if...else statement was an easier way to go.

Didn't try it before I posted it. Yes, i know now the if...else won't work. This problem is going to drive me nuts. I am late on it, and every route I try I bang my head.
Last edited on
Topic archived. No new replies allowed.