0% found this document useful (0 votes)
3 views41 pages

Chapter 1 - Introduction To C++ Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views41 pages

Chapter 1 - Introduction To C++ Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Chapter 1: Introduction to C++

Programming
What is C++?
• C++ was developed by Bjarne Stroustrup, as
an extension to the C language.
Why Use C++
• C++ is one of the world's most popular
programming languages.
• C++ is an object-oriented programming language
which 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.
Difference between C and C++
• C++ was developed as an extension of C, and
both languages have almost the same syntax.
• The main difference between C and C++ is that
C++ support classes and objects, while C does
not.
OOP Features
Class

• Collection of objects of similar type is called


class. It is a logical entity.
• A class can also be defined as a blueprint from
which you can create an individual object.
• Class doesn't consume any space.
Object
• An objects is an instance of a class, it is basic runtime entity
in an object oriented system.
• Any entity that has state and behaviour is known as an
object.
• In a programming terms an objects is a collection of data
and member functions.
• And in general terms an objects is a real time entity. Ex:
students , books etc.
• An object contains an address and takes up some space in
memory.
• Example: A dog is an object of class animals because it has
states like colour, name, breed, etc. as well as behaviours
like wagging the tail, barking, eating, etc.
Abstraction

• It basically deals with hiding the details and


showing the essential things to the user.
• Abstraction refers
to representation of necessary features wi
thout including more details or e
xplanations.
Example , If you look at the image here,
whenever we get a call, we get an option to
either pick it up or just reject it. But in reality,
there is a lot of code that runs in the
background. So you don’t know the internal
processing of how a call is generated, that’s
the beauty of abstraction.
Encapsulation
• Encapsulation is a mechanism where you bind your
data{variables} and function{methods} together as a single
unit.
• Class is the best example of encapsulation.
• In encapsulation the data is not accessible to the functions
outside the class.
• It also means to hide your data in order to make it safe
from any modification.
• What does this mean? The best way to understand
encapsulation is to look at the example of a medical
capsule, where the drug is always safe inside the capsule.
Similarly, through encapsulation the methods and variables
of a class are well hidden and safe.
Inheritance
• Inheritance is a process by which objects of
one class acquires the properties of objects of
another class.
• It helps to reuse the code and establish a
relationship between different classes.

As we can see in the image, a child


inherits the properties from his
father.
Polymorphism
• Polymorphism is derived from 2 Greek words:
poly and morphs. The word "poly" means
many and "morphs" means forms. So
polymorphism means many forms.
• For example, + is used to make sum of two
numbers as well as it is used to combine two
strings.
Differences between Procedural and
Object Oriented Programming.
Sr. Procedural Oriented Object Oriented Programming
No. Programming (POP) (OOP)
1. Functions are more important The main priority is data rather
than data in a program. than functions in a program.
2. Most of the functions share Data structures are designed in
global data. such a way that functions that
operate on data are ties together
called class.(Encapsulation)
3. Object oriented programming
There is no access specifier in
have access specifiers like private,
procedural programming
public, protected etc.

4.
Examples: C. Examples : C++, Java.
Programming Language
Programming Languages
• A computer cannot understand human languages
directly. So, we need a language to communicate
with a computer.
• Simply, the languages which are used to
communicate with a computer are called
Programming languages.
or
• Computer work on instructions given to them in a
specific language , which is known as
Programming Language
Types Of Computer Languages
3 Level of Programming Language:
Low Level Language:
• A Low-level computer language includes only 1’s
and 0’s.
• This language was used in first and second
generation computers.
• A Low level language is very easily understood by
a computer but hard to understand for Humans.
• Low level languages are categorized into two
types:
1. Machine level language and
2. Assembly level language.
Machine Language:
• As discussed above, Machine level language is a type of
Low level language.
• Machine language is considered to be the oldest computer
language.
• Machine language is developed by only using binary
numbers i.e., 0 and 1. So, the instructions or the statements
in this language are written in series of 0’s and 1’s only.
• The processing speeds is very fast because microprocessor
can process directly without a compilation. Therefore most
of the games and simulation programs are written in
machines languages.
Advantages of Machine Language
• Machine language programs and code run rapidly because
they are in binary format.
• A translator is not necessary for machine language.
Disadvantages of Machine Language
• The main drawback of machine language is, it is difficult to
develop and learn.
• It is pretty time-consuming to fix flaws and mistakes in codes
and programs.
• It is platform dependent language.
• Machine language coding requires a significant amount of
time.
• It's hard to recall instructions in numerical form, and this
causes mistakes. Only some people can memorize or even
write the code.
Assembly Language:
• Assembly Language is a symbolic programming
language.
• It uses symbolic (letter) codes instead of binary
digits(numbers).
• These symbolic (letter) codes are alphabetical
abbreviation, known as Mnemonics.
• For example-For Subtraction, the Mnemonics SUB, For
Multiply ,the Mnemonics MULT, For Divide, the
Mnemonics DIV and so on may be used.
• Programme written is Assembly Language is converted
into executable machine code with the help of
Assembler.
• The Assembly language code will be converted
into a Machine language code with the help of
an Assembler so that the computer can
understand the binary converted Assembly
Language.
Advantages of Assembly Language:
• It is memory efficient, as it requires less memory.
• It is faster in speed, as its execution time is less.
• It is mainly hardware-oriented.
Disadvantages of Assembly Language:
• It is very complex and difficult to understand.
• The syntax is difficult to remember.
• It takes a lot of time and effort to write the code.
• It has a lack of portability of program.
High Level Language:
• These languages are also known as Fourth generation languages.
• They are very similar to English languages.
• It is very easy to understand by Human but difficult for computer to
understand.
• Computer need a program which converts high level language to
machine level or low level language known as compiler or
interpreter to understand it.
• Example: C/C++,HTML,JAVA etc.
Advantages of High level language
• High level languages are programmer friendly.
They are easy to write, debug and maintain.
• It is machine independent language.
• Easy to learn.
• Less error prone, easy to find and debug
errors.
• High level programming results in better
programming productivity.
Disadvantages of High level language
• It need language translator to translate
program in to machine language.
• It takes additional translation times to
translate the source to machine code.
• High level programs are comparatively slower
than low level programs.
• Compared to low level programs, they are
generally less memory efficient.
Low Level Computer Language High Level Computer Language
1.Low level languages are hard to understand by humans as High level languages are simple to understand by humans as
they use binary numbers and easy to understand by the they use English statements.
computer.

