The names gives us a clue to what it is.
Programming in which we use objects to design our software is called object oriented programming.
An object is an instance of a class for example in the declaration int i, i is an object of type integer.
If you are familiar with C you would already know what a structure is. It is a collection of data and functions for example
If you are familiar with C you would already know what a structure is. It is a collection of data and functions for example
typedef struct employee
{
char firstName[10];
char lastName[10];
};
The equivalent of struct in C++ is a class. A collection of data members and functions which operate on them. For example
There are two concepts in the above code data hiding and inheritance. Data hiding is achieved by access specifiers like public, private and protected key words within a class. The most important difference between a Class and struct is access specifier. That is all the members in struct are public by default and all the members in a class are private by default.
Access specifiers :
public : It is visible and available to all the member functions and objects.
protected : It is visible only to member functions and member functions of derived class.
private : It is visible only to member functions of its own class.
Access specifiers are only meant to restrict access to the world outside the class .i.e all members functions within the class have access to all its members. You don't have to ask, to access anything in your own house, do you ?
You give your guest access only to your living room and not to your safe or bedroom right ?
Now taking the same example I will tell you about all the three access specifiers.
public :
Living Room
protected :
Kitchen
Dining hall
private :
Bed Room
Personal Bathroom
An object is created outside the class, so it belongs to outside world, think of it as guests and hence it has access only to its public members.
One thing to remember here is private members are strictly private and cannot be accessed by anything outside the class.
To understand the protected access specifier you need to know about inheritance.
Inheritance is a concept which allows code reusability. If you need to be able to give access only to another class which is inherited from a base class and not to objects. As said earlier public access specifier gives access to outside world and private does not give access to anything outside the class. If we need something which can be accessed outside the class in only a specific class and not outside, here comes the protected access specifier at your service.
Coming back to the house analogy treat derived classes as your cousins or relatives who do have access to your Kitchen and Dining hall.
A picture is worth a 1000 words, I hope the below picture proves the same.
Members in a derived class have access to protected and public members of the base class. But the accessibility for the objects created from the derived class d1 and d2 above is based on its inheritance type, whether it is public or private. d1 can access public members of Base but d2 cannot access public members of Base.
The main properties of object oriented programing are :
1) Inheritance
2) Abstraction
3) Encapsulation
4) Polymorphism
Inheritance
The ability to inherit the existing or already implemented methods/functions of an existing class is called inheritance.
Abstraction
We do not want to expose the engine and its connections in a vehicle since the user does not need to know.Abstraction is a similar property in Object Oriented Programing in theory. We do not want to expose for example SSN number to the external user of our class.
Encapsulation
It is similar to abstraction from the implementation point of view. Ability to hide the implementation part of any method or hide data which is not meant to be visible to the user(object of a class).
Polymorphism
Its an ability to have the same method name with different implementations you can even have the same signature but have different behavior. So essentially there are two types of polymorphism one in which we use the same method/function name but have different signatures and second one with same signature. For the second one we need the concept of virtual functions.
Multiple Inheritance
Another interesting concept in inheritance is multiple inheritance. Its an ability to derive from more than one class.
For example :
Coming soon!
Static Member Functions
Static member functions in C++ can be accessed without an object using class name and scope resolution operator.An example of a use case would be to display the number of objects created without creating an object.
Here is an example of static member function
http://codepad.org/9VMGdlY9
Virtual Functions
Its one of the difficult concepts of object oriented programming, but only till you understand with an example.
Its about calling a derived class method from a base class object based on the address of the derived class object. This does sound confusing to me as well. Lets take a look at actual code example.
http://codepad.org/3P9hTEwV
In the code example above we expect an output from each of the derived class but we get one each from the base class.
The virtual key word will fix this issue for us. This is virtual functions, there is no more complications
http://codepad.org/QC3L5dc7
Pure Virtual Function :
We now know how to make a virtual function. If you only want the objects to be made out of derived class and none out of the base class we can do that using a pure virtual function.
To make a virtual function a pure virtual function we only need to remove the definition of the function and make the function equal to zero.
http://codepad.org/GPvTpHpI
A class which has pure virtual function is called an abstract class which cannot have an object created of its own.
Its about calling a derived class method from a base class object based on the address of the derived class object. This does sound confusing to me as well. Lets take a look at actual code example.
http://codepad.org/3P9hTEwV
In the code example above we expect an output from each of the derived class but we get one each from the base class.
The virtual key word will fix this issue for us. This is virtual functions, there is no more complications
http://codepad.org/QC3L5dc7
Pure Virtual Function :
We now know how to make a virtual function. If you only want the objects to be made out of derived class and none out of the base class we can do that using a pure virtual function.
To make a virtual function a pure virtual function we only need to remove the definition of the function and make the function equal to zero.
http://codepad.org/GPvTpHpI
A class which has pure virtual function is called an abstract class which cannot have an object created of its own.
The explanation is detailed and easy to understand for a person with basic programming knowledge. Keep posting more concepts.Good work Arun!
ReplyDeleteVery helpful and detailed with good examples! Helps get good understanding of basic concepts. Please keep adding more Arun! Thank you and great job!
ReplyDelete