473,657 Members | 2,572 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(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;
};
May 3 '16 #1
5 1197
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::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.


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
2404
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 inheriting this base usercontrol to override the method with its own code. How do I do it? I have tried to make the base usercontrol an abstract class (mustinherits + mustoverrides), this works, but all the usercontrols that inherit it will not able...
2
7107
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 them using the same generic base class. I have seen that I can use the Assembly.load method to load the implementation I need for the hardware being connected to, but wish to write the code such that I can reference the methods of the class...
6
5798
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 the need to have abstract class types in C#? Derived classes from abstract base classes must overrided the abstract method defininition anyway, so why not just provide a complete definition for the abstract method when defining the containing...
7
1500
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 access my implementation without a Reference . Can a base class generates his derived classes ? Like when i will use some method it will be happen in others derived classes ?
9
5496
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 FrameWork.cpp I define some of the methods while I naturally leave the abstract methods undefined. Now I wrote a class FrameWork_GUI.h which inherits from the abstract FrameWork class and implements the missing (so far abstract) methods....
1
1513
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 GarXface4::GpsDevice* GetDevice() = 0; };
6
2304
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: template <class D, class R> class DFunction: public Function <D, R> {...} template <class D, class R> class AtomicDFunction : public DFunction<D, R> {...}
19
2370
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 Charles Galea 134 Where my class needs to check if the first letter is a P it do the stuff in the professors class and if it is an S it do the stuff in the Students class. The professor and student class inherits the SchoolMember abstract class which...
21
3927
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 on. All these elements share properties and behavior, so what I want to do is an abstract class "Note" and other subclasses, for example "NaturalNote", "FlatNote", "SharpNote" etc. The idea is not original, I read it in some papers where they...
4
4386
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 interface type (as the virtual functions match up), but there is a snag. No members are initialized, which means it can't be used as the original type if it has anything but virtual functions. It would be possible to make it work, if I could call the...
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.