C ++继承不继承

时间:2015-02-18 07:47:41

标签: c++ inheritance compiler-errors

rectangleType.h

class rectangleType
{
public:
    void setDimension(double l, double w);
      //Function to set the length and width of the rectangle.
      //Postcondition: length = l; width = w;

    double getLength() const;
      //Function to return the length of the rectangle.
      //Postcondition: The value of length is returned. 

    double getWidth() const;
      //Function to return the width of the rectangle.
      //Postcondition: The value of width is returned. 

    double area() const;
      //Function to return the area of the rectangle.
      //Postcondition: The area of the rectangle is 
      //               calculated and returned.

    double perimeter() const;
      //Function to return the perimeter of the rectangle.
      //Postcondition: The perimeter of the rectangle is 
      //               calculated and returned.

    void print() const;
      //Function to output the length and width of 
      //the rectangle.

    rectangleType();
      //Default constructor
      //Postcondition: length = 0; width = 0;

    rectangleType(double l, double w);
      //Constructor with parameters
      //Postcondition: length = l; width = w;

private:
    double length;
    double width;
};

rectangleType.cpp

#include<iostream>
#include"rectangleType.h"

using namespace std;

void rectangleType:: setDimension(double l, double w){
    length = l;
    width = w;
}

double rectangleType:: getLength() const{
    return length;
}

double rectangleType:: getWidth() const{
    return width;
}

double rectangleType:: area() const{
    return (length*width);
}

double rectangleType:: perimeter() const{
    return ((length*2)+(width*2));
}

void rectangleType:: print() const{
    cout << "the width is: " << width << endl;
    cout << "the length is: " << length << endl;
}

rectangleType:: rectangleType(){
    length = 0;
    width = 0;
}

rectangleType:: rectangleType(double l, double w){
    length = l;
    width = w;
}

boxType.h

class boxType : public rectangleType
{
public:
    void setDimension(double l, double w, double h);
      //Function to set the length, width, and height 
      //of the box.
      //Postcondition: length = l; width = w; height = h;

    double getHeight() const;
      //Function to return the height of the box.
      //Postcondition: The value of height is returned. 

    double area() const;
      //Function to return the surface area of the box.
      //Postcondition: The surface area of the box is 
      //                calculated and returned.

    double volume() const;
      //Function to return the volume of the box. 
      //Postcondition: The volume of the box is 
      //               calculated and returned.

    void print() const;
      //Function to output the length, width, and height of a box.

    boxType();
      //Default constructor
      //Postcondition: length = 0; width = 0; height = 0;

    boxType(double l, double w, double h);
      //Constructor with parameters
      //Postcondition: length = l; width = w; height = h;

private:
    double height;
};

boxType.cpp

#include<iostream>
#include"boxType.h"
#include"rectangleType.h"

using namespace std;

void boxType:: setDimension(double l, double w, double h){
    length = l;
    width = w;
    height = h;
}

double boxType:: getHeight() const{
    return height;
}

double boxType:: area() const{
    return (length*width*height);
}

double boxType:: volume() const{
    return ((2*length*width)+(2*length*height)+(2*width*height))
}

void boxType:: print() const{
    cout << "the width is: " << width << endl;
    cout << "the length is: " << length << endl;
    cout << "the height is: " << height << endl;
}

boxType:: boxType() : rectangleType.cpp(){
    height = 0;
}

boxType:: boxType(double l, double w, double h) : rectangleType.cpp(double l, double w){
    height = h;
}

RectangleTypeTest.cpp

#include<iostream>
#include"rectangleType.h"
#include"boxType.h"

using namespace std;

int main(){

    rectangleType firstRectangle;
    rectangleType secondRectangle(2, 2);

    firstRectangle.setDimension(3, 3);
    cout << "rectangle's length is: " << firstRectangle.getLength() << endl;
    cout << "rectangle's width is: " << firstRectangle.getWidth() << endl;
    cout << "rectangle's area is: " << firstRectangle.area() << endl;
    cout << "rectangle's perimeter is: " << firstRectangle.perimeter() << endl;
    secondRectangle.print();

    boxType firstBox;
    boxType secondBox(2, 2, 2);
    firstBox.setDimension(3, 3, 3);
    cout << "box's length is: " << firstBox.getLength() << endl;
    cout << "box's width is: " << firstBox.getWidth() << endl;
    cout << "box's height is: " << firstBox.getHeight() << endl;
    cout << "box's area is: " << firstBox.area() << endl;
    cout << "box's volume is: " << firstBox.volume() << endl;
    secondBox.print();


}

我正在编译:

g++ -o rectangleTest rectangleTypeTest.cpp rectangleType.cpp boxType.cpp

我收到错误消息:

In file included from boxType.cpp:2:0:
boxType.h:2:1: error: expected class-name before ‘{’ token
 {
 ^
boxType.cpp: In member function ‘void boxType::setDimension(double, double, double)’:
boxType.cpp:8:2: error: ‘length’ was not declared in this scope
  length = l;
  ^
boxType.cpp:9:2: error: ‘width’ was not declared in this scope
  width = w;
  ^
boxType.cpp: In member function ‘double boxType::area() const’:
boxType.cpp:18:10: error: ‘length’ was not declared in this scope
  return (length*width*height);
          ^
boxType.cpp:18:17: error: ‘width’ was not declared in this scope
  return (length*width*height);
                 ^
boxType.cpp: In member function ‘double boxType::volume() const’:
boxType.cpp:22:13: error: ‘length’ was not declared in this scope
  return ((2*length*width)+(2*length*height)+(2*width*height))
             ^
boxType.cpp:22:20: error: ‘width’ was not declared in this scope
  return ((2*length*width)+(2*length*height)+(2*width*height))
                    ^
boxType.cpp:23:1: error: expected ‘;’ before ‘}’ token
 }
 ^
boxType.cpp: In member function ‘void boxType::print() const’:
boxType.cpp:26:30: error: ‘width’ was not declared in this scope
  cout << "the width is: " << width << endl;
                              ^
boxType.cpp:27:31: error: ‘length’ was not declared in this scope
  cout << "the length is: " << length << endl;
                               ^
boxType.cpp: In constructor ‘boxType::boxType()’:
boxType.cpp:31:23: error: type ‘rectangleType’ is not a direct base of ‘boxType’
 boxType:: boxType() : rectangleType(){
                       ^
boxType.cpp: In constructor ‘boxType::boxType(double, double, double)’:
boxType.cpp:35:51: error: type ‘rectangleType’ is not a direct base of ‘boxType’
 boxType:: boxType(double l, double w, double h) : rectangleType(double l, double w){
                                                   ^
boxType.cpp:35:65: error: expected primary-expression before ‘double’
 boxType:: boxType(double l, double w, double h) : rectangleType(double l, double w){
                                                                 ^
boxType.cpp:35:75: error: expected primary-expression before ‘double’
 boxType:: boxType(double l, double w, double h) : rectangleType(double l, double w){
                                                                       ^

此错误似乎主要未在此范围错误中声明。在我看来,它不是继承自rectangleType。不确定我做错了什么。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

多个错误

  1. 必须保护您希望继承的类可访问的变量; 私有。
  2. 您想要“多态”调用的函数必须是虚拟的;除非你试图创建碰巧具有相同名称的类特定函数;在这种情况下,如果你有一个基类指针,无论指向的abject是框还是矩形,都会调用基类函数。
  3. 如果这是作业,那么这是一个不好的例子。框不是一种矩形。 “是类型”是在定义类层次结构时要遵循的一种规则。例如盒子的面积是多少?有点无意义。