Lecture 4
Lecture 4
Problem Solving
Concepts
To be covered
today
C++
C++ is used to develop games, desktop apps, operating systems, browsers, and so on
because of its performance.
After learning C++, it will be much easier to learn other programming languages like
Java, Python, etc.
C++ helps you to understand the internal architecture of a computer, how computer stores
and retrieves information.
Advantages of C++
1. Portability
C++ provides this feature of portability allowing us to develop codes without caring about the
hardware. This lets us move the development of a program from one platform to another.
For example, you’re working on Windows OS and for some reason, you have to switch to LINUX,
the codes from Windows OS will also run in the LINUX OS without any error.
2. Mid-level programming language
Being a mid-level programming language, we can treat it as both a low-level and high-level
language. Features of high-level language help to develop games and desktop applications, whereas
features of low-level language help make kernels and drivers.
Advantages of C++
4. Low-level Manipulation
Since C++ is closely associated with C, which is a
procedural language closely related to the machine language,
C++ allows low-level manipulation of data at a certain level.
Embedded systems and compiler are created with the help of
C++.
Advantages of C++
5. Memory Management
C++ gives the programmer the provision of total control over memory management.
7. Compatibility with C
C++ is pretty much compatible with C.
8. Scalability
Scalability refers to the ability of a program to scale. It means that the C++ program is
capable of running on a small scale as well as a large scale of data.
Program
iostream is the header file which contains the functions of program like cout, cin.
<iostream> is called as input-output stream for C++, which comprises of various defined
functions for the console.
#include <iostream>
Namespace std
The compiler distinguished between the variable name used in the C++ program or a
variable used in the library function.
std is the standard
Namespace is a declarative region where input, output functions are defined for a class
(C++).
cout, cin and a lot of other functions are defined in it.
Syntax:
using namespace std;
Also call these functions using std::cout , std::cin.
Main() Function & Program Body
An executable statement causes the computer to perform some specific action when the
program runs.
Display output
Executable statements must always end with a semicolon.
A non-executable statement describes some feature of the program or its data but
doesn’t cause the computer to perform any action when a program runs.
Comment (Explanatory remarks made in a program)
Line Comment (// Comment)
Block Comment (/* set of lines */)
cout Object
Console Output
cout<< “Hello World !!!”;
<< represents insertion operator
“ ” represents the string as sentence written to be displayed as output on console screen.
Statement must be terminated with a semicolon.
Program Structure
Main Function
#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World!!!”;
return 0;
}
Common Coding Problems
The errors commonly made when first programming in C++ include the following:
1. Omitting the parentheses after main().
2. Omitting or incorrectly typing the opening brace, {, that signifies the start of a function body.
3. Omitting or incorrectly typing the closing brace, }, that signifies the end of a function.
4. Omitting the semicolon at the end of each C++ executable statement.
5. Adding a semicolon after the #include <iostream> preprocessor command.
6. Misspelling the name of an object or function, such as typing cot instead of cout.
Escape Character
Backslash ( \ )
Placed in front of a group of characters, it tells the compiler to escape from normal
interpretation of these characters
Escape sequence
CSPF-121
“Programming Fundamentals”
References
Gary j. Bronson, A first book of C++, Garry Bronson, 4th edition (Chapter 1)