Showing posts with label oops. Show all posts
Showing posts with label oops. Show all posts

Tuesday, November 24, 2015

What are the different between object oriented programming language and procedural programming language?

Solution:-

 

Procedure oriented programming(C)

Object oriented programming (C++)

1

Emphasizing in doing (following flowchart)

1

Emphasize on data

2

Large program divided into function

2

Large program divided into objects

3

Data can be freely accessed by function around the program

3

Data are hidden and cannot be accessed by external functions

4

Function can transfer data from one from to other

4

Data are not transferred but object common action is done

5

It is base on Top-down approach

5

It is base on Bottom-up approach

6

Eg:- FORTRAN,COBOL, Pascal, C

6

Eg:-  C++, JAVA, Smalltalk

Tuesday, October 06, 2015

Write a program to find the sum of first ‘n’ odd natural numbers, where ‘n’ is entered by the user.

Solution:-
     #include<conio.h>
     #include<stdio.h>
     void main ( )
     {
              int i,n,sum,odd;
              clrscr( );
              scanf("%d",&n);
              sum = 0;
              odd = 1;
              for(i=1;i<=n;i++)
              {
                      sum=sum+odd;
                      odd=odd+2;
              }
              printf("%d\t",sum);
              getch( );
     }
 

Write a program to display the series: 1, 8, 27, up to 10th term using for loop.

Solution:-
    #include<conio.h>
    #include<stdio.h>
    void main ( )
    {
            clrscr( ) ;
            int term,cube;
            printf(" Display the series 1, 8, 27, 64,  ........ up to 10th term : \n");
            for(term=1;term<=10;term++)
            {
                   cube=term*term*term;
                   printf("%d\t",cube);
            }
            getch( );
    }

Write a program to display the multiplication table of 1 to 10 using do while loop.

Solution:-
   #include<stdio.h>
   #include<conio.h>
   void main( )
   {
            int row,col,table;
            clrscr( );
            printf("Multiplication table 1 to 10 \n");
            row=1;
            do
            {
                   col=1;
                   do
                   {
                            table=row*col;
                            printf("%d\t",table);
                            col ++ ;
                   }
                   while(col<=10);
                   printf("\n");
                   row ++ ;
            }
            while(row<=10);
            getch( );
   }

Write a program to read two 3 × 3 matrices and calculate and display the sum of those two matrices.

Solution:-
#include<stdio.h>
#include<conio.h>
void main( )
{
       clrscr( );
       int i,j,total;
       int matrix[3][3],matrix1[3][3],sum[3][3];
       printf("Enter the 3*3 matrix \n");
       for(i=0;i<3;i++)
            {
                     for(j=0;j<3;j++)
                    {
                            scanf("%d",&matrix[i][j]);
                     }
             }
        printf("Enter the 3*3 matrix1 \n");
        for(i=0;i<3;i++)
        {
                 for(j=0;j<3;j++)
                {
                       scanf("%d",&matrix1[i][j]);
                 }
        }
        for(i=0;i<3;i++)
       {
                 for(j=0;j<3;j++)
                {
                        sum[i][j]=matrix[i][j]+matrix1[i][j];
                 }
        }
        for(i=0;i<3;i++)
        {
                  for(j=0;j<3;j++)
                 {
    
                        printf("%d\t ",sum[i][j]);
                 }
                 printf("\n");
        }
        getch( );
}
 

Sunday, October 04, 2015

Write a program to take marks of 15 students and print them out in descending order of marks.

Solution:-
     #include<stdio.h>
     #include<conio.h>
     void main( )
     {
            int a[15],temp=0,i,j;
            clrscr( );
            printf("Enter the 15students marks : \n");
            for(i=0;i<15;i++)
            {
                      printf("\n Enter the mark %d \t",i+1);
                      scanf("%d",&a[i]);
             }
             for(i=0;i<15;i++)
             {
                      for(j=i+1;j<15;j++)
                      {
                                 if(a[i]<a[j])
                                 { 
                                           temp=a[i];
                                           a[i]=a[j];
                                           a[j]=temp;
                                  }
                       }
             }
             printf("\n The marks in desending order are : \n");
             for(i=0;i<15;i++)
             {
                        printf("%d \t",a[i]);
             }
             getch( );
     }

 

Saturday, October 03, 2015

Write a program to reverse an integer number. (Hint: if number is 135, it should be displayed as 531)

Solution:-
     #include<stdio.h>
     #include<conio.h>
     void main( )
     {
             clrscr( );
             long int num,rem;
             printf("Enter the integer number: \n");
             scanf("%ld",&num);
             while( num ! = 0 )
             {
                       rem = num % 10 ;
                       printf("%ld",rem);
                       num=num/10;
              }
              getch( );
     }

Sunday, September 20, 2015

