Hi gang,
Yet another inheritance problem. Can anyone tell me why, for the below code, the length_ variable of my Cylinder object is being output when I call it with Cylinder::disp() as -9.25596e+061? Can't figure this one out!
Thanks!
Code:#include <iostream> #include <cmath> using namespace std; class Point { protected: double x_, y_; public: Point(double, double); double area(); double distance(Point&, Point&); }; double Point::area() { return 0; } Point::Point(double XX, double YY) { x_ = XX; y_ = YY; } double Point::distance(Point& point1, Point& point2) { return sqrt(pow((point2.x_-point1.x_), 2)+pow((point2.y_-point1.y_), 2)); } class Circle : public Point { protected: double radius_; public: Circle(double rad_, double x_, double y_) : radius_(rad_), Point(x_, y_){} double area(); }; double Circle::area() { return radius_*radius_*3.1419; } class Cylinder : public Circle { protected: double length_; public: Cylinder(double l_, double r_, double x_, double y_) : length_(l_), Circle(radius_, x_, y_) {} double area(); void disp(); }; double Cylinder::area() { return length_*radius_*x_*y_; } void Cylinder::disp() { cout << length_ << " " << radius_ << " " << x_ << " " << y_ << endl; } int main() { Point myPoint(3.1, 4.2); Circle myCircle(4.3, 3.2, 2.3); Cylinder myCylinder(1.3, 3.1, 3.4, 3.2); myCylinder.disp(); cout << "The area of myPoint is " << myPoint.area() << endl; cout << "The area of myCircle is " << myCircle.area() << endl; cout << "The area of myCylinder is " << myCylinder.area() << endl; return 0; }



LinkBack URL
About LinkBacks


