Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

BlueHost Web Hosting Black Friday
BlueHost Web Hosting Black Friday

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

16,311 questions

21,782 answers

573 users

How to use a class with protected variable and protected function in C++

1 Answer

0 votes
#include <iostream>

class Base {
protected:
   int  x;
   void pfunction() { std::cout << "Access to pfunction() OK\n"; }
public:
   void setx( int i ) { x = i; }
   void Display() { std::cout << x << "\n"; }

} cb;

class Derived : public Base {
public:
   void f() { pfunction(); }
} dc;

int main() {
   // cb.x;         // error: ‘int Base::x’ is protected within this context
   cb.setx(298);  
   cb.Display();
   
   dc.setx(3);  
   dc.Display();
   
   // cb.pfunction(); //  error: ‘void Base::pfunction()’ is protected within this context
   dc.f();     
   
   return 0;
}


/*
run:

298
3
Access to pfunction() OK

*/

 




answered 4 days ago by avibootz
...