Reputation: 61
I have figured out most of the code/assignment, here is part of the "To-Do" I can't figure out:
Here is my code (nothing in main can be changed):
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <string>
const double PI = 3.14159265359;
class Geometry
{
public:
Geometry(std::string name)
:m_name(name)
{
}
virtual double computeVolume() = 0;
virtual double computeSurfaceArea() = 0;
std::string getName() { return m_name; }
std::string getType() { return m_type; }
protected:
std::string m_name;
std::string m_type;
};
class Box : public Geometry
{
public:
Box(std::string name, double length, double width, double height)
: Geometry(name), m_length(length), m_width(width), m_height(height)
{
}
virtual double computeSurfaceArea()
{
return (2 * (m_height * m_width)) + (2 * (m_height * m_length)) + (2
* (m_length * m_width));
}
virtual double computeVolume()
{
return m_length * m_width * m_height;
}
protected:
double m_length;
double m_width;
double m_height;
};
class Sphere : public Geometry
{
public:
Sphere(std::string name, double radius)
:Geometry(name), m_radius(radius)
{
}
virtual double computeVolume()
{
return (4.0/3.0) * PI * (m_radius * m_radius * m_radius);
}
virtual double computeSurfaceArea()
{
return 4 * PI * (m_radius * m_radius);
}
protected:
double m_radius;
};
void report(Geometry* obj)
{
std::cout << "----- Geometry Report -----" << std::endl << std::endl
<< "Type: " << obj->getType() << std::endl
<< "Name: " << obj->getName() << std::endl
<< "Volume " << obj->computeVolume() << std::endl
<< "Surface Area " << obj->computeSurfaceArea() << std::endl <<
std::endl;
}
int main()
{
std::vector<Geometry*> items;
items.push_back(new Box("Box 1", 1, 2, 3));
items.push_back(new Box("Box 2", 2, 3, 4));
items.push_back(new Sphere("Sphere 1", 5));
items.push_back(new Sphere("Sphere 2", 6));
for (int i = 0; i < items.size(); i++)
{
report(items[i]);
}
system("PAUSE");
return 0;
}
Thanks
Upvotes: 3
Views: 277
Reputation: 159
A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.
So you can directly set m_type from derived class.
Example::
class Box {
protected:
double width;
};
class SmallBox:Box { // SmallBox is the derived class.
public:
void setSmallWidth( double wid );
double getSmallWidth( void );
};
// Member functions of child class
double SmallBox::getSmallWidth(void) {
return width ;
}
void SmallBox::setSmallWidth( double wid ) {
width = wid;
}
// Main function for the program
int main() {
SmallBox box;
// set box width using member function
box.setSmallWidth(5.0);
cout << "Width of box : "<< box.getSmallWidth() << endl;
return 0;
}
In your case It may be used like this
Box(std::string name, double length, double width, double height)
: Geometry(name), m_length(length), m_width(width), m_height(height)
{
m_type="abc";
}
Upvotes: 1
Reputation: 1133
If I understand your question correctly then you just need to set the m_type
variable directly within the constructor of each of your derived classes. You are able to do this because this variable has protected
access which means derived classes can manipulate it directly. So you should simply be able to do:
Box(std::string name, double length, double width, double height)
: Geometry(name), m_length(length), m_width(width), m_height(height)
{
m_type = "Box type";
}
Upvotes: 2