Classes (base point.h and derived circle.h)

Programming Logic II
Project #7

1. Given the base class header file point.h
// Point Class Header File
// Filename: point.h

#ifndef POINT_H
#define POINT_H
class point
{
public:
point(double init_x = 0.0, double init_y = 0.0); // sets x to init_x and y to init_y
// or uses the default values
void setPoint(double init_x, double init_y); // sets x to init_x and y to init_y
void print() const; // prints a point in the form (x, y) with a precision of 1
double getX() const; // returns the value of x
double getY() const; // returns the value of y
private:
double x;
double y;
};
#endif

2. Write the corresponding implementation file point.cpp

3. The derived class circle will inherit from the point class. You are given the header file circle.h
// Class Circle Header File
// Filename: circle.h

#ifndef CIRCLE_H
#define CIRCLE_H
#include "point.h"
class circle: public point
{
public:
circle(double init_x = 0.0, double init_y = 0.0, double r = 0.0);
// uses default parameters to set x to init_x, y to init_y and radius to r
void print() const;
// prints the following information about a circle:
// center point, radius, circumference, area with a precision of 1
void setRadius(double r);
// sets radius to r
double getRadius() const;
// returns radius
double getCircumference() const;
// returns circumference (2 * radius * 3.14159)
double getArea() const;
// returns area (3.14159 * radius2)
private:
double radius;
};
#endif
4. Write the corresponding implementation file circle.cpp

5. Write the driver program lastname7.cpp to accomplish the following:
Create object circle1 using the constructor with three parameters, setting x to 1, y to 2, and radius to 3
Create object circle2 using the default constructor
Print circle1
Set circle2 as follows: x to 4, y to 5 and radius to 6 (this will require two set functions)
Print circle2


I have the two header files complete along with their implementation files. The driver program is working within reason. My issue is the last two lines I've bolded and italicized in the quoted text. I cannot get the radius to output for circle2. The point outputs, but how do I get circle2 to accept the radius of six? Any tips?

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream> 
#include "point.h"
#include "circle.h"

using namespace std;

int main()
{
	circle circle1(1, 2, 3);
	circle circle2;

	cout << "Circle 1 should be (1, 2) and have a radius of 3. Circle 1 is as follows: " << endl;
	circle1.print();
	cout << endl;

	circle2.setPoint(4, 5);
	circle2.setRadius(6);
	cout << "Circle 2 should be (4, 5) and have a radius of 6. Circle 2 is as follows: " << endl;
	circle2.print();
	cout << endl;

	return(0);
}


My output:
Circle 1 should be (1, 2) and have a radius of 3. Circle 1 is as follows:
The center point is (1.0, 2.0)
The radius is: 3.0
The circumference is: 18.8
The area is: 28.3

Circle 2 should be (4, 5) and have a radius of 6. Circle 2 is as follows:
The center point is (4.0, 5.0)
The radius is: 0.0
The circumference is: 0.0
The area is: 0.0

Press any key to continue . . .
Without knowing how setRadius is implemented, I don't think anyone will be able to tell you what is wrong.

In other words, please post the definition of setRadius.
I understand - I didn't want to fill up with a bunch of code so I tested the waters to see if anyone could decipher anything from that. I assumed more would be needed but I was waiting heh... :-)

I'm going to include here the circle.cpp file (which has the setRadius function and coincides with circle.h) and the point.cpp file (as a back up).

circle.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
46
#include <iostream>
#include <iomanip>
#include "circle.h"

using namespace std;

circle::circle(double init_x, double init_y, double r) : point(init_x, init_y)
	{
		radius = r;
	}

void circle::print() const
	{
		cout << "The center point is ";
		point::print();
		cout << "The radius is: " << fixed << setprecision(1) << getRadius() << endl;
		cout << "The circumference is: " << getCircumference() << endl;
		cout << "The area is: " << getArea() << endl;

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

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

double circle::getCircumference() const
	{
		double c;

		c = (2 * radius * 3.14159);
		return(c);
	}

double circle::getArea() const
	{
		double a;

		a = (3.14159 * (radius * radius));
		return(a);
	}


point.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
#include <iostream>
#include <iomanip>
#include "point.h"

using namespace std;

point::point(double init_x, double init_y)
	{
		x = init_x;
		y = init_y;
	}

void point::setPoint(double init_x, double init_y)
	{
		x = init_x;
		y = init_y;
	}

void point::print() const
	{
		cout << fixed << setprecision(1) << "("  << x << "," << " " << y << ")" << endl;
 	}

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

double point::getY() const
	{
		return(y);
	}
1
2
3
4
void circle::setRadius(double r)
	{
		r = radius;
	}

r is what you want to assign TO radius (meaning you have it backwards: radius = r;).
AHH! I knew it would be something small. I feel like an uber-dork now. xD

It all works perfectly now! Thanks so much!
Topic archived. No new replies allowed.