关于继承的困惑
Confusion regarding inheritance
我在测试一个层次继承的小程序时遇到了这个问题。该程序包含一个 parent class 银行和两个 child classes 取款和存款。
#include<iostream>
#include<conio.h>
#include<stdlib.h>
//Hierarchical Inheritance
using namespace std;
class bank{
protected:
char name[20];
int age, id;
float bal;
public:
void getData(){
cout <<"Enter your name, age, bank ID and balance" << endl;
cin >> name >> age >> id >> bal;
}
void display(){
cout <<"Name: " << name << endl << "ID: " << id << endl;
cout <<"Age: " << age <<endl <<"Balance: " << bal << endl;
}
void check(){
cout <<"Your balance is " << bal << endl;
}
};
class withdraw : public bank{
float wd;
public:
void withdrawMoney(){
cout << "Enter the amount to withdraw" << endl;
cin >> wd;
if(wd > bal)
cout << "You cannot withdraw that much. Your balance is only " << bal << endl;
else{
bal = bal-wd;
cout << "You successfully withdrew " << wd << ". Your remaining balance is " << bal << endl;
}
}
};
class deposit : public bank{
float dp;
public:
void depo(){
cout <<"Enter the amount to deposit" << endl;
cin >> dp;
bal = bal+dp;
cout <<"You successfully deposited " << dp << ". Your balance is now " << bal << "." << endl;
}
};
int main()
{
int c;
bank b;
deposit d;
withdraw w;
b.getData();
do{
cout <<"***The Dank Bank***" << endl;
cout <<"What do you want to do?\n 1)Withdraw\n 2)Deposit\n 3)Check Balance\n 4)Display all details\n 5)Exit\n" << endl;
cin >> c;
if(c == 1)
w.withdrawMoney();
else if (c == 2)
d.depo();
else if(c == 3)
b.check();
else if(c == 4)
b.display();
else if(c == 5)
exit(0);
else
cout <<"Wrong choice." << endl;
cout<<"Press any key to continue" << endl;
getch();
}
while(1);
getch();
return 0;
}
执行撤回功能时,我得到以下输出:
You cannot withdraw that much. Your balance is only 6.03937e-039
使用存款功能时,输出显示存款金额而不是实际余额。
You successfully deposited 1000. Your balance is now 1000.
childclasses 使用的唯一变量是 bal,所以我决定像这样全局声明它。
#include<iostream>
#include<conio.h>
#include<stdlib.h>
float bal;
该程序没有任何缺陷。但是这样做违背了使用继承的全部目的。
我很困惑。为什么会这样?
类 是对 object 的外观和行为的描述。你有其中三个。一个描述你所说的 bank
,一个 deposit
和一个 withdraw
.
Objects 是 classes 的实例。所以 object 是具有 class 所说的它应该具有的存储和行为的东西。您有三个 object:b
、d
和 w
。
每个object都有自己独立的存储空间。如果你想让 object 知道另一个 object,你需要告诉它。 w
不能只找到 b
。如果您有 b_barclays
、b_natwest
和 b_hsbc
会怎样?您希望 w
找到哪个?为什么?
我认为您所做的是将 classes 合并,它告诉编译器如何创建 object 与 objects 本身。一个从另一个继承的 class 简单地说子 class 将包含 parent 的行为和存储。所以它说,如果你创建两种类型的 object,那么 child 将拥有 parent 能力的超集。它不会创建任何存储共享;每个都有完全独立的存储空间,按照 class.
的定义布局
我在测试一个层次继承的小程序时遇到了这个问题。该程序包含一个 parent class 银行和两个 child classes 取款和存款。
#include<iostream>
#include<conio.h>
#include<stdlib.h>
//Hierarchical Inheritance
using namespace std;
class bank{
protected:
char name[20];
int age, id;
float bal;
public:
void getData(){
cout <<"Enter your name, age, bank ID and balance" << endl;
cin >> name >> age >> id >> bal;
}
void display(){
cout <<"Name: " << name << endl << "ID: " << id << endl;
cout <<"Age: " << age <<endl <<"Balance: " << bal << endl;
}
void check(){
cout <<"Your balance is " << bal << endl;
}
};
class withdraw : public bank{
float wd;
public:
void withdrawMoney(){
cout << "Enter the amount to withdraw" << endl;
cin >> wd;
if(wd > bal)
cout << "You cannot withdraw that much. Your balance is only " << bal << endl;
else{
bal = bal-wd;
cout << "You successfully withdrew " << wd << ". Your remaining balance is " << bal << endl;
}
}
};
class deposit : public bank{
float dp;
public:
void depo(){
cout <<"Enter the amount to deposit" << endl;
cin >> dp;
bal = bal+dp;
cout <<"You successfully deposited " << dp << ". Your balance is now " << bal << "." << endl;
}
};
int main()
{
int c;
bank b;
deposit d;
withdraw w;
b.getData();
do{
cout <<"***The Dank Bank***" << endl;
cout <<"What do you want to do?\n 1)Withdraw\n 2)Deposit\n 3)Check Balance\n 4)Display all details\n 5)Exit\n" << endl;
cin >> c;
if(c == 1)
w.withdrawMoney();
else if (c == 2)
d.depo();
else if(c == 3)
b.check();
else if(c == 4)
b.display();
else if(c == 5)
exit(0);
else
cout <<"Wrong choice." << endl;
cout<<"Press any key to continue" << endl;
getch();
}
while(1);
getch();
return 0;
}
执行撤回功能时,我得到以下输出:
You cannot withdraw that much. Your balance is only 6.03937e-039
使用存款功能时,输出显示存款金额而不是实际余额。
You successfully deposited 1000. Your balance is now 1000.
childclasses 使用的唯一变量是 bal,所以我决定像这样全局声明它。
#include<iostream>
#include<conio.h>
#include<stdlib.h>
float bal;
该程序没有任何缺陷。但是这样做违背了使用继承的全部目的。
我很困惑。为什么会这样?
类 是对 object 的外观和行为的描述。你有其中三个。一个描述你所说的 bank
,一个 deposit
和一个 withdraw
.
Objects 是 classes 的实例。所以 object 是具有 class 所说的它应该具有的存储和行为的东西。您有三个 object:b
、d
和 w
。
每个object都有自己独立的存储空间。如果你想让 object 知道另一个 object,你需要告诉它。 w
不能只找到 b
。如果您有 b_barclays
、b_natwest
和 b_hsbc
会怎样?您希望 w
找到哪个?为什么?
我认为您所做的是将 classes 合并,它告诉编译器如何创建 object 与 objects 本身。一个从另一个继承的 class 简单地说子 class 将包含 parent 的行为和存储。所以它说,如果你创建两种类型的 object,那么 child 将拥有 parent 能力的超集。它不会创建任何存储共享;每个都有完全独立的存储空间,按照 class.
的定义布局