c++ - Derived function not returning correct data values -
still working on inheritance program, base class shape , there 3 derived classes: rectangle, circle, , square (square derived rectangle). when set data values trough respective constructors, false values data members of each derived class when display them i'm either not setting them correctly (my guess) or i'm not displaying them correctly. here code snippet.
class shape { public: shape(double w = 0, double h = 0, double r = 0) { width = w; height = h; radius = r; } virtual double area() = 0; void display(); protected: double width; double height; double radius; };
one derived class:
class rectangle : public shape { public: rectangle(double w, double h) : shape(w, h) { } double area(); void display(); };
rectangle's display function:
double rectangle::area() { return width * height; }
here main():
#include<iostream> #include "shapeclass.h" using namespace std; int main() { rectangle r(3, 2); circle c(3); square s(3); c.display(); s.display(); r.display(); system ("pause"); return 0; }
complete shapeclass.cpp:
#include<iostream> #include "shapeclass.h" using namespace std; double shape::area() { return (width * height); } double rectangle::area() { return width * height; } double circle::area() { return (3.14159 * radius * radius); } double square::area() { return width * width; } void square::display() { cout << "side length of square: " << width << endl; cout << "area of square: " << this->area() << endl; } void circle::display() { cout << "radius of circle: " << radius << endl; cout << "area of circle: " << this->area() << endl; } void rectangle::display() { cout << "width of rectangle: " << width << endl; cout << "height of rectangle: " << height << endl; cout << "area of rectangle: " << this->area() << endl; }
you need make area()
function virtual in rectangle
well:
class rectangle : public shape { public: rectangle(double w, double h) : shape(w, h) { } virtual double area(); void display(); };
now, keep in mind if want display()
function overriden in specific shapes (in example see rectangle
has display()
function), need make virtual well, in both classes:
virtual void display();
edit: tried following code , worked perfectly. it's based on code, there might problem how built project or how compile/link it.
#include <iostream> using namespace std; class shape { public: shape(double w = 0, double h = 0, double r = 0) { width = w; height = h; radius = r; } virtual double area() = 0; virtual void display() = 0; protected: double width; double height; double radius; }; class rectangle : public shape { public: rectangle(double w, double h) : shape(w, h) { } virtual double area() { return width * height; } virtual void display() { cout << "width of rectangle: " << width << endl; cout << "height of rectangle: " << height << endl; cout << "area of rectangle: " << this->area() << endl; } }; int main(int argc, char* argv[]) { rectangle r(3, 2); r.display(); system ("pause"); return 0; }
Comments
Post a Comment