0% found this document useful (0 votes)
31 views

L2 - INTRODUCTION TO C++ PROGRAMING

Uploaded by

lkixome
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

L2 - INTRODUCTION TO C++ PROGRAMING

Uploaded by

lkixome
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

C++ PROGRAMMING

By Sir. Joshua
Definition
 C++ is a cross-platform language that can be
used to create high-performance applications.
 C++ was developed by Bjarne Stroustrup,
as an extension to the C language.
 C++ gives programmers a high level of control
over system resources and memory.
 The
language was updated 4 major times in
2011, 2014, 2017 , 2020 , and 2023 to C++11,
C++14, C++17, C++20 and C++23.
Why Use C++?
 C++ is one of the world's most popular programming
languages.
 C++ can be found in today's operating systems,
Graphical User Interfaces, and embedded systems.
 C++ is an object-oriented programming language
which gives a clear structure to programs and allows
code to be reused, lowering development costs.
 C++ is portable and can be used to develop
applications that can be adapted to multiple
platforms.
 C++ is fun and easy to learn!
 As C++ is close to C, C# and Java, it makes it easy for
programmers to switch to C++ or vice versa.
History of C++
 History of C++ language is interesting to know.
 Here we are going to discuss brief history of C++ language.
C++ programming language was developed in 1980 by
Bjarne Stroustrup at bell laboratories of AT&T (American
Telephone & Telegraph), located in U.S.A. Bjarne
Stroustrup is known as the founder of C++ language.
 It was develop for adding a feature of OOP (Object
Oriented Programming) in C without significantly
changing the C component.
 C++ programming is "relative" (called a superset) of C, it
means any valid C program is also a valid C++ program.
Programming languages that were
developed before C++ language

Language Year Developed By

Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis Ritchie

C++ 1980 Bjarne Stroustrup


Difference between C and C++
No C C++
.

1) C follows the procedural C++ is multi-paradigm. It supports


style programming. both procedural and Object oriented.

2) Data is less secured in C. In C++, you can use modifiers for class members
to make it inaccessible for outside users.

3) C follows the top-down C++ follows the bottom-up approach.


approach.

4) C does not support C++ supports function overloading.


function overloading.

No C C++
.

5) In C, you can't use functions in In C++, you can use functions in


structure. structure.

6) C does not support reference C++ supports reference variables.


variables.

7) In C, scanf() and printf() are C++ mainly uses stream cin and cout to
mainly used for input/output. perform

Input and output operations.

8) Operator overloading is not Operator overloading is possible in C++.



No C C++
.

9) C programs are divided C++ programs are divided into functions


into procedures and modules and classes.

10) C does not provide the feature C++ supports the feature of namespace.
of namespace.

11) Exception handling is not easy C++ provides exception handling using
in C. It has to perform using Try and
other functions. Catch block.

12) C does not support the C++ supports inheritance.


C++ Features
1) Simple
 C++ is a simple language because it provides a
structured approach (to break the problem into
parts), a rich set of library functions, data types, etc.
2) Abstract Data types
 In C++, complex data types called Abstract Data
Types (ADT) can be created using classes.
3) Portable
 C++ is a portable language and programs made in it
can be run on different machines.

4) Mid-level / Intermediate programming language
 C++ includes both low-level programming and high-level
language so it is known as a mid-level and intermediate
programming language. It is used to develop system
applications such as kernel, driver, etc.
5) Structured programming language
 C++ is a structured programming language. In this we can
divide the program into several parts using functions.
6) Rich Library
 C++ provides a lot of inbuilt functions that make the
development fast. Following are the libraries used in C++
programming are:
<iostream>, <cmath>, <cstdlib>, <fstream>

7) Memory Management
 C++ provides very efficient management techniques. The
various memory management operators help save the memory
and improve the program's efficiency. These operators allocate
and deallocate memory at run time. Some common memory
management operators available C++ are new, delete etc.
8) Quicker Compilation
 C++ programs tend to be compact and run quickly. Hence the
compilation and execution time of the C++ language is fast.
9) Pointer
 C++ provides the feature of pointers. We can use pointers for
