Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between protected and provate 1

Status
Not open for further replies.

daveask

Programmer
Aug 11, 2004
108
0
0
GB
Hi Expert,

This is me again :(
I am learning Protected and Private member in a base class. Can you show me what is the difference by following code? I tried to access the protected variable proValue in the base class without success!

#include<iostream.h>
#include<stdio.h>

class BaseClass
{
protected:
int proValue;
private:
int priValue;
public:
BaseClass(){proValue = 1; priValue = 2;}
};

class DeriveClass : public BaseClass
{
};

void main()
{
DeriveClass class2;
cout<<class2.proValue()<<endl;
// cout<<class2.priProValue<<endl;
getchar();
}
 
You can only access the attribute proValue in the scope of the class DeriveClass.
Example :
Code:
class DeriveClass : public BaseClass
{
public:
  int dummy_nb () const
  {
    return proValue;
  }
};

--
Globos
 
public -- Anyone can use it.
protected -- Children can use it.
private -- It's mine ALL MINE! YOU CAN'T HAVE IT!
 
jstreich said:
public -- Anyone can use it.
protected -- Children can use it.
private -- It's mine ALL MINE! YOU CAN'T HAVE IT!
Unless, of course, you are my friend, in which case you can use anything of mine, including my privates.
 
There is a 4th level:

ultra private - It's mine ALL MINE! You don't even know I have it!

Code:
//-------------------
// SomeClass.h

SomeClass
{
   // ...
   int someMethod() const;
};

//-------------------
// SomeClass.cpp

#include "SomeClass.h"

// Stuff in an anonymous namespace is only accessible from this file's scope.
// Ie only accessible from the class' methods that are
// implemented here (or other functions here)

namespace
{
  int anUltraPrivateFunction()
  {
    // ...
  }
}

// ...
int SomeClass::someMethod() const
{
  return anUltraPrivateFunction();
}

Consider ultra privates when dealing with internal helper functions that really don't have to be part of the class.

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Ultra private can be applied to data members, as well (even instance ones) with a little extra work:

Secret.h
Code:
class Secret
{
  public:
    Secret(int val);
    Secret(const Secret& s);
    ~Secret();

    void set_value(int val);

    int get_value() const;

  private:
    struct Impl;

    Impl* guts;
};

Secret.cpp
Code:
struct Secret::Impl
{
    int secret_variable;
}

Secret::Secret(int val)
: guts(new Impl)
{
    set_value(val);
}

Secret::Secret(const Secret& s)
: guts(new Impl(s->guts))
{}

Secret::~Secret()
{
    delete guts;
}

void Secret::set_value(int val)
{
    guts->secret_variable = val;
}

int Secret::get_value() const
{
    return guts->secret_variable;
}

Now, even if I change the source file to hold a key into a global map containing the value, or a key into a database, the header file doesn't change, and I don't have to recompile any code that was using [tt]Secret[/tt].


Also, writing the copy constructor reminded me of something: [tt]public[/tt], [tt]protected[/tt], and [tt]private[/tt] work at the class level, not the object level. So an instance of a class can access private (or protected) members of an instance of the same class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top