01 Introduction To C++
01 Introduction To C++
Introduction to C++
BCS-1102: Fundamentals of
Programming
C++
• C++ is a middle-level programming language
• Developed by Bjarne Stroustrup
• Starting in 1979 at Bell Labs
• C++ runs on a variety of platforms
– Windows,
– Mac OS,
– various versions of UNIX
• Designed with a bias toward system programming
and embedded
2
C++
A Programming Language
• Artificial Language
• General-purpose programming language
• Features
– Object-oriented & generic programming features
– Low-level memory manipulation
• Consists of
– Key words
– Syntax
– Semantics
C++ Vs Natural Languages
• C++ • Natural Language
– Artificial Language – Natural
– Consist of – Consist of
• Words
• Keywords
• Syntax
• Syntax
• Semantics
• Semantics
– Translate trough the
– Translate through Machine
the Translation systems
Compilers or Human
Key words/ words
• C++ • Natural Language
Syntax
• C++ • Natural Language
Rules for construction of valid Grammar rules, subject, object,
statements, including, Order of verbs etc.
words, Punctuation
Semantics
• C++ • Natural Language
The set of rules that determines Is the study of meaning
the meaning of instructions (what
the computer will do) written in a
programming language.
Minimum C++ Program
• Do nothing
C++ Block
• A block is a set of logically connected statements
that are surrounded by opening and closing braces.
Blocks
B
C++ semicolon
• The semicolon is a statement terminator. That is,
each individual statement must be ended with a
semicolon.
Example
• Create a C++ program to print message on console
window
Example
• Is a C++ statement.
• This statement performs the only action that
generates a visible effect in our first program.
• Command
Example
21
Example 2
• Create a C++ program to display your name and
address
23
Cording Styles
24
New Line
Example 2
• What is output of the following program
Output
Escape sequences
• Escape sequences are used to represent certain special
characters within string literals (“ ”)
Escape sequence Description
\' single quote
\" double quote
\? question mark
\\ backslash
\a audible bell
\b backspace
\f form feed - new page
\n line feed - new line
\r carriage return
\t horizontal tab
ASCII art with C++
ASCII art is a graphic design technique that uses computers
for presentation and consists of pictures pieced together
from the 95 printable (from a total of 128) characters defined
by the ASCII Standard from
cout<<" @ @ @ \n“;
cout<<" @ @ @ @ \n“;
cout<<" @ @ @ \n“;
cout<<" \\|/ \n“;
cout<<" | | | \n“;
cout<<" | | | \n“;
cout<<" | | | \n“;
cout<<" | | | \n“;
cout<<" ( \n“;
)
cout<<" ^^^^^ \n“;
Example
• Write a C++ program to display your name using
Ascii Art
29
Exercise
1. Write a C++ Program to Display the Following output
C1033
Fundamentals of Programming
30
Different ways to create a
C++ program
Different ways to create a
C++program
Different ways to create a
C++program
Different ways to create a
C++program
Template for a C++ Program
C++ Programming
C++ Compiler
Operating
system can
directly execute
program
e)
36
Preparing a C++ program for
running
37
Compiling
38
Compile errors
Compilation Errors
• Compiler fails to compile a piece of computer
program source code.
• Error message is given
Common C++ compilation errors
• Undeclared identifier
• Common function undeclared
• = expected
• Internal compiler error
• Unexpected closing brace
Example
• Write the following C++ program and identify
Compilation errors
Example
• Correct errors and rewrite the program
Clear the console screen
Header
Command
Change console Text and background
color
• Sets the default console foreground and background
colours.
• Syntax
– COLOR [background][foreground]
– system("Color FA");
– system("Color F0");
Color Code
• 0 = Black • 8 = Gray
• 1 = Blue • 9 = Light Blue
• 2 = Green • A = Light Green
• 3 = Aqua • B = Light Aqua
• 4 = Red • C = Light Red
• 5 = Purple • D = Light Purple
• 6 = Yellow • E = Light Yellow
• 7 = White • F = Bright White
Example
• Write a C++ program to display following screen
USER INFORMATION
NAME : B. HETTIGE
ADDRESS: No23, Panadura
AGE : 19
SALARY : 23500
GENDER : M
Summary
• C++ Programming Language?
• C++ Vs Natural Languages
• C++ Syntax
• Create a C++ program using code:blocks
• Cording styles
• ASCII Art
• Handle compile errors
• Customize Console screen
48