3 Replies - 278 Views - Last Post: 20 March 2013 - 10:36 PM Rate Topic: ***** 1 Votes

#1 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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();
             int additionFood();
             int additionLeisure();
             void print_addition();
      };
      
//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 derived :: additionFood()
{
    int total = get_food() + food_of_derived;
    return total;
 }
 
int derived :: additionLeisure()
{
    int total = get_leisure() + leisure_of_derived;
    return total;
    }

void derived :: print_addition()
{
     cout<<"Total money spent on both days (addition): "<<endl;
     cout<<"Food : RM"<<additionFood()<<endl;
     cout<<"Leisure ; RM"<<additionLeisure()<<endl<<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  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • 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
};

Was This Post Helpful? 0
  • +
  • -

#3 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1