虚函数和多态性 - 无法在测试中声明对象 class
Virtual Functions and Polymorphism - unable to declare object in test class
我创建了一个程序,该程序使用虚函数和多态性来计算三种不同对象的参数和面积:圆形、矩形和直角三角形。每当我尝试将不同对象之一 classes 分配给我在测试 class 中的指针时,它会显示 'Error: expected a type specifier':
shape_ptr = new Rectangle;
截图of error shown
我几乎可以肯定这是我错过的非常简单的事情,但它不是头文件的包含,因为我在每个 class 上都这样做了,没有任何我能看到的错误。这是我的代码:
基础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(测试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;
}
感谢您抽出时间,我很乐意回答任何问题。
"shape.h" header 不会自动知道派生 类 的定义,例如 Rectangle
,因此您的测试文件需要包含那些 header也是:
#include "shape.h"
#include "rectangle.h"
// etc.
#include <iostream>
您似乎在 .cpp 文件中定义了派生的 类。将那些 声明 移动到 header 文件中,然后在 .cpp 文件中定义它们。
我创建了一个程序,该程序使用虚函数和多态性来计算三种不同对象的参数和面积:圆形、矩形和直角三角形。每当我尝试将不同对象之一 classes 分配给我在测试 class 中的指针时,它会显示 'Error: expected a type specifier':
shape_ptr = new Rectangle;
截图of error shown
我几乎可以肯定这是我错过的非常简单的事情,但它不是头文件的包含,因为我在每个 class 上都这样做了,没有任何我能看到的错误。这是我的代码:
基础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(测试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;
}
感谢您抽出时间,我很乐意回答任何问题。
"shape.h" header 不会自动知道派生 类 的定义,例如 Rectangle
,因此您的测试文件需要包含那些 header也是:
#include "shape.h"
#include "rectangle.h"
// etc.
#include <iostream>
您似乎在 .cpp 文件中定义了派生的 类。将那些 声明 移动到 header 文件中,然后在 .cpp 文件中定义它们。