Class: A class is a way to bind the data and its associated functions together. It allows the
data (and functions) to be hidden, if necessary, from external use. When defining a class, we
are creating a new abstract data type that can be treated like any other build-in data type.
Generally, a class specification has two parts:
1. Class declaration
2. Class function definitions
The class declaration describes the type scope of its members. The class function definitions
describe how the class functions are implemented.
The general form of a class declaration is:
class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
visibility mode:
In inheritance whenever the derived class inherits from the base class than which
of the member of the parent class can be accessible in the child class is
controlled by the visibility mode. By default visibility mode is always set to private.
yntax is:
class derived_class_name :: visibility_mode base_class_name
{
//Lines of code
}
Types of Visibility Mode in C++
There are total 3 types of visibility mode in C++ that are:
, 1. Private visibility mode,
2. Protected visibility mode,
3. Public visibility mode
1. Private visibility mode:
When we inherit a derived class from the base class with private visibility mode
then the public and protected members of the base class become private
members of the derived class.
#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : private X{
//As the visibility mode is private none of the member of base class is inherited to derived class
};
int main()
{
//Nothing can be accessed using Class Y object because there are no members in class Y.
return 0;
, }
2. Protected visibility mode:
When we inherit a derived class from a base class with protected visibility mode
the protected and public members of the base class become protected members
of the derived class
#include <iostream>
class X{
private:
int a;
protected:
int b;
public:
int c;
};
class Y : protected X{
//As the visibility mode is protected the protected and public members of class X becomes the protected
members of Class Y
//protected: int b,int c inherited from class X
};
int main()
{
//As the members in the class Y are protected they cannot be accessed in main using Class Y object.
return 0;
}