Trouble with dynamic array of pure base class c++ -


i have programming assignment intro c++ class. program supposed use virtual functions input, calculate , display quantities associated various shapes. shape class must contain pure virtual functions: display, getdimensions, area, perimeter, , volume.

the program driver must allocate array of 20 pointers shape repeatedly fill array objects entered.

my program quitting when gets display call. take mean either getdimensions or display functions not working correctly. i've spent hours trying sort out , can't seem find wrong if point me in right direction.

here code:

#include <iostream> #include <cmath> using namespace std;  const double pi=3.141592654;  class shape { protected:     double x, y; public:     shape(): x(1.0),y(1.0){}     shape(double a, double b): x(a),y(b){}     virtual void display()=0;     virtual void getdimensions()=0;     virtual double area()=0;     virtual double perimeter()=0;    virtual double volume()=0; };  class rectangle: public shape { public:     rectangle():shape(){}     rectangle(double a, double b):shape(a,b){}     void display();     void getdimensions()     {         cout<<"input rectangle - "             <<"enter length , width: ";         cin>>x>>y;     }     double area()         {return x*y;}     double perimeter()         {return (2*x)+(2*y);}     double volume()         {return 0;} };  class circle: public shape { private:     double radius; public:     circle(): radius(1.0){}     circle(double r): radius(r){}     void display();     void getdimensions()     {         cout<<"input circle - enter radius: ";         cin>>radius;     }     double area()         {return pi*radius*radius;}     double perimeter()         {return 2*pi*radius;}     double volume()         {return 0;}  };  class triangle: public shape  { private:     double z; public:     triangle():z(1.0), shape(){}     triangle(double f, double g, double w):z(w), shape(f,g){}     void display();     void getdimensions()     {         cout<<"input triangle - "             <<"enter side 1, side 2, side 3: ";         cin>>x>>y>>z;     }     double area()     {         double half, underroot;         half = perimeter()/2;         underroot = half*(half-x)*(half-y)*(half-z);         return sqrt(underroot);     }     double perimeter()         {return x+y+z;}     double volume()         {return 0;} };  class box: public shape { private:     double p; public:     box():p(1.0), shape(){}     box(double a, double b, double c): shape(a,b), p(c){}     void display();     void getdimensions()     {         cout<<"input box - "             <<"enter length, width, , height: ";         cin>>x>>y>>p;     }     double area()         {return (2*x*y)+(2*x*p)+(2*y*p);}     double perimeter()         {return 0;}     double volume()         {return x*y*p;} };  class can: public shape { public:     can():shape(){}     can(double r, double h):shape(r,h){}     void display();     void getdimensions()     {         cout<<"input can - "             <<"enter radius , height: ";         cin>>x>>y;     }     double area()     {         double base;         base = pi*x*x;         return (2*pi*x*y)+(2*pi*x*x);     }     double perimeter()         {return 0;}     double volume()         {return pi*x*x*y;} };  class cone: public shape { public:     cone():shape(){}     cone(double r, double h):shape(r, h){}     void display();     void getdimensions()     {         cout<<"input cone - "             <<"enter radius , height: ";         cin>>x>>y;     }     double area()     {         double underroot;         underroot = sqrt((x*x)+(y*y));         return (pi*x*x)+(pi*x*underroot);     }     double perimeter()         {return 0;}     double volume()         {return (1/3)*pi*x*x*y;} };  class ball: public shape { private:     double radius; public:     ball():radius(1.0){}     ball(double r):radius(r){}     void display();     void getdimensions()     {         cout<<"input ball - "             <<"enter radius: ";         cin>>radius;     }     double area()         {return 4*pi*radius*radius;}     double perimeter()         {return 0;}     double volume()         {return (4/3)*pi*radius*radius*radius;} };  int main() {     int cnt=0, cnt2=0, choice;     char yorn;      shape *sptr[20];      while(cnt<20)     {         cnt++;         cnt2++;         cout<<"\nit time enter shape selection"             <<" , dimensions.\n"             <<"enter number of shape type: \n"             <<"\t1 - rectangle\n"             <<"\t2 - circle\n"             <<"\t3 - triangle\n"             <<"\t4 - box\n"             <<"\t5 - can\n"             <<"\t6 - cone\n"             <<"\t7 - ball\n"             <<"=> ";         cin>>choice;         while (choice<1||choice>7)         {             cout<<"invalid selection. re-enter: \n"                 <<"=> ";             cin>>choice;         }         switch (choice)         {         case 1:         {             sptr[cnt]=new rectangle;             sptr[cnt]->getdimensions();             break;         }         case 2:         {             sptr[cnt]=new circle;             sptr[cnt]->getdimensions();             break;         }         case 3:         {             sptr[cnt] = new triangle;             sptr[cnt]->getdimensions();             break;         }         case 4:         {             sptr[cnt] = new box;             sptr[cnt]->getdimensions();             break;         }         case 5:         {             sptr[cnt] = new can;             sptr[cnt]->getdimensions();             break;         }         case 6:         {             sptr[cnt] = new cone;             sptr[cnt]->getdimensions();             break;         }         case 7:         {             sptr[cnt] = new ball;             sptr[cnt]->getdimensions();             break;         }         }          cout<<"\nselect shape? (y or n): ";         cin>>yorn;         if(yorn=='n'||yorn=='n')             cnt=20;     }     for(int i=0; i<cnt2; i++)     {         sptr[i]->display();     }     return 0; }  void rectangle::display() {     cout<<"\nrectangle: "         <<"\ndimensions: "         <<"length: "<<x<<"\nwidth: "<<y         <<"\narea: "<<area()         <<"\nperimeter: "<<perimeter(); }  void circle::display() {     cout<<"\ncircle: "         <<"\ndimensions: "         <<"\nradius: "<<radius         <<"\narea: "<<area()         <<"\nperimeter: "<<perimeter(); }  void triangle::display() {     cout<<"\ntriangle: "         <<"\ndimensions: "         <<"\nside 1: "<<x         <<"\nside 2: "<<y         <<"\nside 3: "<<z         <<"\narea: "<<area()         <<"\nperimeter: "<<perimeter(); }  void box::display() {     cout<<"\nbox: "         <<"\ndimensions: "         <<"\nlength: "<<x         <<"\nwidth: "<<y         <<"\nheight: "<<p         <<"\nsurface area: "<<area()         <<"\nvolume: "<<volume(); }  void can::display() {     cout<<"\nbox: "         <<"\ndimensions: "         <<"\nradius: "<<x         <<"\nheight: "<<y         <<"\nsurface area: "<<area()         <<"\nvolume: "<<volume(); }  void cone::display() {     cout<<"\ncone: "         <<"\ndimensions: "         <<"\nradius: "<<x         <<"\nheight: "<<y         <<"\nsurface area: "<<area()         <<"\nvolume: "<<volume(); }  void ball::display() {     cout<<"\nball: "         <<"\ndimensions: "         <<"\nradius: "<<radius         <<"\nsurface area: "<<area()         <<"\nvolume: "<<volume(); } 

you're displaying starting 0:

for(int i=0; i<cnt2; i++) {     sptr[i]->display(); } 

but you're adding sptr starting @ 1:

int cnt=0; while(cnt<20) {     cnt++;     ...     sptr[cnt] = new ...; } 

these kinds of errors great reason prefer use std::vector<shape*> raw arrays , counters. write following, less error-prone:

std::vector<shape*> shapes; while (shapes.size() < 20) {     ...     shapes.push_back(new rectangle);     ... }  (size_t = 0; < shapes.size(); ++i) {      shapes[i]->display(); } 

or really, in c++11, should vector<unique_ptr<shape>>.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -