C++ C++;继承而非继承

C++ C++;继承而非继承,c++,inheritance,compiler-errors,C++,Inheritance,Compiler Errors,矩形型 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 le

矩形型

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;
};
矩形类型.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.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;
}
我收到错误消息:

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){
                                                                       ^
boxType.cpp中包含的文件中的
:2:0:
h:2:1:错误:在“{”标记之前应该有类名
{
^
boxType.cpp:在成员函数“void boxType::setDimension(double,double,double)”中:
boxType.cpp:8:2:错误:未在此作用域中声明“长度”
长度=l;
^
boxType.cpp:9:2:错误:未在此作用域中声明“宽度”
宽度=w;
^
boxType.cpp:在成员函数“double boxType::area()const”中:
boxType.cpp:18:10:错误:未在此作用域中声明“长度”
返回(长度*宽度*高度);
^
boxType.cpp:18:17:错误:未在此作用域中声明“宽度”
返回(长度*宽度*高度);
^
boxType.cpp:在成员函数“double boxType::volume()const”中:
boxType.cpp:22:13:错误:未在此作用域中声明“长度”
返回((2*长*宽)+(2*长*高)+(2*宽*高))
^
boxType.cpp:22:20:错误:未在此作用域中声明“宽度”
返回((2*长*宽)+(2*长*高)+(2*宽*高))
^
boxType.cpp:23:1:错误:应为“;”在“}”标记之前
}
^
boxType.cpp:在成员函数“void boxType::print()const”中:
boxType.cpp:26:30:错误:未在此作用域中声明“宽度”
不能多个错误

  • 您希望继承类可以访问的变量必须受到保护;不是私人的
  • 要“多态”调用的函数必须是虚拟的;除非您正试图创建恰好具有相同名称的类特定函数;在这种情况下,如果您有一个基类指针,则将调用基类函数,而不管指向的对象是长方体还是矩形
  • 如果这是家庭作业,那么这是一个不好的例子。长方体不是矩形的类型。“is type of”是定义类层次结构时要遵循的一种规则。e、 一个盒子的面积是多少?有点无意义
    看看www.sscce.org,头文件是我的教授给我的,所以我认为它们是正确的。所以,我不能重写一个函数,除非它是虚拟的?您可以定义一个与基类中的函数同名的函数。没有什么能阻止你。读一下我写的——它不会表现出多态性。有区别。有一个术语可以解释这种差异;这个术语现在对我来说不仅仅是点击。任务是什么?从矩形中派生类?我怀疑。他给了我两个头文件,并告诉我为它们编写类文件。我没有对头文件进行任何更改。你能解释一下你所说的“不会多态”是什么意思吗?你的意思是我不能访问父类的变量/函数吗?我没有上下文;但如果是这样,也就是说,没有其他背景,那么就换一个教授。比改变C++更容易让它做你想做的事情。
    #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){
                                                                           ^