bobbyjoe bobbyjoe - 1 month ago 11
C++ Question

Virtual Functions and Polymorphism - unable to declare object in test class

I've created a program that uses virtual functions and polymorphism to calculate the parameter and area for three different objects: circles, rectangles and right angled triangles. Whenever I try to assign one of the different object classes to the pointer I have in my test class, it says 'Error: expected a type specifier':

shape_ptr = new Rectangle;


Screenshot of error shown

I'm almost sure it's something really simple that I've missed but it's not the inclusion of the header file as I've done that on each class without any mistakes that I can see. Here is my code:

Base Class:

#ifndef SHAPE
#define SHAPE
#include <iostream>
using namespace std;
class Shape {
public:
virtual void compute_area() = 0; // a pure virtual function
virtual void compute_perimeter() = 0; // a pure virtual function
virtual void read_shape_data() = 0; // a pure virtual function
virtual void print_result() { // a virtual function
cout << "The area is " << area << endl;
cout << "The perimeter is " << perim << endl;
}
protected: // protected access specifier
double area, perim;
};


#endif


circle.cpp:

#include "shape.h"
#include <iostream>
using namespace std;
class Circle : public Shape
{
public:
void compute_area() { area = pi * radius; }
void compute_perimeter() { perim = 2 * pi * radius; }
void read_shape_data() {
cout << "Enter radius of the rectangle : ";
cin >> radius;
}
private:
int radius;
double pi = 3.14159265359;
};


rectangle.cpp:

#include "shape.h"
#include <iostream>
using namespace std;
class Rectangle : public Shape
{
public:
void compute_area() { area = width * height; }
void compute_perimeter() { perim = 2 * width + 2 * height; }
void read_shape_data() {
cout << "Enter width of the rectangle : ";
cin >> width;
cout << "Enter height of the rectangle : ";
cin >> height;
}
private:
int width, height;
};


RightTriangle.cpp:

#include "shape.h"
#include <iostream>
using namespace std;
class RightTriangle : public Shape
{
public:
void compute_area() { area = base * height; }
void compute_perimeter() { perim = pow((pow(base, 2) * pow(height, 2)), 2); }
void read_shape_data() {
cout << "Enter base length of triangle : ";
cin >> base;
cout << "Enter height of triangle : ";
cin >> height;
cout <<
}
private:
int radius, base, height;
};


test.cpp (test class):

#include "shape.h"
#include <iostream>
using namespace std;

int main(){
int choice;
Shape* shape_ptr = NULL;
cout << "Enter 1 for circle, 2 for rectangle, 3 for right angled triangle or 0 for exit";
cin >> choice;
switch (choice){
case 1:
shape_ptr = new Rectangle;
break;
}

shape_ptr->read_shape_data();
shape_ptr->compute_area();
shape_ptr->compute_perimeter();
shape_ptr->print_result();
delete shape_ptr;

return 0;
}


Thanks for your time and I'm happy to answer any questions.

Answer

The "shape.h" header doesn't automatically know about the definitions of derived classes like Rectangle, so your test file needs to include those headers too:

#include "shape.h"
#include "rectangle.h"
// etc.
#include <iostream>

It looks like you defined your derived classes within .cpp files. Move those declarations into header files, and define them in the .cpp file instead.