By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 431,650 Members | 753 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

# Instantiating an abstract class error

 P: 4 // Im having a lot of trouble with this and i've spent hours trying to figure out how to fix it. //Do any of you know which abstract class is being instantiated? //VS says its the Shape class but i don't see whats wrong class Shape { public: Shape() {} virtual double computeArea() = 0; virtual void expand(int factor) = 0; virtual void display() = 0; virtual ~Shape() {} //other member functions if you want to add; }; class Circle: public Shape { public: Circle(double r); double computeArea() { return (3.14 * (radius * radius)); } void expand(int factor) { radius = radius * factor; } void display() { cout << "Circle: (radius = " << radius << " )\n"; } private: double radius; }; class Rectangle: public Shape { public: Rectangle(double wid, double len); double computeArea() { return (length * width); } void expand(int factor) { width = width * factor; length = length * factor; } void display() { cout << "Rectangle: (length = " << length << ", width = " << width << " )\n"; } private: double width, length; }; class Cuboid: public Shape { public: Cuboid(double wid, double len, double hei); void computeVolume(); double computeArea() { return ((2 * width*length) + (2 * width*height) + (2 * length*height)); } void expand(int factor) { width = width * factor; length = length * factor; height = height * factor; } void display() { cout << "Cuboid: (length = " << length << ", width = " << width << ", height = " << height << " )\n"; } private: double width, length, height, volume; }; class Cylinder: public Shape { public: Cylinder(double r, double hei); void computeVolume(); double computeArea() { return ((2 * 3.14*radius*height) + (2 * 3.14*(radius*radius))); } void expand(int factor) { radius = radius * factor; height = height * factor; } void display() { cout << "Cylinder: (height = " << height << ", radius = " << radius << " )\n"; } private: double radius, height, volume; }; class Sphere: public Shape { public: Sphere(double r); void computeVolume(); double computeArea() { return (4 * 3.14*(radius*radius)); } void expand(int factor) { radius = radius * factor; } void display() { cout << "Sphere: (radius = " << radius << " )\n"; } private: double radius, volume; }; May 3 '16 #1
Share this Question
5 Replies

 P: 4 I cleaned it up a bit so maybe its easier to find. Shapes.h Expand|Select|Wrap|Line Numbers #ifndef SHAPE_H #define SHAPE_H   #include  #include  #include  using namespace std;   class Shape { public:     Shape() {}     virtual double computeArea() = 0;     virtual void expand(int factor) = 0;     virtual void display() = 0;     virtual ~Shape() {}     //other member functions if you want to add; };   class Circle: public Shape { public:     Circle(double r);     double computeArea();     void expand(int factor);     void display(); private:     double radius; };   class Rectangle: public Shape { public:     Rectangle(double wid, double len);     double computeArea();     void expand(int factor);     void display(); private:     double width, length; };   class Cuboid: public Shape { public:     Cuboid(double wid, double len, double hei);     void computeVolume();     double computeArea();     void expand(int factor);     void display(); private:     double width, length, height, volume; };   class Cylinder: public Shape { public:     Cylinder(double r, double hei);     void computeVolume();     double computeArea();     void expand(int factor);     void display(); private:     double radius, height, volume; };   class Sphere: public Shape { public:     Sphere(double r);     void computeVolume();     double computeArea();     void expand(int factor);     void display(); private:     double radius, volume; };   #endif   and Source.cpp Expand|Select|Wrap|Line Numbers while (fail<3) {     cout << "Please type the file name for the shape information..." << endl;     cin >> file;     myfile.open(file);     if (!myfile.is_open())     {         cout << "Open file with name " << file << ": failure";         cin.clear();         fail++;     }     else     {         vector list;         while (getline(myfile, line))         {             int i = 0;             while(i < line.length())             {                 if (line[i] != ' ')                 {                     type = type + line[i];                     i++;                 }                 else break;                 }             if (type == "Rectangle")             {                 getline(myfile, temp1);                 tempd1 = stod(temp1, &sz);                 getline(myfile, temp2);                 tempd2 = stod(temp2, &sz);                 list.push_back(Rectangle(tempd1, tempd2));             }             else if (type == "Circle")             {                 getline(myfile, temp1);                 tempd1 = stod(temp1, &sz);                 list.push_back(Circle(tempd1));             }             else if (type == "Cylinder")             {                 getline(myfile, temp1);                 tempd1 = stod(temp1, &sz);                 getline(myfile, temp2);                 tempd2 = stod(temp2, &sz);                 list.push_back(Cylinder(tempd1, tempd2));             }             else if (type == "Cuboid")             {                 getline(myfile, temp1);                 tempd1 = stod(temp1, &sz);                 getline(myfile, temp2);                 tempd2 = stod(temp2, &sz);                  getline(myfile, temp3);                 tempd3 = stod(temp3, &sz);                 list.push_back(Cuboid(tempd1, tempd2, tempd3));             }             else if (type == "Sphere")             {                 getline(myfile, temp1);                 tempd1 = stod(temp1, &sz);                 list.push_back(Sphere(tempd1));             }         }         for (int j = 0; j < list.size(); j++)         {             list[j].display();         }     } }   May 3 '16 #2

 Expert Mod 5K+ P: 9,197 Read this: https://bytes.com/topic/c/insights/7...polymorphism-c Then post again. I didn't want to repeat the article here. May 3 '16 #3

 P: 4 The only problem is my base class Shape must be written that way. It is the only piece of code I was given to build the program and cannot change it. The article doesn't adress any way to complete this with using the original Shape class. May 3 '16 #4

 Expert Mod 5K+ P: 9,197 Then do this: Expand|Select|Wrap|Line Numbers Shape* s = new Circle(5); From here all access to the Circle must use the base class: Expand|Select|Wrap|Line Numbers s->computeArea(); This is a call to Circle::computeArea(). The virtual keyword tells the compiler that if there is a choice between Shape::computeArea() and Circle::computeArea() use the derived class rather than the base class. The compile knows this is a Circle because the object was created as a Circle. I didn't see any use of base class pointers in your sample code. BTW: In your code I saw a vector. This is a no-no. Shape is an abstract base class (has at least one pure virtual function) and that means you can't create objects of type Shape. Instead you create derived objects and store them in the vector as Shape pointers. Expand|Select|Wrap|Line Numbers vector arr; Does any of this help? May 3 '16 #5

 P: 4 Wow! Thank you so much! That actually makes a lot of sense now. And the error is gone! May 3 '16 #6

### Post your reply

Sign in to post your reply or Sign up for a free account.