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
Bytes IT Community
+ 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
Share on Google+
5 Replies


P: 4
I cleaned it up a bit so maybe its easier to find.


Shapes.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef SHAPE_H
  2. #define SHAPE_H
  3.  
  4. #include <string>
  5. #include <iostream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. class Shape
  10. {
  11. public:
  12.     Shape() {}
  13.     virtual double computeArea() = 0;
  14.     virtual void expand(int factor) = 0;
  15.     virtual void display() = 0;
  16.     virtual ~Shape() {}
  17.     //other member functions if you want to add;
  18. };
  19.  
  20. class Circle: public Shape
  21. {
  22. public:
  23.     Circle(double r);
  24.     double computeArea();
  25.     void expand(int factor);
  26.     void display();
  27. private:
  28.     double radius;
  29. };
  30.  
  31. class Rectangle: public Shape
  32. {
  33. public:
  34.     Rectangle(double wid, double len);
  35.     double computeArea();
  36.     void expand(int factor);
  37.     void display();
  38. private:
  39.     double width, length;
  40. };
  41.  
  42. class Cuboid: public Shape
  43. {
  44. public:
  45.     Cuboid(double wid, double len, double hei);
  46.     void computeVolume();
  47.     double computeArea();
  48.     void expand(int factor);
  49.     void display();
  50. private:
  51.     double width, length, height, volume;
  52. };
  53.  
  54. class Cylinder: public Shape
  55. {
  56. public:
  57.     Cylinder(double r, double hei);
  58.     void computeVolume();
  59.     double computeArea();
  60.     void expand(int factor);
  61.     void display();
  62. private:
  63.     double radius, height, volume;
  64. };
  65.  
  66. class Sphere: public Shape
  67. {
  68. public:
  69.     Sphere(double r);
  70.     void computeVolume();
  71.     double computeArea();
  72.     void expand(int factor);
  73.     void display();
  74. private:
  75.     double radius, volume;
  76. };
  77.  
  78. #endif
  79.  

and


Source.cpp
Expand|Select|Wrap|Line Numbers
  1. while (fail<3)
  2. {
  3.     cout << "Please type the file name for the shape information..." << endl;
  4.     cin >> file;
  5.     myfile.open(file);
  6.     if (!myfile.is_open())
  7.     {
  8.         cout << "Open file with name " << file << ": failure";
  9.         cin.clear();
  10.         fail++;
  11.     }
  12.     else
  13.     {
  14.         vector<Shape> list;
  15.         while (getline(myfile, line))
  16.         {
  17.             int i = 0;
  18.             while(i < line.length())
  19.             {
  20.                 if (line[i] != ' ')
  21.                 {
  22.                     type = type + line[i];
  23.                     i++;
  24.                 }
  25.                 else break;
  26.                 }
  27.             if (type == "Rectangle")
  28.             {
  29.                 getline(myfile, temp1);
  30.                 tempd1 = stod(temp1, &sz);
  31.                 getline(myfile, temp2);
  32.                 tempd2 = stod(temp2, &sz);
  33.                 list.push_back(Rectangle(tempd1, tempd2));
  34.             }
  35.             else if (type == "Circle")
  36.             {
  37.                 getline(myfile, temp1);
  38.                 tempd1 = stod(temp1, &sz);
  39.                 list.push_back(Circle(tempd1));
  40.             }
  41.             else if (type == "Cylinder")
  42.             {
  43.                 getline(myfile, temp1);
  44.                 tempd1 = stod(temp1, &sz);
  45.                 getline(myfile, temp2);
  46.                 tempd2 = stod(temp2, &sz);
  47.                 list.push_back(Cylinder(tempd1, tempd2));
  48.             }
  49.             else if (type == "Cuboid")
  50.             {
  51.                 getline(myfile, temp1);
  52.                 tempd1 = stod(temp1, &sz);
  53.                 getline(myfile, temp2);
  54.                 tempd2 = stod(temp2, &sz); 
  55.                 getline(myfile, temp3);
  56.                 tempd3 = stod(temp3, &sz);
  57.                 list.push_back(Cuboid(tempd1, tempd2, tempd3));
  58.             }
  59.             else if (type == "Sphere")
  60.             {
  61.                 getline(myfile, temp1);
  62.                 tempd1 = stod(temp1, &sz);
  63.                 list.push_back(Sphere(tempd1));
  64.             }
  65.         }
  66.         for (int j = 0; j < list.size(); j++)
  67.         {
  68.             list[j].display();
  69.         }
  70.     }
  71. }
  72.  
May 3 '16 #2

weaknessforcats
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

weaknessforcats
Expert Mod 5K+
P: 9,197
Then do this:

Expand|Select|Wrap|Line Numbers
  1. Shape* s = new Circle(5);
From here all access to the Circle must use the base class:

Expand|Select|Wrap|Line Numbers
  1. 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<Shape>. 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
  1. vector<Shape*> 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.