Level:1 Novice

Hey all..
I still believe C++ has its own place in programming world.
This is a quick reference of C++ programming concepts with example codes presented in four levels from Novice to Expert. You can choose the level based on your exposure and need for a quick reference.
Sourcecodes presented here are accompanied with crispy comments which makes it easy while you walk through the code, hopefully…
Lets begin from Novice level
LEVEL : 1 | NOVICE
1. First C++ Hello World Program :
#include<iostream> //#include->preprocssor directive, iostream(headerFile)->cout,cin..
using namespace std; //std -> endl is defined here (library ftn
int main() //execution starts here
// {....} ->body of the ftn
{
cout<<"Hello world."<<endl; //endl->newline, defined in std namespace
return 0; //returns 0 to the O.S, whch called the main ftn & 0->successful
}
/*if using DevC++
#include<iostream>
#include<cstdlib> //to use system pause
using namespace std;
int main()
{
cout<<"Hello world."<<endl;
system("PAUSE"); //waits
return 0;
}
*/
2. C++ Constants, Variables, Data types, Keywords :
/*
constant->integers or values dat cant be changed
variable->name given to mem.location to access the value contained in it
datatypes->int,char,float,bool-TRUE or FALSE,void-spl.type(no value)
type modifiers->signed,unsigned,short,long,(used wid datatypes),influences mem. occupied
keywords->predefined meaning to the compiler
*/
3. Creating and Using C++ Variables :
#include<iostream>
using namespace std;
int main()
{
int age; // variable definition(whch type of data
/*VARIABLE age of TYPE integer
float average=32.36; //variable initialized
char sex='M';
bool isit=true; //true=1,false=0
short int discountprice=10; //typemodifier
age=26; //can be declared anywhere, after defining it
cout<<"age= "<<age<<endl<<"average= "<<average<<endl<<"sex= "<<sex<<endl<<"isit= "<<isit<<endl<<"Offer= "<<discountprice<<endl;
return 0;
}
4. C++ Console Output with Cout :
#include<iostream>
using namespace std;
int main()
{
int a=10;
cout<<"a= "<<a<<endl;//cout(console output)->object of ostream Class, << stream insertion operator
return 0;
}
5. Cin in C++ for Receiving User, Console Input :
#include<iostream>
using namespace std;
int main()
{
int age;
float average;
cout<<"Enter age and average"<<endl;
/*can read 2 or more values by single cin,irrespective of DATATYPE */
cin>>age>>average;//cin->obj of istream Class, >>stream EXTRACTION operator
cout<<"age= "<<age<<endl<<"avg= "<<average<<endl;
return 0;
}
6. C++ Comments :
/*block of codes can be commented this way, with a pair of slash and asterisk */
//single line can be commented this way, using doubleslash
7. C++ Arithmetic Operators :
#include<iostream>
using namespace std;
int main()
{
int num1=10,num2=20;
cout<<num1<<" + "<<num2<<" = " <<num1+num2<<endl;//add
cout<<num1<<" - "<<num2<<" = " <<num1-num2<<endl;//sub
cout<<num1<<" * "<<num2<<" = " <<num1*num2<<endl;//mulp
cout<<num1<<" / "<<num2<<" = " <<num1/num2<<endl;//div ( WARNING: /0(DIVIDE BY ZERO)->crashes the pgm
return 0;
}
8. C++ Increment and Decrement Operators :
#include<iostream>
using namespace std;
int main()
{
int a=10;
/* prefix ++a or --a */
//Increment
cout<<"PREFIX"<<endl;
cout<<"++a = "<<++a<<endl; //first incremented, then assigned
cout<<"a= "<<a<<endl<<endl;
//Decrement
cout<<"--a = "<<--a<<endl; //first decremented, then assigned
cout<<"a= "<<a<<endl<<endl<<endl;
/* postfix a++ or a-- */
//Increment
cout<<"POSTFIX"<<endl;
cout<<"a++ = " <<a++<<endl; //first assigned then incremented,incremented o/p comes when u encounter next statement
cout<<"a= "<<a<<endl<<endl;
//Decrement
cout<<"a-- = "<<a--<<endl; //first assigned then decremented,decremented o/p comes when u encounter next statement
cout<<"a= "<<a<<endl<<endl<<endl;
return 0;
}
9. C++ Modulus, Short-Hand Operators :
#include<iostream>
using namespace std;
int main()
{
//MODULUS % OPERATOR
int num1=20,num2=3;
cout<<num1%num2<<endl; //returns REMAINDER
/*SHORTHAND OPERATORS
+= -= *= /= %= */
num1+=num2; // same as num1=num1+num2
cout<<num1<<endl;
num1-=num2; // same as num1=num1-num2
cout<<num1<<endl;
num1*=num2; // same as num1=num1*num2
cout<<num1<<endl;
num1/=num2; // same as num1=num1/num2
cout<<num1<<endl;
num1%=num2; // same as num1=num1%num2
cout<<num1<<endl;
return 0;
}
10. IF ELSE _ Conditional Statement :
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter ur age:";
cin>>age;
/* comparison operators:
< > == <= >= != NOTE: =is ASSIGNMENT operator, == is COMPARISON operator
Logical operator: ! */
if(age<=17){
cout<<"NO..u cant vote"<<endl; // executed if d condition is true
}else{ //if else
cout<<"Yes..u can vote"<<endl;
}
return 0;
}
11. C++ Nested IF ELSE and IF ELSEIF :
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter ur age:";
cin>>age;
if(age<100){
if(age<=20){ //nested if
cout<<"u r young"<<endl;
}else if(age<=45){ //if else if
cout<<"u r mid-age"<<endl;
}else{
cout<<"u r old"<<endl;
}
}
return 0;
}
12. C++ Logical and Comparison Operators :
#include<iostream>
using namespace std;
/* COMPARISON OP:
< > <= >= == !=
LOGICAL OP: && || ! */
int main()
{
int date;
cout<<"Enter a date: ";
cin>>date;
< > comparison op
/*
if(date>0){
if(date<32){
cout<<"Date is valid"<<endl;
}
}
*/
<= >= comparison op
/*
if(date>=1){
if(date<=31){
cout<<"Date is valid"<<endl;
}
}
*/
== != comparison op
/*
if(date==1){
cout<<"Got salary"<<endl;
}
if(date!=1){
cout<<"waiting fr salary"<<endl;
}
*/
&& || -logical operators
/*
if(date>=1 && date <=31){
cout<<"date is valid"<<endl;
if(date==1 || date==31){
cout<<"todays spl"<<endl;
}
}else {
cout<<"date is invalid"<<endl;
}
*/
! logical op (operates on BOOLEAN
/*
bool human=true;
if(!human){ //negates
cout<<"...ALIEN..."<<endl;
}cout<<"human"<<endl;
*/
return 0;
}
13. C++ Ternary Operator (Conditional Operator) :
#include<iostream>
using namespace std;
/*ternary or conditional op
shorthand to if else */
int main()
{
int mark;
cout<<"Enter ur mark: ";
cin>>mark;
/* SYNTAX expr ? stmt1 : stmt2 ;
expr-> any logical,comparison
stmt->any valid C++ statement
if expr TRUE stmt1 executed
else stmt2 executed*/
mark>=35 ? cout<<"PASS"<<endl : cout<<"FAIL"<<endl;
return 0;
}
14. While Loop _ Introduction to Looping :
#include<iostream>
using namespace std;
int main()
{
int counter=1;
while(counter<=30){ //while(expr){stmnt..}
cout<<counter<<"-> time Loop running"<<endl;
counter++; //at some point, expr must evaluate FALSE
//WARNING: OR ELSE U'LL STUCK INSIDE INFINTE LOOP
}
return 0;
}
15. Do While Loop with Example :
#include<iostream>
using namespace std;
int main()
{
char input;
do{
cout<<"menu"<<endl;
cout<<"Enter x to exit or any other key to see menu again: ";
cin>>input;
}while(input != 'x');
return 0;
}
16. For Loop with Example :
#include<iostream>
using namespace std;
int main()
{
int counter;
//syntax: for(init;cond;updation)
/*NOTE: for(;;) infinite loop
(loop without updation leads to inf loop*/
for(counter=1;counter<=30;counter++)
cout<<counter<<"-> for loop running"<<endl;
return 0;
}
17. Introduction to ARRAYS :
#include<iostream>
using namespace std;
int main()
{
//int marks[5]={65,75,85,95,85};//can be initialized diff ways
//int marks[]={65,75,85,95,85};//array size =no of elements
int marks[5];
marks[0]=65;
marks[2]=85;
cout<<marks[2]<<endl;
return 0;
}
18.Two Dimensional and Multidimensional Arrays :
#include<iostream>
using namespace std;
int main()
{
/*int marks[2][6]; //can be initialized like this
marks[0][2]=10;*/
int marks[2][6]={
{11,22,33,44,55,66},
{1,2,3,4,5,6}
};
cout<<marks[0][2]<<endl;
return 0;
}
19. Introduction to Functions _ Subroutines :
#include<iostream>
using namespace std;
void display();//ftn prototype syntax: return_type ftn_name(par_data_type,…)
int main() //return_type ftn_name(parameter)
{
display();
display(); //can be called n times
return 0;
}
void display()
{
cout<<"ftn called"<<endl;
}
20. Function Parameters _ Returning Values from Functions :
/*PASS BY VALUE*/
#include<iostream>
using namespace std;
/*
//FUNCTION PARAMETER
void display(int,int); //ftn prototype with ftn parameters
int main()
{
display(20,30); //ftn call
return 0;
}
void display(int a,int b)
{ cout<<a<<endl;
cout<<b<<endl;
cout<<a+b<<endl;
}
*/
//RETURNING VALUES FROM FTN
int display(int, int); //ftn prototype with ftn parameters, returning value from ftn
int main()
{
int result;
result=display(20,30);//returned value of the ftn is stored in variable result
cout<<result<<endl;
return 0;
}
int display(int x, int y)
{
cout<<x<<endl;
cout<<y<<endl;
return x+y; //return value x+y
}
21. C++ Default Function Parameters :
#include<iostream>
using namespace std;
void display(int x,int y ,int z=10){ //start initializing frm RIGHTMOST variable
//void display(int x=10,int y,int z){ //generates ERROR,since u initialized the LEFTMOST var
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
}
int main()
{
display(30,20);//calling ftn with only 2 parameters
//display(30,20,40) // can be used,overwrites the default ftn parameter
return 0;
}
22. C++ Inline Function _ Inline Keyword :
#include<iostream>
using namespace std;
inline void display(int x){ //use ‘inline’ for SHORT FTNs
//WARNING: don’t use ‘inline’ for large ftns
cout<<x<<endl;
}
int main()
{
display(10);//(compiler)reduces overhead by replacing the ftn call by ftn body
return 0;
}
23. C++ Scope _ Local, Global Variable Scopes :
#include<iostream>
using namespace std;
/*scope-wer it is accessible
local scope,global scope,block scope
inside ftn,ftn definition->local scope
outside ftn->global scope
inside a block->block scope
*/
void display();
int x=100;//global ,can be called anywer
int main()
{
display();
cout<<"GLOBAL x from main: x= "<<x<<endl;
{
int y=20;//block scope, can be called only inside this CODE BLOCK
cout<<"BLOCK SCOPE: y= "<<y<<endl;
}
return 0;
}
void display(){
int a=10,b=20;//local scope,can be called only inside this ftn
cout<<"LOCAL a and b: "<<a<<" " <<b<<endl;
cout<<"GLOBAL x from display ftn: x= "<<x<<endl;
}
24. C++ Break Statement with Example :
#include<iostream>
using namespace std;
/*break-used to terminate a loop immediately
& control'll be passed to the next statement
also used in switch , to terminate a case
NOTE:if used in nested loop,
inner loop'd be terminated & control passed 2 d outer loop &continued normally*/
int main()
{
int counter;
for(counter=0;counter<5;counter++)//outer loop
{
int innerctr;
for(innerctr=1;innerctr<=3;innerctr++)//inner loop
{cout<<innerctr<<endl;
if(innerctr==2)break;}//control passed 2 outer loop
}
return 0;
}
25. C++ Continue Statement with Example :
#include<iostream>
using namespace std;
/*continue->skips current iteration,continue with next stmnt
if used in 'for loop'->skips ct condition, continues with conditional chk & updation
if used in 'do while'->skips ct condition, continues with conditional chk*/
/*int main()
{
int counter;
for(counter=1;counter<=10;counter++){
if(counter==5){
continue;//skips when counter=5,control goes to-> counter++ ->counter<=10
}
cout<<counter<<endl;
}
return 0;
}*/
int main()
{
int counter=1;
while(counter<=10){
if(counter==5){
counter++;//this avoids getting stuck inside d 'if loop'
continue;}/*skips printing counter value 5, control goes 2 conditional chk*/
cout<<counter<<endl;
counter++;
}
return 0;
}
So,..we have reached the end of this level. In our next level we will dive deeper..Please do read the complete series of this blog to reach the Level from Novice to Expert.
If you find any error in SourceCode please let me know in the Comments. Corrections and Improvement Suggestions are highly appreciated. Thanks in advance..
love,
<R.S>
P.S: Please ignore typo in code’s comments. Comments provided only for understanding purpose.
For Complete Sourcecode visit my Github Repo: Link provided in the final module
RS-codes

One thought on “CPP QuickReference – From Novice to Expert-Level1”