Lecture-3-Basics of C++
Lecture-3-Basics of C++
Lecture-3-Basics of C++
Basics of C++
Basics of C++
The c comment symbols /* ….*/ are still valid and more suitable for
multi line comments.
/* this is an example of c++ program */
Output Operator
•The statement cout <<”Hello, world” displayed the string with in quotes on
the screen.
•The identifier cout can be used to display individual characters, strings and
even numbers.
•It is a predefined object that corresponds to the standard output stream.
•Stream just refers to a flow of data and the standard Output stream normally
flows to the screen display.
•The cout object, whose properties are defined in iostream.h represents that
stream.
•The insertion operator << also called the ‘put to’ operator directs the
information on its right to the object on its left.
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 statement cin>> number 1; is an input statement and causes the
program to wait for the user to type in a number.
•The number keyed in is placed in the variable number1.
•The identifier cin is a predefined object in C++ that corresponds to the
standard input stream.
•Here this stream represents the key board.
•The operator >> is known as get from operator.
•It extracts value from the keyboard and assigns it to the variable on its
right.
Cascading Of I/O Operator
• cout<<”sum=”<<sum<<”\n”;
• cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;
• cin>>number1>>number2;
Structure Of A Program
•Lines beginning with a hash sign (#) are directives for the preprocessor.
•They are not regular code lines with expressions but indications for the
compiler's preprocessor.
•In this case the directive #include<iostream> tells the preprocessor to
include the iostream standard file.
•This specific file (iostream) includes the declarations of the basic
standard input-output library in C++, and it is included because its
functionality is going to be used later in the program.
using namespace std
•All the elements of the standard C++ library are declared within what is
called a namespace, the
•namespace with the name std.
•So in order to access its functionality we declare with this expression
that we will be using these entities.
•This line is very frequent in C++ programs that use the standard library,
and in fact it will be included in most of the source codes included in
these tutorials.
int main ()
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators
KEYWORDS
•The keywords implement specific C++ language feature.
•They are explicitly reserved identifiers and can’t be used as names for
the program variables or other user defined program elements.
•The keywords not found in ANSI C are shown in red letter.
In ANSI C the maximum length of a variable is 32 chars but in c++ there is no bar.
Constants
•The constants in ‘C++’ are applicable to the values that do not
change during execution of a program.
Write a program to demonstrate various constants.
#include<iostream.h>
#include<conio.h>
int main()
{
int x;
float y;
char z;
double p;
clrscr();
x=20;
y=2e1;
z=‘a’;
p=3.2e20;
cout<<“x=”<<x<<endl;
cout<<“y=”<<y<<endl;
cout<<“z=”<<z<<endl;
cout<<“p=”<<p; OUTPUT
return 0; x=20
} y=20
z=a
p=3.2e+20
BASIC DATA TYPES IN C++
Functions
#include<conio.h>
#include<iostream.h>
int main()
{
clrscr();
void show (void);
show();
return 0;
}
void show()
{
cout<<“\n In function show()”;
}
Differences between c and c++
(1) C is a procedure/function-oriented language and C++ language is driven by
a procedure/object.
(2) Data is not protected in C, whereas data is secured in C++. Data hiding
concept is absent in C.
(3) C uses a top down approach while C++ uses a bottom up approach. The
program is prepared step by step in C, and in C++ base elements are prepared
first.
(4) In C, we cannot give the same name to two functions in a program, whereas
due to the function overloading feature, the above concept is possible in C++.
One can initialize a number of functions with the same name, but with different
arguments. The polymorphism feature is built in C++, which supports this
concept.
Differences between c and c++
(5) C uses printf() and scanf() functions to write and read the data respectively,
while C++ uses cout and cin objects for output and input operations,
respectively. Further, the cout uses << (insertion operator) and cin uses >>
( extraction operator).
(6) C uses stdio.h file for input and output functions, whereas C++ uses
iostream.h for these functions.
(7) Constructor and destructors are absent in C and the same are provided in
C++.
(8) Inline functions are supported by C++, and the same are absent in C. Inline
functions can be used as micros. They are stated by a word ‘inline’.
Thank You