26.8.15

Create a class called 'EMPLOYEE'

Create a class called 'EMPLOYEE' that has

-     EMPCODE and EMPNAME as data members

-     member function getdata( ) to input data

-     member function display( ) to output data

Write a main function to create EMP, an array of EMPLOYEE objects. Accept and display the details of at least 6 employees.


#include<iostream.h>

#include<conio.h>
#include<iomanip.h> class Employee

{
private:  int empcode;
char empname[10]; public: void getdata();

void display();
};
void Employee::getdata()
{
cout<<"\nNAME :"; cin>>empname; cout<<"\nCODE :"; cin>>empcode;

}
void Employee::display()
{
cout<<endl<<setw(20)<<empname<<setw(10)<<empcode;
}

int main()

{
Employee Emp[6]; clrscr();
cout<< "Enter employee details:\n "; for(int i=0;i<6;i++)
{

cout<<"\nemployee "<<i+1<<endl; Emp[i].getdata();
}

cout<<"\nEmployee details are as follows :"; cout<<"\n\n"<<setw(20)<<"NAME"<<setw(10)<<setiosflags(ios::right)<<"CODE";

cout<<"\n------------------------------
";
for(i=0;i<6;i++)

Emp[i].display();

getch();

return(0);

}

*************************OUTPUT*********************************

Enter employee details:

employee 1


NAME :ashok


CODE :111


employee 2


NAME :annapurna


CODE :112


employee 3


NAME :anupama


CODE :113


employee 4


NAME :anuradha


CODE :114


employee 5


NAME :ashraya


CODE :115


employee 6


NAME :akash


CODE :116


Employee details are as follows :

NAME
CODE

----------------------------------------------

Ashok
111

annapurna
112

anupama
113

anuradha
114

ashraya
115

akash
116


Create a 'DISTANCE' class

Create a 'DISTANCE' class with :

-     feet and inches as data members

-     member function to input distance

-     member function to output distance

-     member function to add two distance objects

Write a main function to create objects of DISTANCE class. Input two distances and output the sum.

#include<iostream.h>

#include<conio.h>

class distance

{
int feet,inch;
public:
void getdistance(); void putdistance();

void adddistance(distance d1,distance d2);
};

void distance::getdistance()

{

cout<<"\nEnter the feet : "; cin>>feet;
cout<<"\nEnter the inches :"; cin>>inch;
}

void distance::putdistance()
{
cout<<"\n\nfeet = "<<feet; cout<<"\tinch = "<< inch;

}
void distance::adddistance(distance d1,distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
inch=inch%12;
feet=feet+d1.feet+d2.feet;
}

int main()

{

distance d1,d2,d3; clrscr();

cout<< "\nDistance 1 \n"; d1.getdistance();

cout<< "\nDistance 2 \n"; d2.getdistance(); d3.adddistance(d1,d2);
cout<<"\nThe Sum Of two distance is : "; d3.putdistance();
getch();

return(0);
}


*********************************OUTPUT****************************

Distance 1

Enter the feet : 13

Enter the inches :11

Distance 2

Enter the feet : 11

Enter the inches :11

The Sum Of two distance is :

feet = 25   inch = 10

Implement function overloading

Write a C++ program to implement function overloading in order to compute power(m,n) where i) m is double and n is int ii) m and n are int.

#include<iostream.h>

#include<conio.h>
#include<math.h>

int power(int a,int b);

double power(int i, double d);

int main()

{

int i1,i2,i3; double d1; clrscr();
cout<<"\nEnter Two Integer Numbers To Calculate Power : "; cin >>i1>>i2;
int p1=power(i1,i2);

cout<<"\nThe Power of "<<i1<<" and "<<i2<<" is : "<<p1; cout<<"\n\nEnter One Integer And One Double To calculate power :"; cin>>i3>>d1;

double p2=power(i3,d1);

cout<<"\nThe Power of "<<i3<<" and "<<d1<<" is : "<<p2; getch();
return(0);

}

int power(int a,int b)

{
return(pow(a,b));
}
double power(int i, double d)
{
return(pow(i,d));
}


********************************OUTPUT**********************************

Enter Two Integer Numbers To Calculate Power : 2  4

The Power of 2 and 4 is : 16

Enter One Integer And One Double To calculate power :2 2.5


The Power of 2 and 2.5 is : 5.656854

Sort an array of integer in ascending order

Write a C++ program to sort an array of integer in ascending order using a function called exchange( ) which accepts two integer arguments by reference


#include<iostream.h>

#include<conio.h>

void exchange(int (&a)[],int &n);

int main()

{

int a[10],size; clrscr();

cout<<"Enter the Array size : "; cin>>size;
cout<<"Enter the Array elements :\n"; for(int i=0;i<size;i++)
cin>>a[i];

exchange(a,size); cout<<"After sorting :\n"; for(i=0;i<size;i++) cout<<a[i]<<endl; getch();

return 0;
}

void exchange(int (&a)[],int &n)

{
for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(a[i]<a[j])
{

int temp=a[i]; a[i]=a[j]; a[j]=temp;
}


}



**********************************OUTPUT********************************

Enter the Array size : 10 Enter the Array elements : 15 46 89 62 -5 -78 0 5 45 9











