Overoading operators

Hello,
I am very new to c++ and need a bit of help please. I am writing a program to calculate the area of a cylinder. I have come across a few problems already but the main hurdle I am not able to overcome is overloading the ostream << operator with inheritance. I keep getting C2804 error. Here is my code. If it makes any sense to you I would very much appreciate any feedback. Thank you in advance.
This is a point class which gets the coordinates of the centre of a circle:
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
// Specification file for the Point class

#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;
class Point;
ostream& operator<< (ostream &, const Point &);

class Point
{
protected:
   double x;
   double y;
public:
	Point(); //Default constructor
	Point(double, double); //Constructor
	void setPoints(double, double);
	double getX();
	double getY();
	friend ostream& operator<< (ostream &, const Point &); //friend overloaded << operator
 };

#endif

My 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
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
// Implementation file for the Point class
#include <iostream>
#include "Point.h"
using namespace std;

//***********************************************************
// Initialise  point x and y
//***********************************************************

Point::Point()
{
   x = 0.0;
   y = 0.0;
 
}
//***********************************************************
// Constructor accepts arguments for x and y
//***********************************************************

Point::Point(double pointx, double pointy)
{
   x = pointx;
   y = pointy;
 
}
//***********************************************************
// setPoints sets the value of points x and y      
//***********************************************************

void Point::setPoints(double pointx, double pointy)
{
	x = pointx;
	y = pointy;
}
//***********************************************************
// getPointX gets the value of point x      
//***********************************************************

double Point::getX ()
{
	return x;
}

//***********************************************************
// getPointY gets the value of point y            
//***********************************************************

double Point::getY()
{
	return y;
}

ostream &operator<<(ostream &strm, const Point &pt)
{
  strm<< pt.x << ", " << pt.y << "\n";

  return strm;
}

My circle class which gets radius of circle:
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
// Specification file for the Circle class

#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include "Point.h"
using namespace std;
class Circle;//forward declaration
class Point;//forward declaration
ostream& operator<< (ostream &, const Point &);

class Circle:public Point
{
protected:
	double radius;
public:
	Circle();//Default constructor
	Circle(double, double, double); //Constructor
	void setRadius();
	double getRadius();
	double getArea();
	friend ostream& operator<< (ostream &, const Point &, Circle &cir); //friend overloaded << operator
 };

#endif

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
47
48
49
50
51
52
53
54
55
56
57
// Implementation file for the Circle class
#include <iostream>
#include <iomanip>
#include <math.h>
#include "Circle.h"
#include "Point.h"
using namespace std;

//***********************************************************
// Default constructor
//***********************************************************

Circle::Circle():Point()
{ 
	radius=0.0;
}

//***********************************************************
// Constructor accepts arguments radius
//***********************************************************

Circle::Circle(double r):Point (pointx, pointy)
{ 
	radius=r;
}
  
//***********************************************************
// mutator function
//***********************************************************

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

//***********************************************************
// accessor function
//***********************************************************

double Circle::getRadius ()
{
	return radius;
}
//***********************************************************
// getArea returns the area of a circle   
//***********************************************************

double Circle::getArea ()
{
	return (2 * radius * radius * 3.14159);
}
ostream &operator<<(ostream &strm, const Point &pt, Circle &cir)
{
  strm<< pt.x << ", " << pt.y << ", " << cir.radius << "\n";

  return strm;
}

My final class the cylinder class:
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
// Specification file for the Cylinder class

#ifndef CYLINDER_H
#define CYLINDER_H
#include <iostream>
#include "Point.h"
#include "Circle.h"
using namespace std;
class Circle;
class Point;
class Cylinder;

class Cylinder:public Circle
{
protected:
	double height;
public:
	Cylinder(); //Default constructor
	Cylinder(double); //Constructor
	double setHeight();
	double getHeight();
	double Area();
 };

#endif

Cylinder.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
// Implementation file for the Cylinder class
#include <iostream>
#include <iomanip>
#include <math.h>
#include "Point.h"
#include "Circle.h"
#include "Cylinder.h"
using namespace std;

//***********************************************************
// Default constructor
//***********************************************************
Cylinder::Cylinder():Circle()
{
	height=0.0;
}

//Constructor #2
Cylinder::Cylinder(double r, double h):Circle(double r)
{
	height=h;
	area= getArea()*h;
}
Cylinder::setHeight()
{ 
	height=h;
}

//Get height function
Cylinder::getHeight ()
{
	return height;
}

//Get area function
Cylinder::Area()
{
	return getArea()+ (2*3.14159*radius*h);
}

And finallly if you are still with me my 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
#include <iostream>
#include <iomanip>
using namespace std;

#include "Point.h"
#include "Circle.h"
#include "Cylinder.h"

void main()
{
	// create a Point object and display it
	Point aPoint(1,2);
	cout<<aPoint;

	//create a Circle object and display it
	Circle aCircle(-3,2,5.25);
	cout << fixed << showpoint << setprecision(2);
	cout<<endl<<aCircle<<endl;

	//create a Cylinder object 
	Cylinder aCylinder(1,2,3,4);
    //use get functions to display the cylinder's coordinates, radius and height
	cout << "X coordinate is " << aCylinder.getX()
		<< "\nY coordinate is " <<aCylinder.getY()
		<< "\nRadius is " <<aCylinder.getRadius()
		<< "\nHeight is " <<aCylinder.getHeight() << "\n\n";
	cout<<"Area of the above cylinder is "<<aCylinder.area()<<" sq.cm"<<endl<<endl;
}
operator<< must take exactly two arguments so this doesn't work:
ostream &operator<<(ostream &strm, const Point &pt, Circle &cir)
Thanks makes sense.
Topic archived. No new replies allowed.