Thursday, 3 October 2013

Inheritance

Inheritance
When we make a class on the basis of the existing class is known as inheritance. The existing class in known as  Base class( or parent class) and the new class is known as derived class
( or child class)

The general form is ,

        class DerivedClassName : BaseClassName
        {
        body of the Derived Class
        }



Here we have 5 types of inheritance
1) single inheritance
2 multilevel
3 multiple
4 hierarchial

5 hybride 
single  inheritance

                
In the case of Single Inheritance , we derive the single derived class , on the basis of the single base class.
 
 # include <iostream.h>
    # include <conio.h>
          class A
          {
            int a;
        public:
        void setA(int i)
        {
        a=i;
        }
        void showA( )
        {
        cout<<"a="<<a;
        }
             };

         class B : public A
        {
        int b;
        public:
        void setB(int j)
{
b=j;
        }
 void showB( )
        {
cout<<"b="<<b;
               
               }
               };

                 void main()
          {
 B ob;
ob.setA(5);
ob.setB(10);
ob.showA();
ob.showB();
getch();
}

private members :
                In the case of inheritance ,  the private memebers have the following features,
(i) These members are not directly accesable by the                         object of a class.

                        e.g.
                                ob.b=10; is Invalid

(ii) These members will not get inherited.That is, they cannot  be used in the derived class or by the object of the derived class.
                                        showB()
                                        cout<<"a="<<a;
                        it will be invalid

public members :
        In the case of inheritance ,  the public  memebers have the following features,

(i) These members are  directly accesable by the                      object of a class.
                        e.g.
                                ob.setA(5) ; is valid

(ii) These members will  get inherited. That is, they can  be used in the derived class  or by the object of the derived class.

                        B ob;
       
                        ob.showA();   

Tuesday, 1 October 2013

constructors

Constructors
 constructors are the special data member of the class,
they have some special property are as follows:-
1) Same name of the class name .
2) have no return type.
3) No need to call in the main ,they get called when the object  is created.
General form of the constructor is :-
class name ()
{
body of the constructor
}
there are three types of constructor

1) default constructor
2) parameterised constructor

3)copy constructor
1) Default cons

   consider the following code
#include < iostream.h>
#include <conio.h>
class Sample
{
int num;

Sample ()        //constructor
{
cout<<"constructor called"<<num;
}
};
void main()
{
Sample ob;
getch();
>>>>>>>>>>>Copy constructor<<<<<<<<<<<<<<<<
It is used to Copy one object into another



classname ( Class Name   & Object name)
{
body
}

Eg-->

class Sample
{
int i;
public:
Sample()        //default
{
i=0;
}
Sample(int a)   //parameterised
{
i=a;
}

Sample( Sample &ob)  //copy cons
{
i = ob.i;
}