Instantiating an abstract class error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BrendonD3OT
    New Member
    • May 2016
    • 4

    Instantiating an abstract class error

    // 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(doubl e 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*hei ght) + (2 * 3.14*(radius*ra dius)));
    }
    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*ra dius));
    }
    void expand(int factor)
    {
    radius = radius * factor;
    }
    void display()
    {
    cout << "Sphere: (radius = " << radius << " )\n";
    }
    private:
    double radius, volume;
    };
  • BrendonD3OT
    New Member
    • May 2016
    • 4

    #2
    Fixed Formating

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


    Shapes.h
    Code:
    #ifndef SHAPE_H
    #define SHAPE_H
    
    #include <string>
    #include <iostream>
    #include <vector>
    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
    Code:
    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<Shape> 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();
    		}
    	}
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Read this: https://bytes.com/topic/c/insights/7...polymorphism-c

      Then post again. I didn't want to repeat the article here.

      Comment

      • BrendonD3OT
        New Member
        • May 2016
        • 4

        #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.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Then do this:

          Code:
          Shape* s = new Circle(5);
          From here all access to the Circle must use the base class:

          Code:
          s->computeArea();
          This is a call to Circle::compute Area(). The virtual keyword tells the compiler that if there is a choice between Shape::computeA rea() and Circle::compute Area() 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.


          Code:
          vector<Shape*> arr;
          Does any of this help?

          Comment

          • BrendonD3OT
            New Member
            • May 2016
            • 4

            #6
            Wow! Thank you so much! That actually makes a lot of sense now. And the error is gone!

            Comment

            Working...