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:
// Implementation file for the Point class
#include <iostream>
#include "Point.h"
usingnamespace 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;
}
#include <iostream>
#include <iomanip>
usingnamespace 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;
}