After sorting : -78 -5 0 5 9 15 45 46 62 89

Write a C++ program to find the largest of three numbers using inline function

Write a C++ program to find the largest of three numbers using inline function

#include<iostream.h>

#include<conio.h>

inline int largest(int &a,int &b,int &c)

{
int big=0; if(a>b)

big=a;
else
big=b;
if(c>big)
big=c; return big;

}

int main()

{
int a,b,c; clrscr();

cout<<"Enter Three Numbers To Find The Largest "<<endl; cout<<"a = ";
cin>>a; cout<<"\nb = "; cin>>b; cout<<"\nc = "; cin>>c;
int large=largest(a,b,c);
cout<<"\n Largest of "<<a<<","<<b<<" and "<<c<<" is "<<large; getch();
return(0);
}

********************************OUTOUT**********************************

Enter Three Numbers To Find The Largest a = 24

b = 45 c = 23

Largest of 24,45 and 23 is 45

10.12.14

Recursion in C++

Create a recursive c++ function to fill up an array and make sum of all digit. you have to create different recursive function for fill up and to make sum.


#include<iostream>
using namespace std;

void array_recursive(int array[],int n)
{
     if (n-1>=0)
          {
               cout<<"Enter elements into the array:\n";
               cin>>array[n];
               n--;
               array_recursive(array,n);
           }

}

int sum_array(int array[],int n)
{
            if (n= =0)
                 {
                       return array[0] ;
                  }
            return array[n]+sum_array(array,n-1);
}

int main( )
{
         int n;
         cout<<"Enter the size of an array:\n";
         cin>>n;
         int array[n];
         array_recursive(array,n);
         cout<<"Printing Recursive Data\n";
         for(int i=n; i>0; i--)
             {
                  cout<<array[i]<<"\n";
             }
         cout<<"the sum is: "<<sum_array(array+1,n-1)<<endl;
}

Inheritence in c++ to generate a fibonacci series

Generate a Fibonacci series between 0 and 1000. The first two initial value are 0 and 1


#include<iostream>
using namespace std;
class fibonacci
{
     public:
     void set_digit (int m, int n)
         {
              f1=m;
              f2=n;
          }
      protected:
           int f1;
           int f2;
           int f3;
};

class print : public fibonacci
{
     public:
          void show_print()
              {
                   int maxnum;
                   cout <<"Enter the maximum number of fibonacci series: "<< "\n";
                   cout <<"\nPrinting fibonacci series from 0 to 1000: " << "\n");
                   while (f1+f2 < = 1000)
                   {
                        f3=f1+f2;
                        cout <<f3 <<"\n";
                        f1=f2;
                        f2=f3;
                   }
              }
};
int main()
{
      print p;
      p.set_digit (0, 1);
      p.show_print( );
}

Constructor in c++ to Sum of all multipliers by given range

write a c++ program which contains a constructor to make sum of all multipliers of 7 or 9 between 1 and 100.


#include<iostream>
using namespace std;
class divis
{
      public:
            int sum=0;
            divis();
            ~divis();
            void result();
};

divis :: divis()
{
    int a=1;
    while(a<100)
    {
        if ( (a%7==0) || (a%9==0) )
            {
                cout << a <<"\n";
                sum = sum+a;
            }
        a++;
    }
}

divis :: ~divis()
{
    cout <<"memory released" << "\n";
}

void divis :: result()
{
    cout << "\n The sum of all divisibles: "<< sum << endl;
}

int main( )
{
     divis vs;
     vs.result();
}

27.11.14

Inheritence in C++ to check a Perfect Number (Excercise-19)

Write a Object Oriented C++ program using Inheritance to check whether a given number is perfect or not ?


#include<iostream>
using namespace std;
class perfect
{
     public:
          void setnum (int digit)
              {
                    number = digit;
               }
               protected: 
               int number;
};

class chknumber : public perfect
{
      public:
          int sum=0;
          void getperfect ( )
                 {
                       for ( int i=1; i<number; i++ )
                           {
                                if ( number%i = =0 )
                                     {
                                         sum += i;
                                     }
                            }
                   }
         int chksum( )
                {
                     if  (number = = sum)
                          cout << "Yes!!" << endl<< "the given number is perfect number" << endl << endl;
                     else
                          cout << "Not!!"<< endl << "the given number is not a perfect number" << endl;
                }
};

int main( )
{
     chknumber per;
     int digit;
     cout << "Enter a number to check perfect or not : ";
     cin >> digit;
     per.setnum(digit);
     per.getperfect();
     per.chksum();
     return 0;
}

C++ Definition : object oriented programming

Write answer for the following questions in comments. Give answer in short. Example as source code is preferable with each answer.


How is OOP important in C++?

What is abstract class?

What is this Pointer ? What is its significance?

What is the difference between while and do-while loop?

Why main() function is important in C++?

What is type casting in C++?


What is OOP?

What is the type conversion in C++

What is the difference between local variable and global variable.

Show Inheritance with at least two derived class

Show Data Abstraction and data Encapsulation

Describe Polymorphism with Example

 

Excercise-18 : Recursive Function Progamming



Write a program using recursive function in C++ to find factorial of a given integer number.