0

I try to compile the next code but the compiler says that **`int base::i' is private **

#include <cstdlib>
#include <iostream>

class base 
{ 
     int i; 
     public: 
             base (int i=0); 
             void set (); 
             int get (); 
             }; 

class derivada:public base 
{ 
      public: 
      void set (); 
      } ;

using namespace std;

int main(int argc, char *argv[])
{  
    base clas1; derivada clas2; 
    base *ptrclas1; derivada *ptrclas2;
    cout<<"Value of A using A's construtor "<<clas1.get ()<<endl;
    cout<<"Value of B using A's construtor "<<clas1.get ()<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}


base::base (int i) 
{ 
           this->i=i; 
           } 

void base::set () 
{ 
     this->i=1; 
     return; 
     } 
int base::get () 
{ 
    return (i); 
} 

void derivada :: set () 
{ 
     this->i=2; 
     return; 
     }
2
Contributors
1
Reply
8
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
0

line 50: this->i=2;

since i is private derived classes cannot change it's value.

You need to change base::set() (line 9) to accept an integer, then derived classes can call public set() to change the value of i.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.