Classes
The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types.
Class Definition:
A class is a way to bind the data describing an entity and its associated functions together.
In C++, the class makes a data type that is used to create objects of class type.
or
Class is a method of binding together properties and operations on those properties in a single logical unit.
or
Class is a method of binding together data elements and member functions into a single logical unit.OrClass is a combination of structure and functions.
When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations.
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types.
A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
Points to remember
* Keyword “class” is used to declare a class.
* Student in the above example is the name of the class.
* Roll, name, and age are known as data elements/member elements/data members/attributes/properties of the class student.
* Read() and show() are known as the member functions / methods / operations / implementation details of the class.
* Member function/operations of the class help us to perform operations on the data elements/properties of the class.
* Definition of a class is terminated by a semicolon (;).
* The data elements / properties and member functions of the class are declared using one of the following Program access levels / access methods / visibility levels / visibility modes/ access specifiers
Private
Protected
Public
Private:
The data elements and member functions of the class declared using private access specifier/level can be directly accessed by the following:
* Member functions of the class.
* Friends/friend functions of the class.
These cannot be directly accessed from outside the class, and if we try to access them an error will get generated.
Protected:
The data elements and member function declared using protected access specifier/level can be directly accessed by the following:
* Member functions of the class.
* Friends/friend functions of the class.
* Member functions of the derived class.
* Friends/friend functions of the derived class.
These cannot be directly accessed from outside the class and if we try to access them an error will get generated.
Note:
1. The data elements and member function declared using private or protected access levels cannot be directly accessed from outside the class i.e. they are not visible from outside the class i.e. they are hidden from outside the class and this is the concept of data hiding.
2. The concept of the derived class will come into use when we deal with inheritance so, till the time we are not using inheritance, the data elements and member function declared using private and protected access levels will behave in a similar manner.
3. If while declaring data elements and member functions access level is not specified then by default they are considered as private.
Public:
The data elements and member functions declared using public access specifier /level can be directly accessed from outside the class.
* The above defined class cannot be directly used while programming. We have to declare objects of type defined class and then use them while programming.