C++ inheritance - public, private, protected
Inheritance in C++ could be public, private, or public. It could be confusing for a beginner to find out accessibility of members and objects of these classes. Though public inheritance is mostly used, especially if there are virtual functions in the base class, other two types of inheritance have their uses as well. Here's an example program to make it clear how the 3 of them work i.e. public inheritance, private inheritance, and protected inheritance.
#include
class B{
char a;
public:
char b;
protected:
char c;
};
// public inheritance
class D:public B{
// private members of B can't be accessed
//can access public and protected members of B
char d;
public:
void f(){
//can't do this
// d = a;
d = b; // works
d = c; // works
}
// class D has the following additional members inherited from B
// public: char b
// and
// protected: char c
// this means that any class derived from D can access both b and c
// b is accessible by objects of derived class, c is not (#1)
};
// private inheritance
class E:private B{
// private members of B can't be accessed...
// can access public and protected members of B
char d;
public:
void f(){
//can't do this
// d = a;
d = b; // works
d = c; // works
}
// class E has the following additional data members inherited from B
// private:char b
// and
// private: char c
// this means that any class derived from E can't access any of the above derived data
// members, nor are they accessible to objects of derived class, public functions
// though are accessible to objects of this class (#2)
};
// protected inheritance
class F:protected B{
// private members of B can't be accessed in this class as above, so
// can access public and protected members of B
char d;
public:
void f(){
//can't do this
// d = a;
d = b; // works
d = c; // works
}
// class F has following additional members inherited from B
// protected:char b
// and
// protected: char c
// this means that any class derived from F can access
// the above members, but they can't be accessed from
// derived class objects (#3)
};
class DF: public F{
char d;
public:
void g(){
d = b; // works from #3 above
d = c; // works from #3
}
};
int main(){
char i;
B b;
D d;
E e;
F f;
// these won't work by definition
// b.c = i;
// b.a = i;
// for object d of publicly derived class
// this works from (#1)
d.b = i;
// this won't work from (#1)
// d.c = i;
// for object e of privately derived class
// these won't work from (#2)
// e.b = i;
// e.c = i;
// for object f of protected-ly derived class
// these won't work from (#3)
// f.b = i;
// f.c = i;
return 0;
}
Internet Security and Ethical Hacking
Internet Security and Ethical Hacking |
Welcome to the unique confluence of hackers , crackers and security professionals on the world wide web. This is your complete resource for internet security and ethical hacking. -: The Ethical Hacker :- Most people think that hackers are computer criminals. They fail to recognise the fact that criminals and hackers are two totally different things. Media is responsible for this. Hackers in reality are actually good and extremely intelligent people who by using their knowledge in a constructive manner help organisations, companies, goverment, etc. to secure documents and secret information on the internet. |
WELCOME TO ALL VISITORS
SEARCH FROM HERE
Thursday, November 5, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment