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
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check C++ Books
- Check Computer Science Books
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for Computer Science Internship