// 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;
};
5 1197
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::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.
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 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...
|
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...
|
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...
|
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 ?
|
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....
| |
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;
};
|
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>
{...}
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |