C++ linked list/polymorphism not running list function -
i have make class shape subclasses specific shapes find volume. have able construct objects , store them in linked list. testing creating 1 object , wondering why not run function cout commands. appreciated. thanks
#include <iostream> #include <array> using namespace std; class shape{ public: friend class my_list; shape(double a=0,double b=0 ,double c=0):width(a),height(b),length(c){ } protected: double width; double height; double length; shape *nextptr; }; class rectangle:public shape { public: rectangle(double a, double b,double c):shape(a,b,c){ area = width * length; }; private: double area; }; class circle:public shape { public: circle(double a):shape(a){ area = (radius*radius)*3.14; } protected: double area; double radius; }; class triangle:public shape { public: triangle(double a, double b,double c):shape(a,b,c){ area = .5*width*length; }; protected: double area; }; class cubic:public rectangle { public: cubic(double a, double b,double c):rectangle(a,b,c){ volume = width * length * height; }; protected: double volume; }; class sphere:public circle { public: sphere(double a):circle(a){ volume = (width*width*width)*(4/3)*3.14; }; protected: double volume; }; class cone:public circle{ public: cone(double a, double b):circle(a){ height = b; volume = 3.14*(width*width)*(height/3); }; protected: double volume; }; class my_list{ public: my_list(); ~my_list(); void add_node(shape*); void print_list(); shape* make_node(); private: shape* firstptr; shape* lastptr; }; my_list::my_list():firstptr(null), lastptr(null){} my_list::~my_list(){} shape* my_list::make_node(){ cout << "what shape construct?" << endl; cout << "\t1. rectangle" << "\n\t2. triangle" << "\n\t3. cirlce" << "\n\t4. cubic" <<"\n\t5. sphere" << "\n\t6. cone" << endl; int number; cin >> number; cin.get(); cout << "enter width/height/length or radius, or radius/height" << endl; double a,b,c; cin >> a; cin>>b; cin>> c; switch (number) { case 1: { shape *newptr = new rectangle(a,b,c); return newptr; } case 2: { shape *newptr = new triangle(a,b,c); return newptr; } case 3: { shape *newptr = new circle(a); return newptr; } case 4: { shape *newptr = new sphere(a); return newptr; } case 5: { shape *newptr = new cubic(a,b,c); return newptr; } case 6:{ shape *newptr = new cone(a,b); return newptr; } default: return 0; } } void my_list::add_node(shape* new_node){ if ( firstptr == null ) // empty list firstptr = lastptr = new_node; // nextptr = null default else { new_node -> nextptr = firstptr; firstptr = new_node; // set first pointer beginning of list; } } int main(){ /* while(true) { cout << endl << endl << "\tselect following options" << endl; cout << "\t1. construct" << "\n\t2. add" << "\n\t3. print" << "\n\t4. quit" << endl; cout << "\tselection:\t"; int options; cin >> options; cin.get(); // cin leaves newline, cin.get() cancel switch (options) { case 1: //construct function case 2: { //add function } case 3: //print function case 4: default: break; } } */ my_list l1; l1.add_node(l1.make_node()); }
few pointers :
try creating object of my_list before calling functions on . my_list l1 = new my_list();
in switch-case usage use break instead of return. like:
switch (n){ case 1: ... break;
case 2: ... break; }
3.start using ide , add break points debugging.
Comments
Post a Comment