Base Access Control In C++
Access modifiers in C++ class
defines the access control rules. C++ has 3 new keywords introduced, namely,
1.
public
2.
private
3.
protected
These access modifiers are used to
set boundaries for availability of members of class be it data members or
member functions
Access modifiers in the program, are
followed by a colon. You can use either one, two or all 3 modifiers in the same
class to set different boundaries for different class members.
Fig: Base Access Control In C++ |
1.Public
Public, means all the class
members declared under public will be available to everyone.
The data members and member functions declared public can be accessed by other
classes too. The key members must not be declared public.
Syntax:
class public access
{
public:
int x;
void display();
}
2.Private
Private keyword, means that
no one can access the class members declared private, outside that
class. If someone tries to access the private members of a class, they will get
a compile time error. By default class variables and member
functions are private.
Syntax:
class private access
{
private:
int x;
void display();
}
3.Protected
Protected, is the last
access specifier, and it is similar to private, it makes class member inaccessible
outside the class. But they can be accessed by any subclass of that class.
Syntax:
class protected access
{
protected:
int x;
void display();
}
Program:
#include<iostream.h>
#include<conio.h>
class bca
{
public:
private:
protected:
};
class abc: private bca
{
public:
void output()
{
cout<<”base class
created”;
}
public:
void show()
{
cout<<”derived class
created”;
}
};
void main()
{
abc ob;
ob.output();
ob.show();
getch();
}
Output:
Base class created
Derived class created
No comments:
Post a Comment