Learn C++ 11-20/56

Learn C++

  1. C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for creating large-scale applications. C++is a superset of the C language. A related programming language, Java, is based on C++ but optimized for the distribution of program objects in a network such as the Internet.

  • Binary Search
  • Complex Number
  • Constructor and Destructor
  • Convert Temperatures from Celsius to Fahrenheit and vice versa.
  • Copy One File to another File
  • Data Abstraction
  • Data Encapsulation
  • Enum example
  • Euclid's algorithm
  • Exception Handling


Name Code

Binary Search

       #include < iostream.h >
   template < class T >
   int binarySearch(T a[], int n, T & x)
   {
     int left = 0; // left end of segment
     int right = n - 1; // right end of segment
     while (left <= right)
     {
       int middle = (left + right)/2; // middle of segment
       if (x == a[middle])
         return middle;
       if (x > a[middle])
         left = middle + 1;
       else
         right = middle - 1;
     }
     return -1; // x not found
   }
   int main()
   {
     int a[10],n,t;
     cout<<"Enter the size:";
     cin>>n;
     cout<<"enter the elements in sorted order:";
     for(int i=0;i< n;i++)
       cin >>a[i];
     cout<<"enter the element to search:";
     cin>>t;
     int f=binarySearch(a,n,t);
     if(f==-1)
       cout<<"element not found";
     else
       cout<<"element found at index:"<   }   

Complex Number

      #include<iostream.h>
   #include<conio.h>
   class complex
   {
     private:
       float real,img;
     public:
       void assign(float x,float y)
     {
       real=x;
       img=y;
     }
     void print()
     {
     if(img>=0)
       cout<< real<<"+"<< img<<"i";
     else
       cout<< real<< img<<"i";
     getch();
     }
   };
   void add( float a,float b,float c, float d)
   {
     float e,f;complex g;
     e=a+c;
     f=b+d;
     g.assign(e,f);
     g.print();
   }
   void sub( float a,float b,float c, float d)
   {
     float e,f;complex g;
     e=a-c;
     f=b-d;
     g.assign(e,f);
     g.print();
   }
   void mul( float a,float b,float c, float d)
   {
     float e,f; complex g;
     e=a*c-b*d;
     f=b*c+a*d;
     g.assign(e,f);
     g.print();
   }
   void main()
   {
     float a,b,c,d;
     complex x,y,z;
     clrscr();
     cout<<" for complex 1:";
     cout<<"real part:";
     cin>>a;
     cout<<"imaginary part:";
     cin>>b;
     cout<<" for complex 2:";
     cout<<"real part:";
     cin>>c;
     cout<<"imaginary part:";
     cin>>d;
     x.assign(a,b);
     y.assign(c,d);
     cout<<"***original data:***\n";
     cout<<"Complex 1:\n";x.print();
     cout<<"\n Complex 2:\n";y.print();
     cout<<"\n***\n";
     cout<<"\n Addition:\n";add(a,b,c,d);
     cout<<"\n Subtraction:\n";sub(a,b,c,d);
     cout<<"\n Multipication:\n";mul(a,b,c,d);
   } 

Constructor and Destructor

      #include <iostream>
   using namespace std;
   #define SIZE 10
   class stack
   {
     int stck[SIZE];
     int topOfStack;
     public:
       stack(); // constructor
       ~stack(); // destructor
       void push(int i);
       int pop();
   };
   // constructor
   stack::stack()
   {
     topOfStack = 0;
     cout << "Stack Initialized\n";
   }
   // destructor
   stack::~stack()
   {
     cout << "Stack Destroyed\n";
   }
   void stack::push(int i)
   {
     if( topOfStack == SIZE )
     {
       cout << "Stack is full.\n";
       return;
     }
     stck[ topOfStack ] = i;
     topOfStack++;
   }
   int stack::pop()
   {
     if( topOfStack == 0 )
     {
       cout << "Stack underflow.\n";
       return 0;
     }
     topOfStack--;
     return stck[ topOfStack ];
   }
   int main()
   {
     stack a, b;
     a.push(1);
     b.push(2);
     a.push(3);
     b.push(4);
     cout << a.pop() << " ";
     cout << a.pop() << " ";
     cout << b.pop() << " ";
     cout << b.pop() << endl;
     return 0;
   } 

