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();


}

Wednesday, 18 September 2013

SOFT ROBOT CHANGES COLOR

Researchers have developed a system — inspired by nature — that allows the soft robots to either camouflage themselves against a background, or to make bold color displays. Such a “dynamic coloration” system could one day have a host of uses, ranging from helping doctors plan complex surgeries to acting as a visual marker to help search crews following a disaster.

Just as with the soft robots, the “color layers” used in the camouflage start as molds created using 3-D printers. Silicone is then poured into the molds to create micro-channels, which are topped with another layer of silicone. The layers can be created as a separate sheet that sits atop the soft robots, or incorporated directly into their structure. Once created, researchers can pump colored liquids into the channels, causing the robot to mimic the colors and patterns of its environment.The system’s camouflage capabilities aren’t limited to visible colors though.
When we began working on soft robots, we were inspired by soft organisms, including octopi and squid,” Morin said. “One of the fascinating characteristics of these animals is their ability to control their appearance, and that inspired us to take this idea further and explore dynamic coloration. I think the important thing we’ve shown in this paper is that even when using simple systems — in this case we have simple, open-ended micro-channels — you can achieve a great deal in terms of your ability to camouflage an object, or to display where an object is.”

“One of the most interesting questions in science is, ‘Why do animals have the shape and color and capabilities that they do?’ ” said Whitesides. “Evolution might lead to a particular form, but why? One function of our work on robotics is to give us, and others interested in this kind of question, systems that we can use to test ideas. Here the question might be: ‘How does a small crawling organism most efficiently disguise (or advertise) itself in leaves?’ These robots are test-beds for ideas about form and color and movement.”

 HOW THEY CHANGE COLOR?

Just as with the soft robots, the “color layers” used in the camouflage start as molds created using 3-D printers. Silicone is then poured into the molds to create micro-channels, which are topped with another layer of silicone. The layers can be created as a separate sheet that sits atop the soft robots, or incorporated directly into their structure. Once created, researchers can pump colored liquids into the channels, causing the robot to mimic the colors and patterns of its environment.
The system’s camouflage capabilities aren’t limited to visible colors though.
By pumping heated or cooled liquids into the channels, researchers can camouflage the robots thermally (infrared color). Other tests described in the Science paper used fluorescent liquids that allowed the color layers to literally glow in the dark.
Just as animals use color change to communicate, there are envisions about robots using the system as a way to signal their position, both to other robots, and to the public. As an example, the possible use of the soft machines during search and rescue operations following a disaster. In dimly lit conditions a robot that stands out from its surroundings (or even glows in the dark) could be useful in leading rescue crews trying to locate survivors.

Going forward, researchers hope to explore more complex systems that use multiple color layers to achieve finer control over camouflage and display colors, as well as ways to create systems — using valves and other controls — that  
the robots to operate autonomously.

 Future Applications

For defense applications, ingenuity and efficiency are not enough—robotic systems must also be cost effective. This novel robot is a significant advance towards achieving all three goals.”
In the video above, a soft robot walks onto a bed of rocks and is filled with fluid to match the color of the rocks and break up the robot’s shape. The robot moves at a speed of approximately 40 meters per hour; absent the colored fluid, it can move at approximately 67 meters per hour.
Future research will focus on smoothing the movements; however, speed is less important than the robot’s flexibility. Soft robots are useful because they are resilient and can maneuver through very constrained spaces.
For this demonstration, the researchers used tethers to attach the control system and pump pressurized gases and liquids into  the robot. Tethered operation reduces the size and weight of such robots by leaving power sources and pumps off-board, but future prototypes could incorporate that
equipment in a self-contained system.
At a pumping rate of 2.25 milliliters per minute, color change in the robot required 30 seconds. Once filled, the color layers require no power to sustain the color.
Aside from their potential tactical value, soft robots with microfluidic channels could also have medical applications. The devices could simulate fluid vessels and muscle motion for realistic modeling or training, and may be used in prosthetic technology.
The system might one day have applications ranging from helping doctors plan complex surgeries to acting as a visual marker to help search crews following a disaster