2.Low level languages are programmer-friendly High level languages are human friendly.

3. Program execution time (computational speed) is less. Program execution time is longer.

4.These are complex to maintain. These are simple to maintain.

5.Debugging process is hard. It is easy to debug in High level languages.

6.The programs in low level language are not portable from The programs in a high level language are portable, so we
one computer to another. can use them on any computer.
Compiler And Interpreter

•A compiler is a special program that translates a


programming language's source code into machine
code.

•An interpreter is a computer program that directly


executes instructions written in a programming or
scripting language, without previously compiling them
into a machine language program.
Difference between compiler and
interpreter
Interpreter Compiler
Translates program one statement at Scans the entire program and
a time. translates it as a whole into machine
code.
Interpreters usually take less amount Compilers usually take a large amount
of time to analyze the source code of time to analyze the source code
but the overall execution time is but the overall execution time is
comparatively slower than compilers. comparatively faster than
interpreters.
Debugging is easy. Debugging is comparatively hard.
Programming languages like Python, Programming languages like C, C++,
Ruby use interpreters. Java use compilers.
Basic Structure of a C++ Program
//This program will display Hello World on screen
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!“ ;
return 0;
}
Basic Programming Structure
• Preprocessor
• Comments
• Functions
• Line of code:
• Return statements
Comments
• Comments are an essential element of a program.
• Comments increase the readability of a program.
• comments are not executable statements. Thus, they do
not increase the size of a file.
• C++ support two types of comments,
1.Single line comment
2. Multiline comment

/ / single line comment

/* An example to demonstrate
multiline comment */
Preprocessor Directive
• The Preprocessor Directive begins with the character #.
It is used to include the necessary header file in a C++
program before compilation.

Header File
• The Header File contains built-in functions, classes,
keywords, constants, operators and more which are
already defined in the standard C++ library.
• For using such pre-defined elements in a program, an
appropriate header must be included in the program.
• When we include header file in C++ program
using #include <filename.h> command, all codes inside
the header file is included in the C++ program
Namespace std
• When we use using namespace std into the C++
program, then it does not require to write std:: in
front of standard commands throughout the
code.
std::cout << "Hello World!";
cout << "Hello World!";
Declaration Section
• This section is used to define global variables to
be used in the programs, that means you can use
these variables throughout the program.
Function:
• Every C++ program possesses a functions and it returns a
value . You can write multiple functions in C++ program.
• Every C++ program always contains a function called the
main functions .
• The execution of the program starts with the main function
even if there are other functions written before or after the
main function.
Return Statement
• This line indicates the end of the main functions and C++
program. This line will be a part of every C++ program.
• Once all instructions from the main function are executed,
control comes out of the main function and the program
terminates and no further execution occur.
Opening Brace
• This is called the Opening Brace {. Whatever we will write
inside the main function, we will write it after the Opening
Brace.
Body of Main Function
• In this section, we will write our C++ program.
Main Function Return Value
• If the program execution is normal, a 0 return value is used.
Abnormal termination(errors, invalid inputs, segmentation
faults, etc.) is usually terminated by a non-zero return.
Closing Brace
• This is called the Closing Brace }.
Function Definition Section
• When we want to define our function that fulfills a
particular requirement, we can define them in this section.

https://www.dremendo.com/cpp-programming-tutorial/cpp-program-
basic-structure

You might also like