Basics of C++
Educator: Twinkle S. Panchal
Assistant Professor
Sutex Bank College of Computer
Applications and science
Basic C++
C ++ is an object oriented programming language.
C ++ was developed by BJarne Stroustrup at AT & T Bell lab, USA in early
1980s.
C ++ was developed from C. “++” means its incremented version of C
C++ was early called “C with classes”.
C++ is superset of C.
C++ Comments
C++ introduces a new comment symbol //(double slash).
1) Single Line Comment
// this is an example of single line comment
1) Multi Line Comment
/* this is an example of c++ program */
Output Operator
The Identifier cout can be used to display individual characters, strings and
even numbers.
The cout object’s properties are defined in iostream.h represents the
stream.
The insertion Operator << also called the “put to” operator directs the
information on its right to the object on its left.
Example: cout<<”Hello World”
Return Statement
In C++ main ( ) returns an integer type value to the operating system.
Therefore every main ( ) in C++ should end with a return (0) statement,
otherwise a warning or an error might occur.
Input Operator
The identifier cin is a predefined object in C++ that corresponds to the
standard input stream.
The operator >> is known as “Extraction” or “get from” operator.
It extracts value from the keyboard and assigns it to the variable on its
right.
Example: cin>>number1
cout<<”sum”<<sum<<”\n”; \\ showing output
cout<<”sum=”<<sum<<”\n”<<”average”<<”\n”;
cin>>number1>>number2; \\ taking input
Structure of Program
// my first program in C++
#include <iostream.h> // pre-processor directive - its input output stream
int main ()
cout << "Hello World!";
return 0; } //return statement
Output:-Hello World!
FUNCTIONS IN C++
INLINE FUNCTION
C++ inline function is powerful concept that is commonly used with classes.
If a function is inline, the compiler places a copy of the code of that
function at each point where the function is called at compile time.
To inline a function, place the keyword inline before the function name and
define the function before any calls are made to the function.
The compiler can ignore the inline keyword in case defined function is
more than a line.
The inline function syntax as follows:-
inline function-header (argument list)
function body;
Example:
inline double cube (double a)
{ return(a*a*a); }
The above inline function can be invoked by statements like
c=cube(3.0); d=cube(2.5);
Example for Inline Function
inline int Max(int x, int y) {
return (x > y)? x : y;
int main() { // Main function for the program
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0; }
Advantages of Inline Function
1. It speeds up your program by avoiding function calling.
2. It save time to return call from a function.
Disadvantages of Inline Function
1. It increases the executable size due to code expansion.
2. C++ inlining is resolved at compile time. Which means if you change
the code of the inlined function, you would need to recompile all the
code using it to make sure it will be updated
Compiler may not perform inlining in such circumstances like:
1) If a function contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return statement
doesn’t exist in function body.
5) If a function contains switch or goto statement.
FUNCTIONS WITH DEFAULT ARGUMENT
Default values are specified when the function is declared.
The compiler looks at the prototype to see how many arguments a function
uses and alerts the program for possible default values.
Example: float amount (float principle, int year,float rate=0.15);
Call: cout<<”Amount ”<<amount(1000,4);
FUNCTION OVERLOADING:
Overloading refers to the use of the same name multiple forms.
This means that we can use the same function name to creates functions
that perform a variety of different tasks.
(This is known as function polymorphism in oops.)
Using the concepts of function overloading , a family of functions with one
function name but with different argument lists in the functions call .
The correct function to be invoked is determined by checking the number
and type of the arguments but not on the function type.
For example an overloaded add() function handles different types of data
as shown below.
//Declaration int add(int a, int b); //2
//prototype 1 int add (int a, int b, int c); //3
//prototype 2 double add(double x, double y); // data type different
//prototype 3 double add(double p , double q); // invalid
//function call
cout<<add(5,10); //uses prototype 1
cout<<add(15,10.0); //uses prototype 4
cout<<add(12.5,7.5); //uses prototype 3
cout<<add(5,10,15); //uses prototype 2
#include<iostream.h>
main( ) {
cout<<volume(10)<<endl;
cout<<volume(10)<<endl;
cout<<volume(10)<<endl; }
int volume( int s) { return (s*s*s); //cube }
double volume( double r, int h) { return(3.1416*r*r*h); //cylinder
} }
output:- 1000 157.2595 112500
Thank you..