Friday, 21 June 2013

classes and objects


Class -A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.
Definition:
A class is a group of objects which show a common property.
OR
A class is a collection of objects of similar type.
Once a class is defined, any number of objects can be created which belong to that class.
For example – Define a class to print the values of two numbers.
Class Helloworld
{
    int one;
    int two;
    public:
    void setdata()
      {
        cout<<”enter two numbers”;
        cin>>one>>two;
      }
     void putdata()
      {
      cout<<one<<endl<<two;
      }
};
int main()
    {
     Hellowrold obj;
     Obj.setdata();
     Obj.putdata();
     return 0;
    }
Classes are generally declared using the keyword class, with the following format:
class class_name
{
access_specifier_1:
   member1;
access_specifier_2:
   member2;
...
};
Object -An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
                               Or
It may be defined as identifiable identity with some characteristics and behavior.
Syntax of creating Object:
Class_name Object_Name
If we have class named AB then the Object of this class can be created using above syntax as
AB Obj;
Abstract class
An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function.
You declare a pure virtual function by using a pure Specifier
(= 0) in the declaration of a virtual member function in the class declaration.
The following is an example of an abstract class:
class AB {
            public:
            virtual void f() = 0; //Pure virtual Function
             };
Concrete class – it is a derived class that implement all the missing functionality of a abstract class is called a concrete class
The following is an example of an concrete class: 
 class CD : public AB //Here class AB is abstract class
{                          // Declare Above
        public:
        CD()
           {   /* set up the CD */  }
       virtual f()   // implementation of f() of class AB
           {
           /* do stuff of f() */
           }
};

Advantages and disadvantages of object oriented programming
Advantages –
1- Reusability of code.
2- Ease of comprehension.
3- Ease of fabrication and maintenance.
4- Ease of redesign and extension.
5- Data can be divided as public and private.(data protection)
6- Program development becomes easy due to increased modularity.(abstraction and encapsulation)
Disadvantages –
  1. The relation among classes become artificial at times
  2.  The object oriented programming design id difficult.
  3.  OOP is a high level concept so takes more time to execute as many routines run behind at the time of execution.
  4.  Offers less number of functions as compared to low level programming which interacts directly with hardware.
  5.  Increased burden on part of OOP developer.

No comments:

Post a Comment