Saturday, 19 September 2015

Concept of C++ classes And examples from real life

Till today we have studied that what is object oriented programming and what makes it useful as compared to procedural c++ programming.

Now it’s time to move on to the next level. We will first define that what is class?
As we have come across   the word class in our previous tutorials. So instead wasting more time, I think it is the appropriate time to explain the importance and definition of class.

A class is just a template, blue print or a carbon copy which do not have any physical existence. Physical existence means that it does not occupy a single byte of memory. You can also relate a class to a plan which does not have any physical existence. You can relate it with a map of a building. As we know that map has only architect of building but building do not actually exist. Map is just a plan or a template which tell us that building when build  should look like that.

A class is a mechanism using which we can instantiate our objects. Common things from objects are placed in a class and then each object is instantiated from it. We will discuss in details about objects after completing class section.

A class contain attributes which are also called data member or fields and behaviors which are also called method,member function or services of the class. Behaviors provided  in the class acts on the attributes and manipulate its value or state.

  Let’s take some examples from our real life to explain the concept of class.

Consider the example of Animal, animal is a class and we can drive  or instantiate objects from this class.We can drive following objects from the
class animal:
   (1) Man
   (2) Dog
   (3) Cat
   (4) Monkey
   (5) Eagle
       
These are the objects which we instantiate now we will put common attributes and behavior in our class animal and will instantiate objects from the class animal. It is important to note that we can also make our objects like human and monkey etc. a class and can instantiate objects from it but
It depends on requirement or problem statement.

Now take another example to further elaborate  the concept of classes in c++ programming.
Consider a class vehicle, from vehicle class we can instantiate many objects like

(1) Car
(2) Bus
(3) Truck
(4) Bike

Now these objects have some attributes and behavior consider following common attributes and behaviors of these objects:

(1) Regno
(2) Modal
(3) Color
(4) Price

 Behaviors

 (1) Drive
  (2) Stop
  (3) Accelerate
  (4) Brake


These all attributes and behavior are placed with in a class body. Now here object oriented programming in c++ comes to work, now we have only to instantiate object and our object will have all these attributes and behavior. It may feel strange to you that how to instantiate object from a
class. Do not worry about  that we will explain it latter. The basic purpose of this section is to let you know the basic purpose of class and give you understanding of what actually a class is, what it contains and how it is the best use of object oriented programming. I have explained all this by taking examples from real life.

Now if you have further queries about class you can email me at jawad1647@gmail.com, I will try to solve all your queries and also please don’t hesitate to report mistakes as you know to "err is a human". You can report the errors by sending me a mail at the email address given above.


0 comments:

Post a Comment

Popular Posts