我的任務是練習繼承,將所有類放在單獨的檔案中。我有一個基類Circle和一個派生類Cylinder。
我堅持的是試圖在螢屏上顯示我計算B的Cylinder型別物件的面積和體積的結果。我找到了一種方法來為 a 做到這一點Circle,盡管它對我的Cylinder.
圈子.h
#pragma once
#include <iostream>
class Circle {
public:
float r;
Circle();
Circle(float r);
Circle circumference();
Circle area();
void view();
void getArea();
void getCircum();
};
圈子.cpp
#include "circle.h"
#include <cmath>
using namespace std;
Circle::Circle() : r(5) {
cout << "Default constructor has been called for a circle\n";
}
Circle::Circle(float r) {
this->r = r;
cout << "Constructor with parameters has been called for a circle\n";
}
void Circle::view() {
cout << "Radius = " << r << endl;
}
Circle Circle::circumference() {
return 2 * M_PI * r;
}
Circle Circle::area() {
return M_PI * pow(r, 2);
}
void Circle::getArea() {
cout << "Area = " << r << " m^2";
}
void Circle::getCircum() {
cout << "Circumference = " << r << " m";
}
圓柱體.h
#pragma once
#include <iostream>
#include "circle.h"
class Cylinder : public Circle {
public:
float h;
Cylinder();
Cylinder(float r, float h);
void view();
double area();
double volume(float r, float h);
void getArea();
void getVolume();
};
圓柱體.cpp
#include "cylinder.h"
#include <cmath>
using namespace std;
Cylinder::Cylinder() : h(7) {
cout << "Default constructor has been called for a cylinder\n";
}
Cylinder::Cylinder(float r, float h) : Circle(r) {
this->h = h;
cout << "Constructor with parameters has been called fo a cylinder\n";
}
void Cylinder::view() {
Circle::view();
cout << "Height = " << h << endl;
}
double Cylinder::area() {
return 2 * M_PI * r * h;
}
double Cylinder::volume(float r, float h) {
return M_PI * pow(r, 2) * h;
}
void Cylinder::getArea() {
cout << "Area = " << h;
}
void Cylinder::getVolume() {
cout << "Volume = " << h;
}
主檔案
#include <iostream>
#include "circle.h"
#include "cylinder.h"
using namespace std;
int main() {
Circle A;
A.view();
Circle A1(8);
A1.view();
Cylinder B;
B.view();
Cylinder B1(4, 6);
B1.view();
//A.area().getArea();
//cout << endl;
//A.circumference().getCircum();
//cout << endl;
//A1.area().getArea();
//cout << endl;
//A1.circumference().getCircum();
B.area().getArea();
return 0;
}
我得到的錯誤:
main.cpp: In function ‘int main()’:
main.cpp:26:14: error: request for member ‘getArea’ in ‘B.Cylinder::area()’, which is of non-class type ‘double’
26 | B.area().getArea();
| ^~~~~~~
main()例如,我覺得我的代碼、B方法getArea()和getVolume()類Cylinder中的代碼都不正確。盡管我注釋掉的代碼確實有效,但可能有更好的方法對物件A和型別執行相同A1的操作。Circle
我知道這是一個愚蠢的問題,這樣的事情應該很簡單,但我正在努力學習,并感謝任何關于如何解決這個問題的建議。
uj5u.com熱心網友回復:
好吧,您收到錯誤訊息的原因是:
main.cpp: In function ‘int main()’:
main.cpp:26:14: error: request for member ‘getArea’ in ‘B.Cylinder::area()’, which is of non-class type ‘double’
26 | B.area().getArea();
| ^~~~~~~
是因為你基本上是這樣做的:
auto _ = B.area();
所以在這里,_推斷為 a double,然后你做:
_.getArea();
您正在嘗試從 a 訪問成員函式double,并且double沒有任何成員函式。
您可能打算這樣做:
auto x = B.area();
B.h = x;
B.GetArea();
這會將 的區域分配B.area()給變數x,并分配x給B.h。B的成員函式然后被呼叫并輸出該區域。
uj5u.com熱心網友回復:
在你的getArea()函式中,而不是說:
cout << "Area = " << endl;
說啊:
cout << "Area = " << area() << endl;
然后在你的main.cpp,只是呼叫B.getArea()。
希望這可以幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/526238.html
標籤:C 目的哎呀遗产方法
上一篇:表創建停止而不拋出例外
下一篇:轉換物件內部的值
