gpt4 book ai didi

C++ 继承不继承

转载 作者:行者123 更新时间:2023-12-02 11:15:53 32 4
gpt4 key购买 nike

矩形类型.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;
};

矩形类型.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;
}

盒子类型.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;
};

盒子类型.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;
}

矩形类型测试.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。不知道我做错了什么。任何帮助,将不胜感激。谢谢你。

最佳答案

多个错误

  • 您希望继承类访问的​​变量必须受到保护;不是私有(private)的。
  • 您希望“多态”调用的函数必须是虚拟的;除非您试图创建恰好具有相同名称的特定于类的函数;在这种情况下,如果您有一个基类指针,则将调用基类函数,而不管指向的 abject 是框还是矩形。
  • 如果这是家庭作业,那么这是一个不好的例子。盒子不是一种矩形。 “是类型”是定义类层次结构时要遵循的一种规则。例如盒子的面积是多少?有点无厘头。
  • 关于C++ 继承不继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28578515/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com