Programming in C++

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Programming

in C++
Introduction to C++
C++ is a general-purpose
programming language that was
developed as an enhancement of the
C language to include object-oriented
paradigm. It is an imperative and
a compiled language
• C++ is a middle-level language rendering it the
advantage of programming low-level (drivers,
kernels) and even higher-level applications
(games, GUI, desktop apps etc.). The basic
syntax and code structure of both C and C++
are the same. 
Some of the features & key-points to note about the
programming language are as follows :
• Simple
• Platform Dependent
• Mid-Level Language
• Rich library Support
• Speed of Execution
• Pointer and Direct Memory Access
• Object oriented
• Compiled Language
Applications of C++:
• Operating Systems & Systems Programming.
e.g. Linux-based OS (Ubuntu etc.)
• Browsers (Chrome & Firefox)
• Graphics & Game engines (Photoshop,
Blender, Unreal-Engine)
• Database Engines (MySQL, MongoDB, Redis
etc.)
• Cloud/Distributed Systems
Syntax of C++
• #include <iostream>
• using namespace std;

• // main() is where program execution begins.


• int main() {
• cout << "Hello World"; // prints Hello World
• return 0;
• }
• The C++ language defines several headers, which
contain information that is either necessary or
useful to your program. For this program, the
header <iostream> is needed.
• The line using namespace std; tells the compiler to
use the std namespace. Namespaces are a relatively
recent addition to C++.
• The next line '// main() is where program execution
begins.' is a single-line comment available in C++.
Single-line comments begin with // and stop at the
end of the line.
• The line int main() is the main function where
program execution begins.
• The next line cout << "Hello World"; causes
the message "Hello World" to be displayed on
the screen.
• The next line return 0; terminates
main( )function and causes it to return the
value 0 to the calling process.
• Compile and Execute C++ Program
• Let's look at how to save the file, compile and run the program. Please
follow the steps given below −
• Open a text editor and add the code as above.
• Save the file as: hello.cpp
• Open a command prompt and go to the directory where you saved the
file.
• Type 'g++ hello.cpp' and press enter to compile your code. If there are
no errors in your code the command prompt will take you to the next
line and would generate a.out executable file.
• Now, type 'a.out' to run your program.
• You will be able to see ' Hello World ' printed on the window.
• $ g++ hello.cpp $ ./a.out Hello World
C++ Data Types

• C++ supports the following data types:


• Primary or Built in or Fundamental data type
• Derived data types
• User defined data types
Flowchart In Programming

Symbol Purpose Description

Indicates the flow of


Flow line logic by connecting
symbols.

Represents the start


Terminal(Stop/Start) and the end of a
flowchart.

Used for input and


Input/Output
output operation.
Used for arithmetic
Processing operations and data-
manipulations.

Used for decision


making between
Decision two or more
alternatives.

Used to join
On-page Connector
different flowline
Used to connect the
Off-page Connector flowchart portion on
a different page.

Represents a group
Predefined of statements
Process/Function performing one
processing task.
Examples of flowcharts in
programming
• 1. Add two numbers entered by the user.
• 2. Find the largest among three different numbers entered by the user.
Introduction to C++ Algorithm
• The finite set of steps arranged sequentially
which acts as a guide to solve any problem.
This c++ algorithm word is particularly 
used in computer science to define the
procedure for solving complex problems. The
architecture of the solution can be different
for different algorithms. 
Example1
• Write a C++ algorithm to write a program to add two
numbers.
• Algorithm
• Steps are given below:
• Start
• Accept num1, num 2
• Sum= num1+ num2
• Display sum
• Stop
Example2
• Write a C++ algorithm to determine if a student is pass or fail based on the
grades. Grades are the average of total marks obtained in all the subjects.
• Algorithm
• Steps are given below:
• Start
• Input Marks1, Marks2, Marks3, Marks4
• Grade= (Marks1+Marks2+Marks3+Marks4)/4
• If (Grade<50) then
• Print “Fail”
• Else
• Print “Pass”
• End if
• Stop

You might also like