Users Online
· Guests Online: 27
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C++ Program to Demonstrate the use of Access Control Specifiers
C++ Program to Demonstrate the use of Access Control Specifiers
This C++ program illustrates the use of access control specifiers in a class. The three access control specifiers are as follows : public – member is visible to the user and derived classes, private – member is not visible to the user and derived classes and protected – member is visible to the derived class but not to the user.
Here is the source code of the C++ program illustrates the use of access control specifiers in a class. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
-
/*
-
* C++ Program to Illustrate the use of Access Control Specifiers
-
*/
-
#include <iostream>
-
-
class Base {
-
private:
-
int a;
-
protected:
-
int b;
-
public:
-
int c;
-
Base(int aa = 1, int bb = 2, int cc = 3) : a(aa), b(bb), c(cc) {}
-
virtual ~Base() {}
-
};
-
-
class Derived: public Base {
-
int j;
-
public:
-
Derived(int jj = 0) : j(jj) {}
-
void change()
-
{
-
// 'b' is protected
-
j = b;
-
}
-
void printValue()
-
{
-
std::cout << "Derived::j = " << j
-
<< std::endl;
-
}
-
};
-
-
int main()
-
{
-
Base base;
-
Derived derived;
-
-
// 'a' and 'b' are private and protected
-
// std::cout << base.a << std::endl;
-
// std::cout << base.b << std::endl;
-
std::cout << "Base::c = "<< base.c << std::endl;
-
derived.change();
-
derived.printValue();
-
}
$ a.out Base::c = 3 Derived::j = 2
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.