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; 
     }

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.