以前的'类形'宣言。基类未正确声明?

时间:2016-12-11 14:27:11

标签: c++ class inheritance subclass declaration

每次尝试编译shape.h文件时,我都会收到此错误消息:

  

以前的'类形'宣言。基类未正确声明?

shape.h头文件:

#include <iostream>
/*
  1. class Shape will have virtual functions called area() and perimeter()
  2. derived classes will encapsulate dimensions of the sides or radius depending on class used
  3. derived classes will have default constructors as well as overloaded constructors to initialize dimensional values
  4. each derived class will have own area and perimeter functions that will be used in polymorphic manner
  5. write class headers, implementations, and test files separately
*/
class shape //parent class
{

public:
  shape();
  virtual int area();
  virtual int perimeter();
};

这是父类shape.cpp

的实现
#include "shape.h"
#include<iostream>
using namespace std;
shape::shape(){}

这是形状rectangle.h的派生类声明:

#include "shape.h"

class rectangle: public shape
{
public: 
  rectangle();//constructor
  rectangle(int l, int w ); //stands for length and width
  int perimeter(int, int); //function shared w/shape parent class
  int area(int, int);    //function shared w/shape parent class

private:
  int longside, wideside;           
};    

这是矩形类rectangle.cpp的实现:

#include "rectangle.h"

rectangle::rectangle(){}

rectangle::rectangle(int l, int w)
{
  longside = l;

  wideside=w;
};

int rectangle::area(int wideside, int longside)
{
  int totarea;
  totarea = (wideside*longside);

  return totarea;
};

int rectangle::perimeter(int side1, int side2)
{
  int perim = ((wideside)+(longside));
  return perim;
};

这是测试文件testfile.cpp

#include <iostream>
#include "shape.h"
#include "rectangle.cpp"
using namespace std;
int main()
{
  rectangle rect1;  
  int side1, side2;
  cout<<"\n Find Area of an Rectangle" << endl;
  cout<<"\nChoose a length for sides 1 abd 2" << endl;
  cout<<"\nSide 1: "<< endl;
  cin >> side1;
  cout<<"\nSide 2: "<< endl;

  cout<<"\nRectangle's Area is:   "<< rect1.area( side1, side2 ) << endl;
  cout<<"\nRectangle's Perimeter is:   "<<rect1.perimeter( side1, side2 )<< endl;

  return 0;
}

类形状是其他形状对象的基类。我只是在一个名为class rectangle的派生类中测试功能。每次定义一个类时,我不应该创建头文件吗?我在这个程序中包含了我的所有文件,看看是否有帮助。

0 个答案:

没有答案