Professional Documents
Culture Documents
Access Specifiers.revisited
The access specifiers of a class are:
private
protected
public
Private
The private members of a class can be
accessed only by the member functions of
that class.
private keyword and a : symbol are used
to indicate the section of the code, which is
to be treated as private.
private:
Protected
The protected members of a class can be
accessed only by the member functions of
that class, and the member functions of the
classes derived from it.
protected keyword and : symbol are used
to indicate the section of the code, that is to
treated as protected.
protected:
Public
The public members of a class can be
accessed anywhere in the application.
public keyword and : symbol are used to
indicate the section of the code, that is to
treated as public.
public:
Power of access.
An object defined outside a class can only
access the public members of the class.
The private members of a class cannot be
directly accessed from outside the class.
For example, the object of the class DERIVED,
named d1, cannot directly access the private
member variable, c.
Hence the following code, raises error.
DERIVED d1;
d1.c=34;
Contd
This holds true even of the class that is
derived from another class.
Even though the derived class inherits all
the data and methods from the base class,
the private members of the base class are
not accessible within the derived class.
The idea here is not to compromise on
encapsulation implemented through data
hiding.
Contd.
If the derived class, could access the private
members of the base class, then the strong
wall of encapsulation created by data hiding
could be easily broken by creating a dummy
derived class.
That would defeat the whole purpose of data
hiding and security.
Example.
class Encryption
{
private:
void keycompare()
{
}
public:
.
};
class cryptfile : public Encryption
{
.
};
int main()
{
..
}
Explanation.
The function, keycompare(), is meant for
internal use by the class, Encryption.
The derived class, cryptfile, may need to call the
function, keycompare().
As this function is private, it is not accessible
outside the class, Encryption.
This function, however, if made public, would be
accessible throughout the program, which is
not what the designer intended.
Contd.
The protected access specifier is used to
solve the above-mentioned problem.
A protected member of a class can be
accessed by its member functions or within
any class derived from it.
Contd.
A class may contain all, private, protected and public access
specifiers.
A programmer can decide which members require to be
declared using which access specifier, and label accordingly.
Example:
class Ex
{
private:
int a;
void fn1();
protected:
int b;
void fn2();
public:
int c;
void fn3();
};
Contd
The access specifier can be
private, protected or
public.
get
imposed
when
Contd.
The protected Access Specifier:
In a protected access specifier, all the
private members of a base class remain private in
the object, the protected members remain
protected, but all the public members of the
base class become protected.
Programmers rarely use this form of
inheritance.
Contd.
The privateAccess Specifier:
In a private access specifier, all the
private members of a base class remain private in
the object and all the public and protected
members in the base class become private.
This form of inheritance depicts a
composition relationship between base and
the derived classes.
Case I
Case 2
Case 3
class BASE
{
private:
int a,b;
protected:
int c,d;
public:
int e;
int fn();
};
class DERIVED : public BASE
{
private:
int p,q;
protected:
int r, s;
public:
int t;
int D( );
};
class BASE
{
private:
int a,b;
protected:
int c,d;
public:
int e;
int fn();
};
class DERIVED: protected
BASE
{
private:
int p,q;
protected:
int r, s;
public:
int t;
int D( );
};
class BASE
{
private:
int a,b;
protected:
int c,d;
public:
int e;
int fn();
};
class DERIVED: private
BASE
{
private:
int p,q;
protected:
int r, s;
public:
int t;
int D( );
};
Case 2
Case 3
Private Members
a
c
b p
d
q
s
e fn() t D( )
Public Members
b p
a b c d p
e fn()
c d e r s fn()
t D( )
Protected Members
r
t D( )
Lets understand
In the code shown previously, e is a public
member variable of the BASE class.
As it can be seen from the visual
representation of the object, the member
variable, e, has public access in the object, if
DERIVED has a public access specifier.
It has a protected access in the object, if
DERIVED has a protected access specifier.
It has a private access in the object, if
DERIVED has a private access specifier.
Contd
In the DERIVED object of the type, CASE 1
(public access specifier), the vafunction fn().riable,
a could have been accessed through the
In CASE 3, the function itself has gone into the
private area of the object and thus, the variables of
the BASE class can no longer be accessed.
Thus public inheritance (through public access
specifier in derivation) propagates hierarchy, while
a private inheritance (through private access
specifier in derivation) effectively stops a
hierarchy.
#include< iostream.h>
#include<iomanip.h>
class BASE
{
public:
BASE()
{
cout<<Constructor of BASE<<endl;
}
~BASE()
{
cout<<Destructor of BASE<< endl;
}
};
Contd
class DERIVED : public BASE
{
public:
DERIVED()
{
cout<<Constructor of DERIVED<<endl;
}
~DERIVED()
{
cout<<Destructor of DERIVED<<endl;
}
};
int main()
{
DERIVED obj;
return 0;
}
Constructor of BASE
Constructor of DERIVED
Destructor of DERIVED
Destructor of BASE
Uses of Inheritance
Extending Functionality:
It is quite possible that in a large project,
several programmers share a class.
In such case, individual programmers may
not be permitted to modify the class.
If a programmer wants to add certain
functions or attributes to the class at this stage, he
or she may create a new class that inherits all the
members from the original class and then, use the
derived class.
This brings about reusability, rather than
recreation from scratch.
B2
B3
B4
B5
Derived1
Types of Inheritance
One important object-oriented mechanism is
multiple inheritance.
Multiple inheritance means that one subclass can
have more than one superclass.
This enables the subclass to inherit properties of
more than one superclass and to ``merge'' their
properties.
If class A inherits from more than one class, ie. A
inherits from B1, B2, ..., Bn, it is called as multiple
inheritance.
A Familiar example of MI is children inheriting
different characteristics of their parents.
Syntax of MI
class A: public base1, public base2
{
//body of class
};
Example
#include< iostream.h>
class base1
{
protected:
int ivar1;
public:
void show1 ()
{
cout<<ivar1<<endl;
}
};
class base2
{
protected:
int ivar2;
public:
void show2()
{
cout<<ivar2<<endl;
}
};
Contd.
};
int main()
{
derived obj;
obj.set(10,20)
obj.show1();
obj.show2();
return 0;
}
Access Specifier in MI
The default access specifier is private.
class derived : public base1, base2
{
};
This means that, base1 will have public
access and base2 would have private, since it has
not been mentioned.
If one intends public access, one should
explicitly declare.
Example of Ambiguity in MI
#include<iostream.h>
class base1
{
public:
void disp()
{
cout<<Base1<<endl;
}
};
class base2
{
public:
void disp()
{
cout<<Base2<<endl;
}
};
Contd.
class derived : public base1, public base2
{
};
int main()
{
derived obj;
obj.disp();
//ambiguous function call
return 0;
}
Ambiguity
In main() function of the above program, the
reference to the function, disp() is ambiguous.
The compiler doesnt know which function to
invoke, that of the class, base1, or the class base2,
since the derived class does not have the disp()
function.
The ambiguity can be resolved in few ways.
One of the way is use of scope resolution operator
while calling the function.
return 0;
}
The program
#include<iostream.h>
class A
{
public:
int Avar;
};
class B : public A
{
public:
int Bvar;
};
class C: public A
{
public:
int Cvar;
};
Contd
class D: public B, public C
{
public :
int Dvar;
};
int main()
{
D Dtemp;
Dtemp.Avar=10;
return 0;
}
//This is ambiguous
Contd
The above program will display an error
because the reference to the variable Avar
in Dtemp is ambiguous.
This ambiguity is caused because class D
::
Example
class A
{.};
class B : virtual public A
{};
Contd.
int main()
{
D Dtemp;
Dtemp.Avar=10;
return 0;