Objects and classes, Relationship between classes and objects
Object Oriented Programming is the most commonly used type of programming technique. This technique is very useful in writing long and complex programs easily. OOP technique provides advanced programming features because it uses real world modeling technique to solve a problem.
Object: Any thing that has state and behavior is called an objected. In real world modeling we observe the actual characteristics of an object and functions performed by the object. For example a person, teacher, vehicle, place is an object in real world. The name of the person is used to define the property of an object. Price of the vehicle is used to define the properties of an object. Same like that a teacher teaches a class so this is the behavior of that object. Vehicle is used to transport some articles from one place to another. These are the functions of an object.
In computer programming, the use
of GUI (graphical user interface) is the combination of various objects like
form, buttons, text boxes and many others. These components which are also
considered as tools/controls and objects have properties like name, color,
size, style, and caption. They also have behavior like click, double click,
load and some others. They are also considered as functions of an object.
Class: Class is simply the combination of data and the functions applied on that data. Class is also considered as set of objects which share their common characteristics like all the buttons on a form belong to a single class of button as all the buttons surely share their common characteristics.
Classes are used in programming phase to define objects.
Writing a class doesn’t mean that we are creating an object. Classes are just
template or sketch that tells us that the object created by that class will how
look like and what functions can be performed by these objects.
There is strong relationship between classes and objects that classes are used to define objects. And a program in execution with its functions and data members are called object of that classes. More than one object can be created from one class. Same like that so many houses can be built from one sketch of house.
Class is the template and sketch that combines data member and member functions applied on data. These data members are normally declared as private and member functions are declare public, so these data members may be access by these member functions within or outside the class. A class is written in the same way as structure is written. But structure only combines data members. But class combines data members and member functions also.
When we write a class it doesn’t create any data member of function in memory. To allocate memory space to the data members and to execute member function, we must have to create the object of that class. Object creation or class instantiation is required to allocate memory space to data members and to execute or call member functions. We can create as many object as we require for our program.
Access Specifiers in OOP
The use of class is very important feature of OOP. By using
class we can restrict the use of member variables and member functions. This
feature makes our programming more efficient and meets with our requirements. We
can allow functions of same and child classes to access them easily.
Basically there are 2 types of access Specifiers private and public. Private access Specifiers are used to restrict the members of the class to be used with in the class, not outside of the class. It provides security that the members of the class cannot accessed or changed outside the class. This is the default behavior. It means if we don’t use the keyword private then the compiler automatically consider that the members of the class are private.
If we declare data members or member functions with public keyword it means that the members can be accessed within or outside the class anywhere in the program. They can also be used in main function which is the starting point of ever program. Normally the member functions of the class are declared with public keyword. These member functions can access private data members as well within or outside the class as well as in the main function where we call these functions for execution.
The private data members cannot be accessed outside the class. If we try to access them with private member functions then compiler will generate an error that we cannot access the private members outside the class. So member functions declared with public keyword can access them for data storing and manipulation with in the class, outside the class and in main function.
CONSTRUCTOR
When we create an
object/instantiation of a class, then a member function of the class with the
same as class will implicitly executed. A class is just a sketch and used for
defining the data variables and functions of the object. So writing the code of
class doesn’t create any object or allocate memory space for variables and
functions. We have to create an object
A special type of function which is called constructor is executed whenever we create an object of a class in main function. This function has no return type. The constructor can work as normal function. It allocates memory space for member variables and also execute member functions. We can use it in class to initialize some variables or just show a message on screen when the object is created.
DESTRUCTOR
When constructor is executed and
allocated memory space to the variables declared, then after execution of that
function that reserved space which is allocated to the variables in RAM must be
released or evacuated so that this space may be utilized for other programs.
After the constructor is executed then destructor will also be executed to release that space. Whenever the constructor is executed, destructor will also be executed. If there are 2 or 3 or more objects of a class then destructor will also be executed as many times as objects are being used. Destructor is written in the same manner as constructor but a ~ tilde sign is added with its name to indicate that this is a destructor.
Inheritance and it feature and benefits
Object oriented programming technique provides us different features for writing long programs with efficient way. One of the most important features of object oriented programming is inheritance. When we write a class which has several member functions and data members and we have to use the features of the existing class in another class without rewriting the same code again then we inherit the class. All the features of the inherited class will be added to the new class which is inheriting. The class which is inherited is called super class, base class or parent class. The class which is inheriting the properties and functions of an existing class is known as subclass, derived class, child class.
Inheritance is one of the most powerful features of object oriented programming. The basic use of inheritance is that each sub class shares common properties with the base class. Child class can add its own capabilities also. The features of inheritance are given below.
REUSABILITY
The features of Inheritance allow
the programmer to reuse the existing code in many situations without rewriting
the code again. A class can be created one time and its functionality can be
reused again and again in other classes.
SAVE TIME AND EFFORT
We
inherit a base class to reuse its features in child class. It also saves our
time and effort in doing the same thing. Lots of time and effort can be saved.
INCREASED PROGRAM STRUCTURE AND RELIABILITY
The main class or super class is
already compiled and tested properly. This class can be used in a new
application without compiling it again. It plays very important role in visual
programming where we use many graphical objects which are derived from one main
class. If a class has some important feature for input, processing and output
then we don’t have to do the same task again. We just use it by inheriting its
code and can focus on developing a well structure program.
Different types of inheritance, levels of inheritance
TYPES OF INHERITANCE
A parent class can be inherited using private, public and
protected type of inheritance. These 3 keywords define the access of the parent
class members in child class.
PUBLIC INHERITANCE
In public inheritance, the child or derived class access the
parent or base class in such a way that the member functions or data members
declared in parent class with public keyword will become the public members of
derived class.
The members of parent class declared with protected class will become the protected members of the derived class.
The members of the parent class declared with private keyword will become the private members of the child class.
The derived class can access the public members of the parent class.
The derived class can access the protected members of the
parent class.
The derived class cannot access the private members of the
parent class.
PROTECTED INHERITANCE
When we inherit a class in
protected way of inheritance, the public members of the parent class become the
protected members of the derived class. The protected and private members of
the parent class will remain protected and private in child class.
The derived class can access the public members of the parent class. Public members will become protected.
The derived class can access the protected members of the
parent class.
The derived class cannot access the private members of the
parent class.
PRIVATE INHERITANCE
In private inheritance, the
private, protected and public members of the parent class will become the
private members of the derived class. Their access will be restricted as
private member may not be accessed outside the class.
CATEGORIES AND LEVELS OF INHERITANCE
There are different categories of inheritance like single
inheritance and multiple inheritances.
Single Inheritance
A type of inheritance in which a child class is derived from
single parent class is called single inheritance. In which one base, super,
parent, base class is derived by a child, dependent class.
Multiple inheritances
A type of inheritance in which a
child class is derived from multiple parent or base classes is called multiple
inheritance. The child class in this inheritance inherits all the data members
and member functions of the parent classes. The child class can also add its own
functionalities in it as well the functionalities of the base and parent
classes.
Level of inheritance
level of inheritance means that a
class inherits a base class and the child class is further inherited by another
child class. This type of inheritance is
considered as multiple level of inheritance.
The members of parent class are inherited to the child class and the
members of child class are inherited to the grand child class. The members of
parent and child class are combined in grand child class so they can be used
easily in the grand child class.
Function overriding by using
classes with example
Answer: As we know that classes are the combination of data members and member functions. We can use these member functions and data members by inheriting these functions in the child class. Some time we can declare two functions separately in parent and child classes with same name. When we use same name and same signatures (means parameters in argument list) for declaring 2 functions then this type of method is called function overriding.
The object of the derived class
cannot access that function of parent class because they have same name and
signatures. But we can access that function of the parent class by using scope
resolution operator with the name of the parent class in the body of the child
class.
{
Protected:
int m;
public:
{
}
};
Class b
{
Private:
int n;
public:
void c ( ) //function with same name in child class. c is the name of the function which is overridden
{
a::c ( ); //calling
of parent class function with scope resolution operator
}
};
14 Comments
Objects and classes are defined very well
ReplyDeleteSuperb
DeleteVery informative
ReplyDeleteperfect information for readers
ReplyDeleteSuperb work
ReplyDeleteClassic examples
ReplyDeleteVery informative 👏
ReplyDeletegood introduction of objects and classes
ReplyDeleteExperience
ReplyDeleteSuperb
ReplyDeleteAmazing text
ReplyDeleteInteresting stuff
ReplyDeleteWell explained
ReplyDeleteGood stuff
ReplyDelete