2. Todays Lecture
▪ Introduction
▪ Why programming?
▪ Programming Languages
▪ C++ as a programming Language
▪ First C++ Program
▪ Program Structure
▪ Semicolons and Blocks in C++
▪ Comments in C++
▪ Variable in C++
▪ Data type in C++
3. Why Programming?
▪ Here are two main reasons why one should learn programming. Most
Demanded Profession:These days technology is becoming so advance
with the help of computers. And for running such computers we need
software that is made using programming languages. It shows that
there are huge job opportunities in present and future.
▪ Our Civilization runs on Software:
• Most Engineering activities involves software
▪ Programming is important
• To interact with machines and computes
• To automate tasks
• To create intelligent machines, etc
4. Programming Languages
▪ Programmers write program in various programming languages,
some are directly understood by computers and other requiring
intermediate translation steps. Hundred of such languages are in
use today.These maybe divided into three general categories:
Machine language
Assembly language
High level language
• Procedural languages
• Object Oriented languages
5. Computer Program
▪ A computer program is a collection of instructions that
performs a specific task when executed by a computer.
▪ A computer program is usually written by a computer
programmer in a programming language.
▪ A collection of computer programs, libraries, and
related data are referred to as software.
6. C++ as a programming Language
▪ C++ is a sophisticated, efficient and a general-purpose
programming language based on C. It was developed by Bjarne
Stroustrup in 1979.
▪ Many of today’s operating systems, system drivers, browsers
and games use C++ as their core language.This makes C++ one
of the most popular languages today.
▪ Since it is an enhanced/extended version of C
programming language, C and C++ are often denoted together
as C/C++.
7. Features of C++
▪ C++ is fast: Since, C++ is an extended version of C, the C part of it is very low level.
This offers a huge boost in speed that high level languages like Python, Java don’t give
you.
▪ C++ is statically typed: In simple terms, C++ doesn’t allow the compiler to make
assumptions about the type of data e.g. 10 is different from “10” and you have to let C++
know which one you are talking about. This helps the compiler catch errors and bugs
before execution of the program.
▪ C++ is a multi-paradigm programming language: C++ supports at least 7 different
styles of programming and gives developers the freedom to choose one at their will.
▪ Object oriented programming with C++:Object oriented programming helps you solve
a complex problem intuitively. With its use in C++, you are able to divide these complex
problems into smaller sets by creating objects.
8. Uses of C++
▪ C++ is used by programmers to create computer software.
▪ It is used to create general systems software, drivers for various computer
devices, software for servers and software for specific applications and
also widely used in the creation of video games.
▪ C++ is used by many programmers of different types and coming from
different fields. C++ is mostly used to write device driver programs, system
software, and applications that depend on direct hardware manipulation
under real-time constraints.
▪ It is also used to teach the basics of object-oriented features because it is
simple and is also used in the fields of research.
▪ Many primary user interfaces and system files of Windows and Macintosh
are written using C++.
9. Major Application of C++
▪ Adobe Products like Photoshop, Illustrator, InDesign
▪ Amazon - one of the biggest e-commerce sites
▪ Autodesk products forComputerAided Design
▪ Facebook - social networking site are heavy C++ centric products.
▪ Browsers e.g Chrome
Moreover, the fact that there’s a huge community improving C++ on
every iteration means that it is only expected to be used even more in
the coming future.
10. BASIC STRUCTURE OF A C++ PROGRAM
#include<iostream.h>
#include<conio.h>
using namespace std;
void main()
{
clrscr();
**program code will be written here**
getch();
}
11. BASIC STRUCTURE OF A C++ PROGRAM
#include<iostream.h>
#is called Preprocessor directive
Preprocessing :
Preprocessing is the phase of compilation before the
actual execution.
12. BASIC STRUCTURE OF A C++ PROGRAM
#include<iostream.h>
#include<conio.h>
These are the header file inclusion statement.
Header Files:
Header files are the predefined files in the C++ library
using which we create new program.
13. BASIC STRUCTURE OF A C++ PROGRAM
iostream.h
Iostream stands for Input Output Stream
It contains functions related to input and
output. For ex.- cout,cin etc.
14. BASIC STRUCTURE OF A C++ PROGRAM
conio.h
conio stands for Console Input Output .
It contains functions related to Console screen input
and output.
>The output screen of C++ is called Console Screen.
15. BASIC STRUCTURE OF A C++ PROGRAM
using namespace std;
The statement is intuitive in itself, you are “using” the
“namespace” “std” in your file.
We use the namespace std to make it easier to reference
operations included in that namespace.
If we hadn’t used the namespace, we’d have written
std::cout instead of cout.
This tells the compiler that every cout is actually std::cout.
16. BASIC STRUCTURE OF A C++ PROGRAM
Semicolon ”;”
The semicolon is a terminal. It terminates a
statement.When missed or incorrectly
used, it will cause a lot of issues.
Ask any C++ programmer and they will tell
you at least one horror story related to the
semicolon ; .
17. BASIC STRUCTURE OF A C++ PROGRAM
voidmain()
This is the main() method/ function of a C++ program.
Actual execution of a program starts from main
method.
18. BASIC STRUCTURE OF A C++ PROGRAM
clrscr();
Function defined within conio.h file
It clears the previous output screen.
20. BASIC STRUCTURE OF A C++ PROGRAM
cout<<“ text to be display”;
Defined within iostream.h
The text written within double quotes(“ ”) will got displayed
on the output screen as it is.
21. BASIC STRUCTURE OF A C++ PROGRAM
getch();
Function defined within conio.h file
It holds the final output screen until any key is
pressed by user.
22. RULES OF C++ LANGUAGE
▪ Every program should have main()
▪ Every statement should be end with semicolon(;)
▪ This Language is case sensitive
▪ All the letter should be in lowercase.
23. Comments in a C++ Program
Comments are used to increase
understandability and readability of a program.
These comments will be ignored by the
compiler at the time of compilation/execution.
i.e, comments are part of a program but not the
part of compiled/executed code.
24. Type of Comments
SINGLE LINE COMMENT:
Starts with // and treats all the contents
following // as comments within that line
MULTIPLE LINE COMMENT:
Starts with /* and ends with */ and all the
contents in between will be treated as
comments
25. COMMENTS USAGE EXAMPLE
/*TITLE :- MY FIRST C++ PROGRAM
CREATED BY :- XYZ */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); // to clear the output screen
cout<<“My First C++ Program”;
getch(); //to pause the final output
}
26. Variable in C++
▪ Variable are used in C++, where we need storage for any value, which will change in
program
▪ Variable can be declared in multiple ways each with different memory requirements
and functioning.
▪ Variable is the name of memory location allocated by the compiler depending upon
the datatype of the variable.
27. Datatypes and Modifiers in C++
Let's start with Datatypes.They are used to define
type of variables and contents used. Data types define
the way you use storage in the programs you write.
Data types can be of two types:
1. Built-in Datatypes
2. User-defined orAbstract Datatypes
28. Built-in Data Types
▪ These are the datatypes which are predefined and are wired directly
into the compiler. For e.g: int, char etc
Example:
Datatype Description
char for character storage (1 byte)
int for integral number (2 bytes)
float single precision floating point (4 bytes)
double double precision floating point numbers (8 bytes)
29. Declaration and Initialization
▪ Variable must be declared before they are used. Usually it is preferred to
declare them at the starting of the program, but in C++ they can be declared in
the middle of program too, but must be done before using them.
30. Scope of Variables
All the variables have their area of functioning, and out of that
boundary they don't hold their value, this boundary is called
scope of the variable.
For most of the cases its between the curly braces, in which
variable is declared that a variable exists, not outside it. We can
broadly divide variables into two main types:
▪ GlobalVariables
▪ Local variables
31. Global variables
▪ Global variables are those, which are once declared and can be used
throughout the lifetime of the program by any class or any function.
▪ They must be declared outside the main() function.
▪ If only declared, they can be assigned different values at different time in
program lifetime.
▪ Even if they are declared and initialized at the same time outside the
main() function, then also they can be assigned any value at any point in
the program.
They must be declared outside the main() function.
33. Local Variables
▪ Local variables are the variables which exist only between the curly
braces, in which its declared.
▪ Outside that they are unavailable and leads to compile time error.