Tuesday, 17 September 2013

pointer

Q.Write a "C" Program using function to swap to number

#include<stdio.h>
#include<conio.h>
/* function prototype */

void swap(int* ,int*);

void main()
{
     int a,b;

     clrscr();

       printf("\nEnter the value of a and b:");
     scanf("%d %d",&a,&b);

     printf("\nBefore Swap , a = %d , b=%d",a,b);

     swap(&a,&b); /*function call */

     printf("\nAfter Swap , a = %d , b=%d",a,b);

     getch();
}

/*function definition */

void swap(int *a,int *b)
{
     int c;

     c=*a;
     *a=*b;
     *b=c;


}
    
Find the output of the following program :

(i)
     #include<stdio.h>    
     #include<conio.h>
     main()
     {
          int i=4,j=5;

          junk(i,&j);

          printf("\n %d %d",i,j);
     }
     junk(int i,int *j)
     {
          i=i*i;

          *j=*j**j;
     }


i=4
j=25




             
#include<stdio.h>
#include<conio.h>
void main()
{
     int i=16;

     float f=3.14;

     char c='A';

     char *cc;

     cc=&c;

     printf("\n cc = %u  , *cc= %c",cc,*cc);

     cc=&i;

     printf("\n cc = %u  , *cc= %c",cc,*cc);

     cc=&f;

     printf("\n cc = %u  , *cc= %c",cc,*cc);

     getch();
}


####################################################################






Reference Variables :
The reference variable will store the reference of the variable. The general form of declaring the reference variable is ,
     datatype & referencevariablename=variablename;
e.g.
     int a=10;
     int &b=a;
     Now, b will be the reference variable and it will reference to a.
The reference variable is prefixed by an ampersand sign at the time of its declaration.     As , the pointer variable will store the address of the variable , the reference variable will store    the     refernce of the variable. That is , it will reference to the same location to which the variable    is referencing to.
     In case of pointers ,
          int a=10;
          int *b;
          b=&a;
                   

     In case of reference variable,
          int a=10;
          int &b=a;
              

          So , if we make any change in b , the change will get reflected in a and vice versa.
In c++, the reference variable can be used in three different ways ,

     (a) Independent Reference variable
     (b) Passing references to the functions
     (c) Returning references from the function.

     (a) Independent References Variables :
          Consider the following programs ,
     #include<iostream.h>
     #include<conio.h>
     void main()
     {
          int a=10;
          int &b=a;
cout<<endl<<"a= " << a <<" b="<<b;
          a=a+2;
          cout<<endl<<"a= " << a <<" b="<<b;
          b=b-1;
cout<<endl<<"a= " << a <<" b="<<b;
          int c=50;
          b=c;

cout<<endl<<"a= " << a <<" b="<<b<<"c="<<c;
          b=b+5;
          cout<<endl<<"a= " << a <<" b="<<b<<"c="<<c;
          getch();
}
output :
a=10 b=10
a=12 b=12
a=11 b=11
a=50 b=50 c=50
a=55 b=55 c=50
(b) Passing references to the function
Consider the following program ,

#include<iostream.h>
#include<conio.h>
/*function prototype*/
void swap(int & , int &);
void main()
{
     int a,b;

     clrscr();
cout<<endl<<"Enter the value of a and b:";
     cin>>a>>b;
cout<<endl<<"Before Swap a="<<a<<" b= "<<b;
swap(a,b); /*function call*/
cout<<endl<<"After Swap a="<<a<<" b="<<b;
getch();
}
/*function definition*/
void swap(int & x , int &y)
{
     int z;

     z=x;

     x=y;

     y=z;
}

Now , we have used the reference variables in the formal arguments ,so they will reference to the actual arguments , so the changes which we make in the formal arguments will also get reflected in the actual arguments.This is known as Call By Reference

