撰寫一個使用繼承的 C 程式,以顯示一籃水果中蘋果和芒果的數量。
制作三類水果,蘋果和芒果。Fruit 作為基類,apple 和 mango 作為子類。
水果類應包含所有變數和兩個函式來輸入值并計算籃子中水果的總數。
子類 apple 和 mango 都應該包含一個函式,用于列印籃子中蘋果/芒果的數量
我想出了以下代碼作為答案:
#include <iostream>
using namespace std;
class fruit
{
public:
int apples = 0, mangoes = 0;
int total_fruits = 0;
void input_fruits()
{
cout << "Enter the number of apples : ";
cin >> apples;
cout << "Enter the number of mangoes : ";
cin >> mangoes;
}
void calculate_total()
{
total_fruits = apples mangoes;
cout << "The total fruits in the basket are : " << total_fruits << endl;
}
};
class apple : public fruit
{
public:
void show_apples()
{
cout << "The number of apples in the basket is : " << apples << endl;
}
};
class mango : public fruit
{
public:
void show_mangoes()
{
cout << "The number of mangoes in the basket is : " << mangoes << endl;
}
};
int main()
{
apple a1;
mango m1;
a1.input_fruits();
a1.show_apples();
m1.show_mangoes();
a1.calculate_total();
return 0;
}
這是輸出,其中芒果計數不起作用:

當show_mangoes()函式被呼叫時,它會回傳之前宣告的mangoesie值0。如果我在宣告時洗掉該值,則該函式將回傳指標值。
我無法弄清楚為什么mango該類無法訪問fruit該類中的資料的原因。
但是我能夠想出兩種獲取輸出的技巧。
- 在全域范圍內宣告變數:
#include <iostream>
using namespace std;
int apples = 0, mangoes = 0;
int total_fruits = 0;
class fruit
{
public:
void input_fruits()
{
cout << "Enter the number of apples : ";
cin >> apples;
cout << "Enter the number of mangoes : ";
cin >> mangoes;
}
void calculate_total()
{
total_fruits = apples mangoes;
cout << "The total fruits in the basket are : " << total_fruits << endl;
}
};
class apple : public fruit
{
public:
void show_apples()
{
cout << "The number of apples in the basket is : " << apples << endl;
}
};
class mango : public fruit
{
public:
void show_mangoes()
{
cout << "The number of mangoes in the basket is : " << mangoes << endl;
}
};
int main()
{
apple a1;
mango m1;
a1.input_fruits();
a1.show_apples();
m1.show_mangoes();
a1.calculate_total();
return 0;
}
輸出:

- 繼承蘋果類而不是芒果:
#include <iostream>
using namespace std;
class fruit
{
public:
int apples = 0, mangoes = 0;
int total_fruits = 0;
void input_fruits()
{
cout << "Enter the number of apples : ";
cin >> apples;
cout << "Enter the number of mangoes : ";
cin >> mangoes;
}
void calculate_total()
{
total_fruits = apples mangoes;
cout << "The total fruits in the basket are : " << total_fruits << endl;
}
};
class apple : public fruit
{
public:
void show_apples()
{
cout << "The number of apples in the basket is : " << apples << endl;
}
};
class mango : public apple
{
public:
void show_mangoes()
{
cout << "The number of mangoes in the basket is : " << mangoes << endl;
}
};
int main()
{
mango m1;
m1.input_fruits();
m1.show_apples();
m1.show_mangoes();
m1.calculate_total();
return 0;
}
輸出:

您能否解釋一下為什么 mango 類在第一版代碼中無法從父類訪問變數。任何關于更好方法的建議也值得贊賞。
uj5u.com熱心網友回復:
當呼叫 show_mangoes() 函式時,它回傳之前宣告的芒果值,即 0
問題是你從來沒有使用過m1.input_fruits()whilem1你a1確實使用過a1.input_fruits(). 而且由于您從未呼叫input_fruits過m1它的資料成員mango,因此仍然具有來自類內初始化程式0的 value( ) 。
所以要解決這個問題,只需呼叫input_fruits()它m1,它將給出所需的輸出(即用戶輸入的值),如下所示。
作業演示
int main()
{
apple a1;
mango m1;
a1.input_fruits();
a1.show_apples();
m1.input_fruits(); //ADDED THIS which was missing before in your example 1
m1.show_mangoes();
a1.calculate_total();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/479838.html
上一篇:抽象類繼承串列中的約束
