Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

CC – 102:

Fundamentals of
Programming

CHAPTER 2:
INTRODUCTION TO C++
PROGRAMMING

Presented by: CHRISTIAN A. FAJARDO, MACE


What is C++ Programming Language?

● C++ is a high – level, general – purpose programming


language that was developed as an extension to the
C programming language. It was designed by Danish
computer scientist Bjarne Stroustrup in 1979 at Bell
Labs.

● He wanted a language that could support object –


oriented programming, provide facilities for low –
level programming, and maintain compatibility with
the existing C codebase.
What is C++ Programming Language?

● Stroustrup initially called his new language "C with Classes,"


as it introduced the concept of classes to the C language.
These classes allowed for the implementation of object –
oriented programming principles such as encapsulation,
inheritance, and polymorphism.

● In 1983, the name was changed to C++ to emphasize its


object-oriented nature and to denote the increment in
features and capabilities it offered beyond the C language.
The ++ operator in C++ refers to the increment operator in C
that represents an enhancement over the original language.
Why use C++ Programming Language?

The following are some common reasons why use C++ as one of your very first
programming language to learn:

● 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.

● As C++ is close to C, C# and Java, it makes it easy for programmers to switch


to C++ or vice versa.
Getting Started with C++ Programming Language?

Generally, two things should be available before getting started with C++
programming:

● A text editor to write the C++ source code. However, it must be saved
using the .cpp extension name to indicate that the source code is
indeed intended for creating C++ programs.

● A compiler, to translate the C++ source code (which is on a high – level


language) into machine code so that the computer (specifically the
CPU) understand the contents of the code and convert it into a running
program.

Fortunately, IDEs (or an Integrated Development Environment) are


available for C++ programming that can do the said requirements on a
single software.
Getting Started with C++ using Integrated Development Environment

A screenshot for the Embarcardero Dev C++ IDE for C++ Programming
What is a syntax?

● A syntax refers to the set of rules and principles that govern


the structure and formation of sentences (or statements) in
a programming language.

● A syntax also determines the correct arrangement of words,


phrases, and symbols to create meaningful and
grammatically correct statements or instructions.

● Syntax is crucial in communication and programming, as it


ensures clarity, consistency, and proper interpretation of
language or code.
Understanding the basic C++ syntax structure

To understand the basic C++ syntax structure, let’s break


up the source code in the previous slide to understand it
better:
Understanding the basic C++ syntax structure

Line1: In C++ the #include represents a preprocessor directive that is


used to include a library or header file in the C++ source code. The
iostream is a header file library in C++ primarily used for input and output
operations.

Specifically, iostream allows the programmer to perform operations like:

1. Reading input from the user through the keyboard using cin and
storing it in variables.
2. Writing output to the console using cout.
3. Manipulating and formatting the output, such as setting precision or
aligning data.
4. Reading and writing data in various formats.
5. Handling errors and exceptions during input and output operations.
Understanding the basic C++ syntax structure

Line 2: Can be also seen on Line 6, it is just a blank line.


This is called a white space. And though white spaces are
completely ignored by the compiler during code
translation, programmers normally uses them to make
the source code more readable.

Line 3: int main()represents the main function of C++


program. And any code inside its curly brackets within
Lines 5 and 7 will be executed. The word int in this line
means that return statement is expected to return an
integer value (typically 0) to end the main function.
Understanding the basic C++ syntax structure

Line 4: The opening curly bracket { represents a marker


where codes of the main function begins.

Line 5: This line represents a single statement or


instruction in the C++ program. std refers to the
standard namespace, in which contains a collection of
functions, classes, and variable that are part of the C++
standard library. cout is an object in C++ that is a part of
the iostream library which allows programmers to print
or display information emphasized between the
beginning and ending double – quotation marks “ ”.
Understanding the basic C++ syntax structure

std is connected cout with use of a scope resolution


operator ::. And message or string inside the double –
quotation marks “ ” are connected to cout using the
insertion operator <<. Finally, the whole statement on this
line is terminated using a semi – colon ; which is also known
as the statement terminator in C, C++, Java and PHP.

Line 7: The return statement is commonly used as a marker


that the main function is about to end. The value 0 on it
signifies that it is the integer value needed to return that also
signifies a successful program run. This statement is also
ended with a semi – colon ;.
Understanding the basic C++ syntax structure

Line 8: The closing curly bracket } represents a marker


where codes of the main function ends as well as the
whole C++ source code.
Understanding C++ Variables

● In programming, variables are containers for data values. Variables


allow programmers to manipulate and operate on data by assigning
values to them and modifying those values throughout the program's
execution. They also enable the reuse of data within the program and
facilitate the interplay of different program elements.

● In C++, variables must be in a specified data type. The following are the
basic data types used:
Data Type Size Description
boolean 1 byte Stores true or false values

char 1 byte Stores a single ASCII value (letter / number / character)

int 2 or 4 bytes Stores whole numbers with no floating point numbers.

float 4 bytes Stores numbers with floating point numbers sufficient for 6 – 7 numbers.

double 8 bytes Stores numbers with floating point numbers sufficient for up to 15 numbers
Declaring C++ Variables

● To declare or create a variable in C++, specify the type


and assign it a value.

Example: int age = 10;

● To declare more than one variable of the same data


type, use a comma – separated list.

Example: int x,y,z=10;


int x = 10,y = 10,z = 10;
Rules for naming C++ Variables

The following are some general rules for naming


variables, not only in C++ programming language but on
other high – level programming languages as well:

1. Names can contain letters, digits and underscore.


2. Names must not begin with numbers.
3. Names for C++ are case – sensitive (similar to Java).
4. Names cannot contain whitespaces or special
characters except underscore.
5. Reserved words such as C++ keywords like int,
return, or main cannot be used as names.
Understanding C++ Comments

● 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 are ignored by the translator and will not be


executed at any part by the program.

● Comments can be written as singled – lined using two


forward slashes // or multi – lined (or block comment)
starting with /* and ends with */. Any text written
within the said characters will be ignored by the
translator.
CC – 102:
Fundamentals of
Programming

END of CHAPTER 2:
INTRODUCTION TO C++
PROGRAMMING

Presented by: CHRISTIAN A. FAJARDO, MACE

You might also like