About constructor and destructor with suitable example.

Solution:-
Constructor:-
          A constructor is a special member function for automatic initialisation of an object. Has the same name as the class it belongs to. It can be declared and defined within the class, or can be declared within the class and defined outside the class. It has no return type is used for constructors.

Destructor:-
          A destructor is a member function that is called automatically when an object is destroyed. It cannot be directly called from the class. The compiler itself generates a call to a destructor when an object expires. Same name as the class but with a tilde (~) before the class name. Destructors have no return type and they take no arguments.

Suitable example:-

class String
{
         char *str;
         public:
         string(char *s)
        {
                  int length = strlen(s);
                  str = new char[length+1];           // constructor
                  strcpy(str,s);
         }
         ~ String( ) {delete[ ] str;}                   //destructor
};

What is an inheritance? Write a program to describe the multilevel inheritance with suitable example.

Solution:-
Inheritance:-
        Inheritance is the process by which one object can acquire the properties of another object. It provides the concept of reusability.
Multi level inheritance:-
        A class can be derived from another derived class which is known as multilevel inheritance.
        Syntax:-
                   1st class name base class
                   2nd class name : 1st class name inheritance base class
                   3rd class name : 2nd class name derived class
Suitable Example:-
#include< iostream.h >
#include< conio.h >
class A
{
         public:
         int x ,y ;
};
class B : public A
{
         public:
         void getdata( )
         {
                    cout<< “ Enter the value of X : “;
                    cin>>x;
                    cout<< “ Enter the value of Y : “;
                    cin>>y;
         }
};
class C : public B
{
          int sum ;
          public:
          void getresult( )
          {   
                    getdata( );
                    sum=x+y ;
          }
          void display( )
          {
                   cout<< “sum x & b are: “<<sum<<endl ;
          }
};
void main( )
{
          C    NP , DK ;
          NP . getresult( );
          NP . display( );
          DK . getresult( );
          DK . display( );
          getch( );
}

What is a virtual function? Describe with example.

Solution:-
Virtual Function:-
i. Same function use in different class with different bahaviours.
ii. Member function access using pointer methods.

Syntax:- virtual keyword return type function name ( parameter )

Example:-
#include< iostream.h >
#include< conio.h >
Class A
{
        public:
        virtual void study( )
        {
                  cout<< " study in school " ;
        }

};
class B : public A
{
        public:
        void study ( )
        {
                  cout<< " study in college " ;
        }
};
void main ( )
{
        A *ptr;
        B np;
        ptr = & np;        

        ptr study ( );
        getch( );
}


Saturday, September 19, 2015

What are friend functions? Explain with example.

Solution:-
Friend Function:-

       A function is said to be a friend function of a class if it can access the members (including private members) of the class even if it is not the member function of this class.  A friend function is a non-member function that has access to the private members of the class.

Example:-

#include< iostream.h >
#include< conio.h >
class student
{
        int money ;
        public:
        void getmoney( )         {
                  cout<< " Enter the money: ";
                  cin>>money;
        }
        void display( )
        {
                  cout<<"money<<endl;
        }
        friend student share(student ) ;
} ;
student share(student x)
{
        studnt temp ;
        temp . money = x . money;
        return temp ;
}
void main( )
{
        clrscr( );
        student netra , durga ;
        netra.getmoney( );
        netra.display( );
        durga=share(netra) ;
        durga.display( );
        getch( );
}

What is template class? Example with example.

Solution:-
Template:-
       Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Example:-

#include<iostream.h>
#include<conio.h>
template < class T >
T add ( T x , T y )
{
       return x+y;
}
void main( )
{
       int c=5 , d=6 ;
       cout<< add (c,d)<<endl;
       float e=5.4 , f=3.3 ;
       cout<< add (e,f)<<endl;
       getch();
}

Video Tutorials:-


What is operator overloading? Write a program to describe the function overloading.

Solution:-
Operator Overloading:-
      Operator overloading is a polymorphism concept in Oops or C++. Special type of function that allocate additional task for object.
Syntax:- Return type operator symbol ( )
{
      Statement
}
Function overloading:-
      Function that share the same name are said to be overloaded functions and the process is referred to as function overloading. i.e. function overloading is the process of using the same name for two or more functions.

Program function overloading:-
#include< iostream.h >
#include< conio.h >
#include< iomanip.h >
int sum( int x , int y )
{
      int sum = x+y ;
      return sum ;
}
float sum ( float a , float b )
{
      float sum = a+b ;
      return sum ;

}
void main( )
{
      int x=5 , y=6 ;
      cout<<" sum: "<<Sum(x,y)<<endl ;
      float a=5.4 , b=3.3 ;
      cout<< " sum: "<<Sum(a,b)<<endl ;
      getch( );
}


Video Tutorials:-