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;
}

Saturday, 28 September 2013

classes ad object(Q3)

Q . WAP to define a class Date which contains the data member , dd,mm, and yy.
     And , functions ,
              * readDate();
              * showDate();
              * increament();
              * decreament();
              * maxDays();
#include<iostream.h>
#include<conio.h>
class Date
{
     int dd,mm,yy;
     public:
     void readDate()
              {
                   cout<<endl<<"Enter the value of dd ,mm , and yy :";
                   cin>>dd>>mm>>yy;
              }

              void showDate() 
              {
                   cout<<endl<<dd<<":"<<mm<<":"<<yy;
                  }
              void increament ();
              void decreament(); 
                int maxDays();                       };

     int Date::maxDays()
     {
         
              switch(mm)
              {
                   case 1:
                   case 3:
                   case 5:
                   case 7:
                   case 8:
                   case 10:
                   case 12: 
                             return 31;
                   case 2:
                             if((yy%4==0&&yy%100!=0)||(yy%400==0))
                                  return 29;
                             else   
                                  return 28;
                   case 4:
                   case 6:
                   case 9:
                   case 11: 
                                  return 30;
          }
}
void Date::increament()
{  
          //increament the day
                  
              dd=dd+1;

          //check for the valid day range

              int max;

              max=maxDays();
              if(dd>max)
              {
//set day to the first day of the next month
          dd=1;
              //increament the month
                        mm=mm+1;
                        if(mm>12)
                        {
                             //set month to jan
                             mm=1;
                             //increament the year

                                  yy=yy+1;        
                  
                        }
              }
}

void Date::decreament()
{
         
          //decreament the day

              dd=dd-1;

          //check for the valid
              if(dd<1)
              {
                   //decreament the month
                    mm=mm-1;
                   if(mm<0)
                   {
                        //set month to dec
                        mm=12;
          //decreament the year

                             yy=yy-1;

                   }

                   //assign the day

                   dd=maxDays();

          }  
}

void main()
{
     Data ob;

     ob.readDate();

     ob.showDate();

     ob.increament();

     ob.showDate();

     ob.decreament();

     ob.showDate();

     getch();

}

Thursday, 26 September 2013

classes and object (Q2.)

Q WAP to define a class Time , which contains the data member , hh,mm and ss and functions ,
          * readTime();
          * showTime();
          * increament();
          * decreament();
Valid range :
     hh= 0-23
     mm=0-59
     ss=0-59
#include<iostream.h>
#include<conio.h>
class Time
{
     int hh,mm,ss;

     public:
          void readTime()
          {
              cout<<endl<<"Enter the value of hh , mm, and ss :";
              cin>>hh>>mm>>ss;
          }

          void showTime()
          {
               cout<<endl<<hh<<":"<<mm<<":"<<ss;
          }

          void decreament();
    
          void increament();

};
     void Time::increament()
          {
          //increament the second
              ss=ss+1;
//check for the seconds valid range
              if(ss>59)
              {
                   //set seconds to 0
                   ss=0;
                   //increament the minutes

                   mm=mm+1;

                   //check for the minutes valid range

                   if(mm>59)
                   {
                        //set the minutes to 0

                        mm=0;

                        //increament the hours

                        hh=hh+1;

                        //check the hours valid range

                        if(hh>23)
                             hh=0;
                   }
               }
          }
    
     void Time::decreament()
     {
          //decrease the seconds by 1

          ss=ss-1;

          //check for the valid range

          if(ss<0)
          {
              //set seconds to maximum

              ss=59;

     //decreament the minutes by 1

              mm=mm-1;

              //check for the valid range
             
              if(mm<0)
              {
                   //set mm to maximum

                   mm=59;

                   //decreament the hour by 1

                   hh=hh-1;

                   //check for the valid range

                   if(hh<0)
                        hh=23;
              }
          }
     }


void main()
{
     Time t;

     t.readTime();

     t.showTime();

     t.increament();

     t.showTime();

     t.decreament();

     t.showTime();

     getch();

}

Question of glass and object

/*
     Question : Define a class Number , which contains the data member num and
          functions ,
              * read the number
              * display the number
              * reverse the number
              * check whether the number is palindrome or not
              * check whether the number is armstrong or not

*/

#include<iostream.h>
#include<conio.h>
class Number
{
     int num;
     public:
          void readNum()
          {
cout<<endl<<"Enter the number :";
              cin>>num;
          }
          void showNum()
          {
cout<<endl<<"Number ="<<num;
          }
          int reverse();
          void palindrome();
          void armstrong();
};
int Number::reverse()
{
     int num2=num;
     int rev=0;
     while(num2!=0)
     {  
          rev=rev*10+num2%10;
          num2=num2/10;
     }
     return rev;
}
void Number::palindrome()
{
     int rev;

     rev=reverse();

     if(rev==num)
          cout<<endl<<"Palindrome";
     else
          cout<<endl<<"Not Palindrome";
}

void Number::armstrong( )
{
     int num2=num;

     int sum=0;

     int a;

     while(num2!=0)
     {
          a=(num2%10);
    
          sum=sum+a*a*a;

          num2=num2/10;
     }

     if(sum==num)
          cout<<endl<<"Armstrong";
     else
          cout<<endl<<"Not Armstrong";

}

void main()
{
     Number ob;
int num;
     ob.readNum();

     ob.showNum();
    num=ob.reverse();
cout<<”Reverse=”<<num;
     ob.palindrome();
     ob.armstrong();

     getch();

Tuesday, 24 September 2013

Function overloading
It refers to using the same function  name to perform the varity of different function
The difference in the Function call is made on the Basis of the following methods:
(1) Number of arguement
(2) Type of arguement
(3) Sequence of arguement
consider the following program:
 #include <iostream.h>
#include <conio.h>
void area( int side)
{
cout<<"Area of Square "<<side*side;
}
void area ( int length,int width)
{
cout<<length*width;
}
void area ( double radius)
{
cout <<3.14*radius*radius;
}
void main()
{
area(5,6);
area(8);
area (5.6);
getch();

}

Thursday, 19 September 2013

Macros :
Macros are symbolic constants and they are used for improving the readability.
The general syntax for defining the macro is ,
#define macroname macroexpansion

e.g.#define MAX 50
Now,     MAX  is a macro and where ever we write MAX  it will be considered equivalent to 50.
And also ,
MAX=60;
will results in an error, as MAX  is replaced by 50 , so the statement means ,
50=60;
as we cannot assign one constant into another so it will give an error .
Consider the following code ,
       #include<iostream.h>
       #include<conio.h>
       #define PI 3.14
       void main()
       {
              float radius,area,circum;

              clrscr();

              cout<<endl<<"Enter the radius of the circle :";
              cin>>radius;

              //calculate the area of the circle

              area=PI * radius * radius;

              // calculate the circumference of the circle

              circum  = 2 * PI  * radius ;

              cout<<endl<<"Area of circle :" << area;

              cout<<endl<<"Circumference of the circle :"<<circum;

              getch();


}