473,503 Members | 12,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instantiating an abstract class error

4 New Member
// 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
5 1186
BrendonD3OT
4 New Member
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
9,208 Recognized Expert Moderator Expert
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
BrendonD3OT
4 New Member
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
9,208 Recognized Expert Moderator Expert
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
BrendonD3OT
4 New Member
Wow! Thank you so much! That actually makes a lot of sense now. And the error is gone!
May 3 '16 #6

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

Similar topics

15
2379
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
2
7097
by: Robb Sadler | last post by:
I am trying to write several hardware interfaces that would use the same base class and functions and be implemented differently. I don't want to ship all of the interfaces, but want to access...
6
5788
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
7
1496
by: Ornez | last post by:
Hi, My customer has an abstract class with some interface A . he wants me to Inherit his Class and implement the intrface with some functionality in a new class B. I need to know how he would...
9
5482
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
1
1501
by: Bruce | last post by:
public ref class GpsDevice abstract { /* Please note GarXface4::GpsDevice* is in a different namespace from this one and GarXface4::GpsDevice is an unmanaged class */ virtual...
6
2296
by: curious2007 | last post by:
So here is how I can summarize my situation. In function.hpp I have the following function: virtual void accept(FunctionVisitor<D,R,I>& v) = 0; And in detfunction.hpp I have the following: ...
19
2345
by: sugard | last post by:
I need to access some data which is in an abstract class. Though you cannot inistialise an abstract class. The task to do is gathering data from a textfile where data is in this format.. P...
21
3901
by: Mr.SpOOn | last post by:
Hi, I'm going to work on a project to represent some musical theory in Python, in an object oriented way. I have to manage many elements of music such as notes, intervals, scales, chords and so...
4
4370
by: dascandy | last post by:
Hi, For a project I'm working on I'm kind-of-hacking my way around deriving a class from an interface or such to create a mock, but instead creating the mock directly. It is usable as the...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.