Convert Temperatures from Celsius to Fahrenheit and vice versa.

      #include <iostream.h>
   #include <conio.h>
   void main()
   {
     clrscr();
     int choice;
     float ctemp,ftemp;
     cout << "1.Celsius to Fahrenheit" << endl;
     cout << "2.Fahrenheit to Celsius" << endl;
     cout << "Choose between 1 & 2 : " << endl;
     cin>>choice;
     if (choice==1)
     {
       cout << "Enter the temperature in Celsius : " << endl;
       cin>>ctemp;
       ftemp=(1.8*ctemp)+32;
       cout << "Temperature in Fahrenheit = " << ftemp << endl;
     }
     else
     {
       cout << "Enter the temperature in Fahrenheit : " << endl;
       cin>>ftemp;
       ctemp=(ftemp-32)/1.8;
       cout << "Temperature in Celsius = " << ctemp << endl;
     }
     getch();
   } 

Copy One File to another File

          #include < iostream.h >
   #include < conio.h >
   #include < iomanip.h >
   #include < stdlib.h >
   #include < ctype.h >
   #include < fstream.h >
   void main( )
   {
     ofstream outfile;
     ifstream infile;
     char fname1[10],fname2[20];
     char ch,uch;
     clrscr( );
     cout<<"Enter a file name to be copied ";
     cin>> fname1;
     cout<<"Enter new file name";
     cin>>fname2;
     infile.open(fname1);
     if( infile.fail( ) )
     {
       cerr<< " No such a file Exit";
       getch();
       exit(1);
     }
     outfile.open( fname2);
       if(outfile.fail( ))
       {
         cerr<<"Unable to create a file";
         getch();
         exit(1);
       }
       while( !infile.eof( ) )
       {
         ch = (char) infile.get( );
         uch = toupper(ch);
         outfile.put(uch);
         }
     infile.close( );
     outfile.close( );
     getch( );
   } 

Data Abstraction

      #include <iostream>
   using namespace std;
   class Adder
   {
     public:
       // constructor
       Adder(int i = 0)
       {
         total = i;
       }
       // interface to outside world
       void addNum(int number)
       {
         total += number;
       }
       // interface to outside world
       int getTotal()
       {
         return total;
       };
       private:
         // hidden data from outside world
         int total;
   };
   int main( )
   {
     Adder a;
     a.addNum(10);
     a.addNum(20);
     a.addNum(30);
     cout << "Total " << a.getTotal() <<endl;
     return 0;
   } 

Data Encapsulation

      #include <iostream>
   using namespace std;
   class Adder
   {
     public:
       // constructor
       Adder(int i = 0)
       {
         total = i;
       }
       // interface to outside world
       void addNum(int number)
       {
         total += number;
       }
       // interface to outside world
       int getTotal()
       {
         return total;
       };
     private:
       // hidden data from outside world
       int total;
   };
   int main( )
   {
     Adder a;
     a.addNum(10);
     a.addNum(20);
     a.addNum(30);
     cout << "Total " << a.getTotal() <<endl;
     return 0;
   } 

Enum example

      #include <iostream.h>
   int main()
   {
     enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
     Days TheDay;
     int j;
     cout<<"Please enter the day of the week (0 to 6)";
     cin>>j;
     TheDay = Days(j);
     if(TheDay == Sunday || TheDay == Saturday)
       cout<<"Hurray it is the weekend"<     else
       cout<<"Curses still at work"<     return 0;
   } 

Euclid's algorithm

      #include <iostream.h>
   // Fundamental idea of Euclid's algorithm (one of the oldest known algorithms)
   // gcd(a,0) = a
   // gcd(a,b) = gcd(b,a%b)
   int gcd (int a, int b)
   {
     int temp;
     while (b != 0)
     {
       temp = a % b;
       a = b;
       b = temp;
     }
     return(a);
   }
   int main ()
   {
     int x, y;
     cout << "Enter two natural numbers: ";
     cin >> x >> y;
     cout << "gcd(" << x << ", " << y << ") = " << gcd(x,y) << endl;
     return(0);
   } 

Exception Handling

      #include <iostream>
   using namespace std;
   int main()
   {
     unsigned int TypeOfLoan = 0;
     const char *LoanType[] =
     { "Personal",
       "Car",
       "Furniture",
       "Musical Instrument",
       "Boat"
     };
     try
     {
       cout << "Enter the type of loan\n";
       for(int i = 0; i < 4; i++)
         cout << i + 1 << ") " << LoanType[i] << endl;
       cout << "Your choice: ";
       cin >> TypeOfLoan;
       if( TypeOfLoan < 1 || TypeOfLoan > 5 )
         throw "Number out of range\n";
       cout << "\nType of Loan: " << LoanType[TypeOfLoan] << endl;
     }
     catch(const char* Msg)
     {
       cout << "Error: " << Msg << endl;
     }
     catch(...)
     {
       cout << "\nSomething went wrong\n\n";
     }
     return 0;
   } 
Learn C++ 11-20/56 Learn C++ 11-20/56 Reviewed by Abdul hanan on 05:06:00 Rating: 5
Powered by Blogger.