// 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;
};
5 1186
I cleaned it up a bit so maybe its easier to find.
Shapes.h -
#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 -
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();
-
}
-
}
-
}
-
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.
Then do this: - Shape* s = new Circle(5);
From here all access to the Circle must use the base class:
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.
Does any of this help?
Wow! Thank you so much! That actually makes a lot of sense now. And the error is gone!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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:
...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |