c++ - How do I modify a private variable in a base class with a derived member function? -


my full question because couldn't fit on title is:how 2 modify private variable in base class derived class b member function call member function thats been overrided in base class modify different private member variable in derived class b. i'm having tough time trying figure out because can't figure out how modify 2 private member variables in different classes in 2 member functions come different classes. have code far. i'll make comments in code on issue i'm trying solve

in .h file have:

#include <iostream> #include <string> using namespace std;  class a{ public:     a();     a(string name_holder, double balance_holder);     int take(float amount);     string getname();     double getbalance();     void output(); private:     string name;     double balance; };  class b:public a{ public:     b();     b(int number_of_withdrawal_holder);     int take(float amount);//overriding     void output(); private:     static int count; }; 

in .cpp file have

#include "header.h"   a::a() {     name=" ";     balance=0; }  a::a(string name_holder,double balance_holder) {     name=name_holder;     balance=balance_holder; } int a::take(float amount) {     if (balance >= amount && amount>=0)//if amount less or equal balance , nonnegative     {         balance-=amount;         cout<<"ok"<<endl;         return 0;     }      cout <<"insufficient funds withdrawal take place"<<endl;     return 1;  }  string a::getname() {     return name; } double a::getbalance() {     return balance; }  void a::output() {     cout<<"name: "<<getname()<<endl;     cout<<"balance: "<<getbalance()<<endl; }  int b::count=0;  b::b() {     count=0; } b::b(int number_of_withdrawal_holder) {     count=number_of_withdrawal_holder; } int b::take(float amount) {     if(count==0)//if first withdrawal     {         ++count;         return bankaccount::take(amount);     }      //the bottom part of code gets excuted if withdrew atleast once-you pay fee of 1.50(in other words want function call first withdraw function , subtract 1.50 member variable balance want modify count in base class function take      ++number_of_withdrawals;      return bankaccount::withdraw(amount); } 

if can me out great!

a private member private derived subclasses. if want allow derived subclass access member, make protected instead of private. such members still not public, can accessed subclasses.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -