Posts

Showing posts from June 13, 2013

C++ Function: Overloading Example

#include <iostream>   using namespace std ;   /* Number of arguments are different */   void display ( char [ ] ) ; // print the string passed as argument void display ( char [ ] , char [ ] ) ;   int main ( ) { char first [ ] = "C programming" ; char second [ ] = "C++ programming" ;   display ( first ) ; display ( first, second ) ;   return 0 ; }   void display ( char s [ ] ) { cout << s << endl ; }   void display ( char s [ ] , char t [ ] ) { cout << s << endl << t << endl ; }

C++ Function: Print 45 Asterisks for Design the output

using namespace std; void starline( ); into main () { starline ( ); cout << "Data type Range" << endl; starline ( ); cout << "char -128 to 127 " << endl << "short -32 ,768 to 32,767"<< endl << "int system independent: << endl << " long q-2,147,483,648 to 2,147,483,647" << endl; starline ( ); return 0; } //"""""""""""""""""""""""".. //starline ( ) // function defintion void starline ( ) { for(into j=0;j<45; j++) cout << "*" ; cout << endl; }      The output from the program looks like this ************************************* Data type Range ************************************* Char -128 to 127 Short -32,768 to 32,767 Into system dependent Double -2,147,483,648 to 2,147,483,647 *******

C++ Function: Adding Two Numbers with default value parameter

#include <iostream> using namespace std ; int sum ( int a , int b = 20 ) { int result ; result = a + b ; return ( result ); } int main () { // local variable declaration: int a = 100 ; int b = 200 ; int result ; // calling a function to add the values. result = sum ( a , b ); cout << "Total value is :" << result << endl ; // calling a function again as follows. result = sum ( a ); cout << "Total value is :" << result << endl ; return 0 ; }

C++ Function : Find Maximum value between two natural numbers

#include <iostream> using namespace std ; // function declaration int max ( int num1 , int num2 ); int main () { // local variable declaration: int a = 100 ; int b = 200 ; int ret ; // calling a function to get max value. ret = max ( a , b ); cout << "Max value is : " << ret << endl ; return 0 ; } // function returning the max between two numbers int max ( int num1 , int num2 ) { // local variable declaration int result ; if ( num1 > num2 ) result = num1 ; else result = num2 ; return result ; }