Instead of providing protected member data, provide protected member functions to get and set the data. - C++ Class

C++ examples for Class:Member Access

Description

Instead of providing protected member data, provide protected member functions to get and set the data.

Demo Code

#include <cstdlib> 
#include <iostream> 

using namespace std; 

class base /*from w w w. j av  a  2  s  . co  m*/
{ 
protected : 
    void AnInt(int intValue); 
    int AnInt(); 

private: 
    int anInt; 
}; 


inline void base::AnInt(int intValue) 
{ 
    if (intValue >= -10) 
    { 
        anInt = intValue; 
    } 
    else 
    { 
        anInt = -10; 
    } 
} 

inline int base::AnInt() 
{ 
    return (anInt); 
} 


class derived : public base 
{ 
    public : 
    void AFunction(int theInt); 
}; 

 inline void derived ::AFunction(int theInt) 
{ 
    AnInt(theInt); 

    cout << AnInt() << endl; 
} 

int main(int argc, char *argv []) 
{ 
    derived temp; 

    temp.AFunction(5); 

  
    return 0; 
}

Result


Related Tutorials