Monday 16 December 2013

Oops with c++

Real time examples of major concepts with keyboard as an object:

Object:

  • Consider a keyboard as an object.
  • It can have all functionalities like inheritance,polymorphism and etc.,
Class:
class keyboard
{
               private:
                             int num;
               public: 
                 void getdata();
                           {
                                      cout<<"Enter the number of keys";
                                      cin>>num;
                           }
          }; 
In the above example, 

  • The name of the class is "keyboard". 
  • The data member is "num" and its data type is integer.
  • The member function is "getdata" and is used to read the number of keys in the keyboard.
Inheritance:

  • The process of inheriting a new class (derived) from an existing class (base class) is called inheritance.
Example:

class keyboard
{
                private:
                                int num;
                public:
                               void getdata()
                               {
                                     cout<<"Enter the number of keys";
                                }
         };

         /*Deriving a class "functional_keys" from the base class "keyboard"*/

         class functional_keys:public keyboard
         {
                private:
                             int num;
                public:
                             void getdata()
                              {
                                          cout<<" Enter the number of function keys";
                                          cin>>num;
                               }
           };

In the above example, keyboard is the base class and from the base class "functional_keys" is derived.The derived class inherits all the property of base class.

Polymorphism:


  • The ability of an object to respond differently at different situations is called as polymorphism
Example:

    Consider a keyboard as an object, it can be used for different purpose at different situations.
For example, the user may have to type a business document where he may needs to use the alphabetical keys, punctuation keys and for some easy or quick access he may use the function keys.Here the characteristics of a single object is differed at different situations.Hence the characteristics of keyboard are:

  • Alphabetical
  • Punctuation
  • Functional


No comments:

Post a Comment