Thursday, May 30, 2019

Class and Objects

Class and Objects

Class: -
     It is a kind of user define data type, it contains attribute, methods, constructor, destructor.
also,
It describes a group of objects with similar properties. Classes are the natural way to implement abstract data type in c++.


 
Attribute      ->    variable       (class members)                         
Method       ->    function

or,
Class is the name of group, having similar property
Or,
Class is a kind of logical entity
Or,
Class is the blue print of object.

Object: -

It is instance of class, object is physical. It is the run-time entities in object-oriented programming. It is the variable of type class.
e.g.: -
student ----> class
{
          roll, name, address, mobile    ------> Attribute
          void display() ------> Method
}
-------> Class member


   Constructor

It Is just like method, used to initialize object
Main points are as follows :
i) It is just like method
ii) Constructor name and class name must be same
iii) Must be public
iv) Can’t return any value (Even void is not allowed)
v) Can be parameterized
vi) Can be overload
vii) Used to initialize the attribute of class
viii) We don’t need to call it, called automatically at the time of object creation
ix) When we don’t define any constructor, there will be default constructor

E.g.: -

 #include<iostream.h>
#include<conio.h>
class a
{
          private:
                             int x,y;
          public:
          a()
          {
                             x=65;
                             y=87;
                             cout<<”welcome”<<end;
          }
          //parameterized constructor
          a(int p,int q)
          {
                             x=p;
                             y=q;
          }
          void display
          {
                             cout<<x<<” “<<y;
          }
};
void main()
{
          clrscr();
          a       t; //call the default constructor
          t.display();
          a     z(33,44);//call the parameterized constructor
          z.display();
          getch();
}

                                                          Destructors

It is use to destroy the objects that have been created by a constructor.
Main points are as follows :
1.   Used to release the memory
2.   Destructors name and class name must be same
Except: ~
3.   We can’t apply any return type and parameter
E.g.:
 #include<iostream.h>
#include<conio.h>
class a
{
                   private:
                                      int x,y;
                   public:
                   a()
{
          count<<” constructor”;
          x=888;
          y=999;
}
~a()
{
          cout<<”bye!!!”;
}
void display()
{
          cout<<x<<” ”<<y;
}
}
void main()
{
          clrscr();
          a      t;
t.display();
a        r;
r.display();
getch();
}

(Note: -
Constructor: - Public---
If we define private constructor, we can’t create object of       class.)

     
     Method Overloading

The method having the same name but different signature in single class
Signature: Number of parameters and types of parameters
->Return type is note included in signature.
->Only return type change is note overloading
E.g.:
#include<iostream.h>
#include<conio.h>
class student
{
          public:
                   void display()
                   {
                             cout<<” method1”;
                   }
                   void display(int r)
                   {
                             cout<<” \n thanks and welcome”<<r;
                   }
                   void display(float x)
                   {
                             cout<<” bye bye!” <<x;
                   }
/*error
                   int display()
                   {
                             cout<<” error ”;
                   }
*/
};
void main()
{
          clrscr();
          student x;
          x.display(); //first
          x.display(525); //2nd method
          x.display(45.56f); //3rd method
          getch();
}

No comments:

Post a Comment