473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to make public member of a class, private in the derived class?

There is a base class A, which contains 4 different data members, all are public.
class B is publicly derived from class A.
How to make just one data member of class A as private in class B ?
May 4 '12 #1
2 1399
Banfa
9,065 Expert Mod 8TB
You use a using statement to alter the specific symbols access level, like this

Expand|Select|Wrap|Line Numbers
  1. class base
  2. {
  3. public:
  4.     int a;
  5.     int b;
  6.     int c;
  7.     int d;
  8. };
  9.  
  10. class derived : public base
  11. {
  12. private:
  13.     using base::a;
  14. };
  15.  
However it should be noted that best practice is for all class data to be private (or at least protected) and to provide getters and setters for them. You could do this just by using private inheritance and providing the getters and setters in the derived class.

Expand|Select|Wrap|Line Numbers
  1. class derived : private base
  2. {
  3. public:
  4.     int getA(){ return a; };
  5.     int getB(){ return b; };
  6.     int getC(){ return c; };
  7.     int getD(){ return d; };
  8.  
  9.     void setA(int value){ a = value; };
  10.     void setB(int value){ b = value; };
  11.     void setC(int value){ c = value; };
  12.     void setD(int value){ d = value; };
  13. };
  14.  
May 4 '12 #2
Thanks a lot for a quick reply.


@Banfa
May 4 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
1
by: Rich Talbot-Watkins | last post by:
Hi all, Thought I'd call on the experts to see if there's a solution to my problem - it's best explained by way of an example. Consider the following code extract: ...
6
by: Alf P. Steinbach | last post by:
#include <iostream> struct Belcher { int x; void belch() { std::cout << x << std::endl; } Belcher( int anX ): x( anX ) {} }; struct Person: private Belcher { Person(): Belcher( 666 ) {} };
2
by: Joe HM | last post by:
Hello - I have a function in a base class that I want to use the shadow'ed member variable of the derived class. Here is the code ... Public Class cCaptureBase Protected Const cDummy As...
8
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one...
3
by: Vulcan | last post by:
<code> class Base { }; class Class1 : public Base{ public: void Function1(); }
3
by: Dmitry | last post by:
Hi all, Consider the following code: class A { public: A() {} void DoMethod() { (this->*m_pMethod)(); } protected: virtual void MethodA() { ... }
6
by: kepeng | last post by:
There are 2 abstract base classes: class IB; class IA { //... public: virtual IB* GetB() = 0; }
3
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.