In this article, we will discuss about certain features of C++ Programming Language, which we haven’t discussed so far. All the features are discussed with the help of example programs so that you could straightaway learn to use them.
Let us have a look at them one-by-one: (Things are discussed in the comments wherever necessary)
EXAMPLE 1: Variation of for-loop
// Example Program in C++ #include<stdio.h> #include<conio.h> void main(void) { char ch; for(;;) { // this loop will run // infinitely till // 'q' is pressed ch=getch(); if (ch=='q' ch=='Q') break; } printf("out of loop\n"); }
EXAMPLE 2: Variation of for-loop
// Example Program in C++ #include<iostream.h> void main(void) { char str[20]="learn C++"; for(int i=0;str[i];i++) ;// notice that there // is nothing in the // body of the loop cout<<"length of str :"<<i; cout<<endl; }
OUTPUT:
length of str :9
Press any key to continue
EXAMPLE 3: Variation of for-loop
// Example Program in C++ #include<iostream.h> void main(void) { for(int a=0,b=0; a+b<10; a++) { // here the number of iteration // also depends on the value of // 'b' which is entered by the // user cout<<"enter value for b: "; cin>>b; cout<<a<<endl; } }
EXAMPLE 4: Function having default arguments
// Example Program in C++ #include<iostream.h> // function prototype // it is necessary to define the // parameter list when default // argument is used float power(float num, int pow=2); void main(void) { float number=10; // here power is called with // the second argument having // the default value 2 cout<<power(number); cout<<endl; // here the function is called // as usual with two arguments cout<<power(number, 4); cout<<endl; } // function definition // DO NOT write 'int pow=2' here float power(float num, int pow) { float num2=num; for(int i=1; i<pow; i++) num=num*num2; return num; }
OUTPUT:
100
10000
Press any key to continue
Good-Bye for now!
Please check back for updates!