# Inheritance Problem

Page 1 of 1

## 3 Replies - 491 Views - Last Post: 20 March 2013 - 10:36 PMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316154&amp;s=d58089dbdfbc21d13bb27522b64dfc27&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 UrIkOn

Reputation: 0
• Posts: 53
• Joined: 06-November 11

# Inheritance Problem

Posted 20 March 2013 - 06:36 PM

I am a beginner. Can I know why my third class cannot access the member function of base class?

```#include <iostream>
#include <cstdlib>
using namespace std;

//base class
class base
{
friend float average(int food,int leisure);
private:
int food_of_base;
int leisure_of_base;

public:
base();
int get_food();
int get_leisure();
void get_money_base();
void print_average();
};
//derived class
class derived : public base
{
friend class third;
private:
int food_of_derived;
int leisure_of_derived;

public:
derived();
void get_money_derived();
};

//third class
class third
{
private:
int food_of_third;
int leisure_of_third;

public:
void get_money_third();
int differenceFood();
int differenceLeisure();
};

//base class implementation
base :: base()
{
food_of_base = 0;
leisure_of_base = 0;
}

void base :: get_money_base()
{
cout<<"Enter the money spent of base class:"<<endl;
cout<<"Food: RM";
cin>>food_of_base;

cout<<"Leisure: RM";
cin>>leisure_of_base;
cout<<endl;
}

int base :: get_food()
{
return food_of_base;
}

int base :: get_leisure()
{
return leisure_of_base;
}

float average(int food_of_base,int leisure_of_base)
{
float f = food_of_base;
float l = leisure_of_base;
return ( f + l ) / 2;
}

void base :: print_average()
{
cout<<"Average money spent in base for function class is : RM "<< average( food_of_base , leisure_of_base ) <<endl;
cout<<endl;
}

//derived class implementation
derived :: derived()
{
int food_of_derived = 0;
int leisure_of_derived = 0;
}

void derived :: get_money_derived()
{
cout<<"Enter the money spent of derived class:"<<endl;
cout<<"Food: RM";
cin>>food_of_derived;

cout<<"Leisure: RM";
cin>>leisure_of_derived;
cout<<endl;
}

{
int total = get_food() + food_of_derived;
}

{
int total = get_leisure() + leisure_of_derived;
}

{
cout<<"Total money spent on both days (addition): "<<endl;
}

//third class implementation
void third :: get_money_third()
{
cout<<"Enter the money spent for friend class:"<<endl;
cout<<"Food: RM";
cin>>food_of_third;

cout<<"Leisure: RM";
cin>>leisure_of_third;
cout<<endl;
}

int third :: differenceFood()
{
int food = get_food();
if(food > food_of_third)
{return food - food_of_third;}
else
{return food_of_third - food;}
}

int third :: differenceLeisure()
{
int leisure = get_leisure();
if(leisure > leisure_of_third)
{return leisure - leisure_of_third;}
else
{return leisure_of_third - leisure;}
}
```

Is This A Good Question/Topic? 0

## Replies To: Inheritance Problem

### #2 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,619
• Joined: 15-May 08

## Re: Inheritance Problem

Posted 20 March 2013 - 07:39 PM

Because your third class is not extending any class.

```class third {
// foo
};
```

Should be either as you intend
```class third : public base {
// foo
};
// or perhaps
class third : public derived {
// foo
};
```

### #3 UrIkOn

Reputation: 0
• Posts: 53
• Joined: 06-November 11

## Re: Inheritance Problem

Posted 20 March 2013 - 08:31 PM

In my code line 23
``` friend class third;
```

This doesn't meant that my third class is friend of derived class and derived class is extend from base class, so my third class also can access the member function of base class?Or my concept is wrong?

### #4 #define

• Duke of Err

Reputation: 1636
• Posts: 5,765
• Joined: 19-February 09

## Re: Inheritance Problem

Posted 20 March 2013 - 10:36 PM

The functions need an object to act on. Since they are not inherited this is not an object of derived or base.

C++ Tutorial - Friend