Ok. I have a start on the program based off of the tutorial linked by MadCow257. However, when I compile, it gives me errors. Heres the code so far:
Code:
// dynamic allocation and polymorphism
#include <iostream>
using namespace std;
class Shape
{
protected:
int width, height, length;
public:
void set_values (int a, int b, int c)
{
width=a; height=b, length=c;
}
virtual int area (void) =0;
void printarea (void)
{
cout << this->area() << endl;
}
virtual int volume(void) =0;
void printvolume(void)
{
cout << this->volume() << endl;
}
};
class TwoDimensionalShape: public Shape
{
public:
int area(void)
{
return (width * height);
}
};
class ThreeDimensionalShape: public Shape
{
public:
int volume(void)
{
return (width * height * length);
}
};
int main()
{
Shape *ppoly1 = new TwoDimensionalShape;
Shape *ppoly2 = new ThreeDimensionalShape;
ppoly1->set_values (4,5,0);
ppoly2->set_values (4,5,6);
ppoly1->printarea();
ppoly2->printvolume();
delete ppoly1;
delete ppoly2;
return 0;
}
I get the following errors:
error C2259: 'TwoDimensionalShape' : cannot instantiate abstract class due to following members:
see declaration of 'TwoDimensionalShape'
warning C4259: 'int __thiscall Shape::volume(void)' : pure virtual function was not defined
And there are a few more warnings and errors that are basically the same, and they point to the various Class and Function definitions.
As to why im 'tutoring' when I don't have a full grasp of the material myself... Well, tutoring was the wrong word. Its more I show them how to do things as I figure them out myself, and help them do assignments(Note: I don't do assignments for others, I just show them how to get started). I enjoy doing this stuff, and these excersices are good practice.