memory, structures, functions, array, etc. We can directly
interact with the memory by using the pointers.

10) Recursion
 In C++, we can call the function within the function. It
provides code reusability for every function.
11) Extensible
 C++ programs can easily be extended as it is very easy
to add new features into the existing program.
12) Object-Oriented
 In C++, object-oriented concepts like data hiding,
encapsulation, and data abstraction can easily be
implemented using keyword class, private, public, and
protected access specifiers. Object-oriented makes
development and maintenance easier.
Structure of C++ program
(Syntax)
• Syntax: the
formal rules of
formulating the
statements in a
given
programming
language.
• Consider the
following simple
C++ program to
display text
“Hello World!” .
Description of each line of
codes in a program
 Line 1: #include <iostream> is a header
file library that lets us work with input and
output objects, such as cout (used in line 5).
Header files add functionality to C++
programs.
 Line2: using namespace std means that
we can use names for objects and variables
from the standard library.

 Line3: A blank line. C++ ignores white
space. But we use it to make the code more
readable.
 Line 4: Another thing that always appear in a
C++ program is int main(). This is called a
function. Any code inside its curly brackets {}
will be executed.
 Line5: cout (pronounced "see-out") is an
object used together with the insertion
operator (<<) to output/print text. In our
example, it will output "Hello World!".

 Line 6: return 0; ends the main function.
 Line 7: Do not forget to add the closing curly
bracket } to actually end the main function.

NOTE
 Every C++ statement ends with a semicolon (;).
 The body of int main() could also been written as:
int main () { cout << "Hello World! "; return
0; }
 The compiler ignores white spaces. However, multiple
lines makes the code more readable.
Omitting Namespace
 You might see some
C++ programs that
runs without the
standard
namespace
library.
• The using
namespace std line
can be omitted and
replaced with the
std keyword,
followed by the ::
C++ Statements
 A computer program is a list of “instructions” to be
“executed” by a computer.
 These instructions are called “statements”.
 Every C++ statement ends with a semicolon (;), if
you forget the semicolon an error will occur and the
program will not run.

cout << “Hello World”;
For example:
 Instructs a computer to print the text “Hello World”
 Most C++ programs contain many statements, these
statements are executed one by one in the same order
as they are written.
Environment setup for C++

program
To start using C++, you need two things:
a) A text editor, like Notepad, to write C++ code
b) A compiler, like GCC, to translate the C++ code
into a language that the computer will understand.
 An IDE (Integrated Development Environment) is
used to edit AND compile the code.
 Popular IDE's include Code::Blocks, Eclipse, Dev
C++ (. Embarcadero DEV C++) and Visual
Studio. These are all free, and they can be used to
both edit and debug C++ code.
Writing of C++ program in the
content pane of the compiler
Saving a C++ program

 From the coding window of C++ program,


click File followed by Save As to save the C+
+ program.
 Then,the dialog box to choose a folder for
saving a program will be displayed.
 Typea file name ending with .cpp as file
extension, eg: “hello_world.cpp” in the file
name, then click Save to save your file.
Compiling a C++ program

 It is important to compile your program not only


to change source code to object code, but also to
find errors in the program.
 Compile your program by clicking the “Execute”
tab from the tool bar, then click “Compile.”
 If there is any error the compiler will display the
error for correction.
 Otherwise, the next step of running a program
will follow.

Running a C++ program

 Running
a program involves copying a
program to the RAM.
 Themessage is sent to the computerto
prepare the environment for operating a
program.
 Runyour C++ program by clicking the
Execute” tab, then click “Run.”

Comments in C++ program
(Documentation)
 Comments can be used to explain C++
code, and to make it more readable.
 It
can also be used to prevent execution
when testing alternative code.
 Comments can be singled-lined or multi-
lined.
Single-line Comments
 Single-line comments start with two forward
slashes (//).
 Any text between // and the end of the line is
ignored by the compiler (will not be executed).
 These are examples of single-line comments:
Multi-line Comments

 Multi-line comments start with /* and ends


with */.
 Any text between /* and */ will be ignored by
the compiler:
Activity

 Write, compile and run a C++ program that


will display your name.

You might also like