Multiple file programs for implementation of classes

Hello,

My book is demonstrating how to make multiple file programs for the implementation of objects. I am unsure on how to include header files however. For example, if I am using <cmath> in my program, but it is for a certain class, do I include it in both in client program and the class implementation file?

Could someone double-check how I did it because I am not sure if it is right. Thanks.

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
//Circle.cpp - Circle Class Implementation
#include "Circle.h"
#include <iostream>
#include <cmath>
using namespace std;
// Circle::SetRadius
//	 	Sets radius of circle to argument passed to function.
// If argument is 0 or less, then sets radius to default of 1.
void Circle::setRadius(double r) {
	if(r > 0) 
		radius = r;
	else
		radius = 1.0;
		cout << "Error. Radius less than 0. Setting radius to default value of 1.0.\n";
}

// Circle::getRadius
// 		Returns radius of circle.
double Circle::getRadius() {
	return radius;
}

// Circle::getArea
//		Returns pi times radius squared
double Circle::getArea() {
	return 3.14 * pow(radius, 2);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Circle.h - Class specification

#ifndef CIRCLE_H
#define CIRCLE_H

//Class declaration
class Circle {
private:
	double radius;
public:
	void setRadius(double);
	double getRadius();
	double getArea();
};
#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
//client program
#include <iostream>
#include <cmath>
#include "Circle.h"
using namespace std;

int main() {
	Circle someCircle;
	Circle someOtherCircle;
	double userRadius;
	
	cout << "Radius of someCircle plz: ";
	cin  >> userRadius;
	someCircle.setRadius(userRadius);
	cout << "Radius of someOtherCircle plz: ";
	cin >> userRadius;
	someOtherCircle.setRadius(userRadius);
	
	cout << "The radius of someCircle is " << someCircle.getRadius() 
		 << " and it has an area of " << someCircle.getArea() << ".\n";
	cout << "The radius of someOtherCircle is " << someOtherCircle.getRadius() 
		 << " and it has an area of " << someOtherCircle.getArea() << ".\n";

	return 0;
}


Also, I get the following console warning when I compile, however it still compiles the program. I am using the G++ compiler.
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated

What does this mean? Thanks!
Don't compile header files. Compile source files.

Odd that clang wouldn't come with a clearer error message.
Thanks cire! Am I including other header files correctly? As in, is it correct to include <cmath> in both the Circle class implementation and client program?
There is no reason to include cmath in the client program, but it doesn't hurt anything.
Gotcha. To make sure I have this nailed down, if I hypothetically had a function in the client program that used <cmath>, then I would include <cmath> in both?
Yes.
Okay! Thank you very much cire. :)
Topic archived. No new replies allowed.