类的static成员变量和成员函数能被继承吗? 友元函数能访问父类的private变量吗?那static成员函数能是纯虚函数吗,能被继承吗?

static不能被继承,不能为虚函数 .


如果要让子类的friend函数访问父类的全部变量 :让父类声明这个函数为friend函数.

 

1. 父类的static变量和函数在派生类中依然可用,但是受访问性控制(比如,父类的private域中的就不可访问),而且对static变量来说,派生类和父类中的static变量是共用空间的,这点在利用static变量进行引用计数的时候要特别注意。

2. static函数没有“虚函数”一说。因为static函数实际上是“加上了访问控制的类的全局函数”,全局函数哪来的什么虚函数?

3. 派生类的friend函数可以访问派生类本身的一切变量,包括从父类继承下来的protected域中的变量。但是对父类来说,他并不是friend的。

好的,我知道了,不泄漏您的信息。以下是银行账户的设计,实现了封装、继承、友元、静态成员变量函数: ```cpp #include <iostream> using namespace std; class Account { // 基 protected: string name; int balance; int number; public: void deposit(int amount) { balance += amount; } virtual void withdraw(int amount) = 0; // 纯虚函数 virtual void display() { cout << "Number: " << number << endl; cout << "Name: " << name << endl; cout << "Balance: " << balance << endl; } friend class ATM; // 将 ATM 设为友元 }; class CheckingAccount : public Account { // 储蓄账户继承自 Account private: static double fee; public: CheckingAccount(string n, int b) { name = n; balance = b; number = rand() % 10000; } void withdraw(int amount) { if (balance >= amount + fee) { balance -= (amount + fee); } else { cout << "Insufficient balance!" << endl; } } void display() { Account::display(); cout << "Checking Account Fee: " << fee << endl; } static void setFee(double f) { fee = f; } // 静态函数 }; double CheckingAccount::fee = 5.0; // 静态成员变量的初始化 class SavingsAccount : public Account { // 存款账户继承自 Account private: int rate; public: SavingsAccount(string n, int b, int r) { name = n; balance = b; rate = r; number = rand() % 10000; } void withdraw(int amount) { if (balance >= amount) { balance -= amount; } else { cout << "Insufficient balance!" << endl; } } void display() { Account::display(); cout << "Savings Account Rate: " << rate << "%" << endl; } static void compoundInterest(SavingsAccount& sa) { // 静态函数 double interest = sa.balance * sa.rate / 100.0; sa.balance += interest; } }; class ATM { // 友元 public: void withdraw(Account& acc, int amount) { acc.withdraw(amount); } }; int main() { CheckingAccount::setFee(10.0); // 使用静态函数设置所有储蓄账户的手续费 Account* acc[3]; acc[0] = new CheckingAccount("Tom", 2000); acc[1] = new SavingsAccount("Jerry", 3000, 5); acc[2] = new CheckingAccount("Alice", 1000); for (int i = 0; i < 3; i++) { acc[i]->display(); cout << endl; } SavingsAccount::compoundInterest(*(SavingsAccount*)acc[1]); // 强制型转换,调用静态函数计算利息 cout << "After compound interest: " << endl; for (int i = 0; i < 3; i++) { acc[i]->display(); cout << endl; } ATM atm; atm.withdraw(*acc[0], 500); // 取款 atm.withdraw(*acc[1], 2000); // 取款 cout << "After withdraw: " << endl; for (int i = 0; i < 3; i++) { acc[i]->display(); cout << endl; } return 0; } ``` 以上代码演示了一个银行账户管理的例子,基为 Account,其中包括了存款和取款操作,及展示账户信息的 display 函数。储蓄账户 CheckingAccount 继承自 Account,多加了一个手续费 fee,不足以支付手续费时,取款操作将失败。存款账户 SavingsAccount 同理,多包括了一个利率 rate。ATM 用于实现取款操作,需要将 Account 设为友元,避免取款操作需要知道私有成员的信息。当然,ATM 技术已经很老,这里只是为了演示的友元。 设计模式上,以上代码主要应用了模板方法模式,封装了储蓄和取款操作,便于扩展新的账户型。同时,利用了静态成员变量函数,使得储蓄账户的手续费费率同步控制,利率定时计算。友元是典型的抽象工厂模式的应用,可以方便地扩展其他银行卡和业务功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值