Accessing nested class function through Base classs Object
Oct 21, 2016 at 1:33am UTC
I have a class Called Cbros, and then a nested class called Wallet. There's a function defined under Wallet. I can access that function in main() through a Cbros::Wallet Object, but that object is access to only the Wallet functions. How can I set it up in the Class to do this or what syntax to use in the main() to make this happen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#ifndef CBROS_H
#define CBROS_H
#include <iostream>
class Cbros
{
public :
Cbros();
//default constructor
Cbros(std::string name, int age, double height);
std::string getName()const ;
void setName(std::string);
void setAge(int );
void setHeight(int );
void sayName();
class Wallet{
public :
double addMoney(double amount);//Adds Money to wallet
double removeMoney(double amount);//removes money from wallet
double countMoney();//displays money in wallet
private :
double money;
};
protected :
std::string newName;
std::string newRace;
int newAge;
double newHeight;
int str;
int intel;
int charm;
private :
};
#endif // CBROS_H
Last edited on Oct 21, 2016 at 3:51am UTC
Oct 21, 2016 at 8:01am UTC
What do you want to make happen?
Oct 21, 2016 at 8:21am UTC
I want to be able to access the nested functions from the enclosing Object while still being able to access the enclosing functions. Not sure if need to use inheritance, friend, or if I have it right.
Oct 21, 2016 at 8:54am UTC
Make a variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#ifndef CBROS_H
#define CBROS_H
#include <iostream>
class Cbros
{
public :
Cbros();
//default constructor
Cbros(std::string name, int age, double height);
std::string getName()const ;
void setName(std::string);
void setAge(int );
void setHeight(int );
void sayName();
class Wallet{
public :
double addMoney(double amount);//Adds Money to wallet
double removeMoney(double amount);//removes money from wallet
double countMoney();//displays money in wallet
private :
double money;
};
Wallet myWallet; // Note
protected :
std::string newName;
std::string newRace;
int newAge;
double newHeight;
int str;
int intel;
int charm;
private :
};
#endif // CBROS_H
...
int main()
{
Cbros myCbros;
myCbros.myWallet.addMoney(...);
return 0;
}
Topic archived. No new replies allowed.