(c) Returning References from the functions .

     Consider the following program ,

     #include<iosteam.h>
     #include<conio.h>
     /*fucntion prototypr*/

     char & func(int);

     /*global declaration */

          char s[ ] = "Hello World";

     void main()
     {
          func(5)='*'; /*function call*/

    
          cout<<endl<<s;
    
          getch();
     }

     /*function defintion */

     char & func(int pos)
     {

          return s[pos];
     }


output : Hello*World

Monday, 16 September 2013


Q Find the output of the following programs :

          (i)
                             #include<stdio.h>
                             #include<conio.h>
                             void main()
                             {
                                      int a=5;

                                      int *b;

                                      b=&a;

printf("\nValue of a = %d" ,a);   ==>       Value of a = 5

printf("\nValue of a = %d" ,*(&a)); ==> Value of a = *(1000) ==> 5
printf("\nValue of a = "%d ,*b);   ==> Value of a = *b ==>*1000==>5

printf("\nAddress of a = %u" ,&a); ===> Address of a = 1000

printf("\nAddress of a = %u" ,b); ===> Address of a = 1000

printf("\nValue of b = %u" ,&a);===> Value of b = &a===> Address of a = 1000

printf("\nValue of b = %u" ,b); ===> Value of b = b===> Address of a = 1000


printf("\nAddress of b = %u",&b);===> Address of b =&b===> Address of b = 2000


          getch();

}

                             output :

                                



 (ii)

          #include<stdio.h>
          #include<conio.h>
                  
          void main()
          {
                   int a=5; /* Integer type variable */

                   int *b=&a; /* Pointer to an integer */              

                   int **c=&b; /* Pointer to a Pointer to an Integer */

                   int ***d=&c; /* Pointer to Pointer to Pointer to an Integer */



                   printf("\n%d%u%u%u",a,b,c,d);


                   printf("\n%d %d %d ",a+*b;*b+**c,*b+**c+***d);

                   getch();
}

output :



5 1000 2000 3000

a+*b ==>   5 + *(1000) ==> 5+5 = 10

*b+**c ==> *1000+ **2000 ==> 5 + *1000 ==> 5+5 = 10

*b+**c+***d===> *1000+**2000+***3000

                   5+*1000+**2000
                   5+5+*1000
                   5+5+5
                   15

(iii)

          #include<stdio.h>
          #include<conio.h>
          void main()
          {
                   int *p=4000;

                   printf("\n p=%u " , p);

                   p=p+1;

                   printf("\n p+1=%u",p);

          }

          %u is the format specifier used for printing the address . The address is always 16 bit and %d will point to 15 bit and they consider the one bit for storing the sign bit. And in case of %u, that is , unsigned all the 16   bits are used for storing the value.

          

          When ever we increament in the address , it will points to the next location  of that type.
          p=p+1=4000+1=4002

          As , each integer is of 2 bytes

(iv) main()
          {
                   char *c=4000;
                   int *i=4000;
                   float *f=4000;
                   double *d=4000;
                   long *l=4000;
                  
                   printf("\nc=%u , c+1=%u",c,c+1);
                   printf("\ni=%u , i+1=%u",i,i+1);
                   printf("\nf=%u , f+1=%u",f,f+1);
                   printf("\nd=%u , d+1=%u",d,d+1);
                   printf("\nl=%u , l+1=%u",l,l+1);
          }

===================================================
          Datatype                                          Size in Bytes
===================================================
          char                                                            1 byte
          int                                                               2 bytes
          float                                                  4 bytes
          double                                                       8 bytes
          long                                                  4 bytes
===================================================

So,the result is ,
          c=4000      , c+1=4001

          i=4000 , i+1= 4002

          f=4000 , f+1 = 4004

          d =4000 , d+1 = 4008


          l = 4000 , l+1=4004