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

CH 1- Introduction to Cpp

Uploaded by

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

CH 1- Introduction to Cpp

Uploaded by

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

What is C++?

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, and 2020 to C++11, C++14, C++17,
C++20.

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.

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.
The trace of the programming language C++ can be done back to 1979 when Bjarne Stroustrup was doing some development
for this thesis for PhD. He is master of Simula 67 & C,wanted to combine the featrues of both. He developed powerful language
which supports object oriented programming with features of C.
C++ was initially known as “C with classes, ” and was renamed C++ in 1983. ++ is
shorthand for adding one to variety in programming; therefore C++ roughly means that
“one higher than C.”
Procedural Programming is a programming language that
follows a step-by-step approach to break down a task into a
collection of variables and routines (or subroutines) through a
sequence of instructions. In procedural oriented programming,
each step is executed in a systematic manner so that the
computer can understand what to do.

The programming model of the procedural oriented


programming is derived from structural programming. The
concept followed in the procedural oriented programming is
called the "procedure". These procedures consist several
computational steps that are carried out during the execution
of a program. Examples of procedural oriented programming
language include – C, Pascal, ALGOL, COBOL, BASIC, etc.

There are 2 kinds of Data: Local Data & Global Data


local data is defined within associated functions.
Global data items are defined in the main program
Features of C++
•Simple: It is a simple language in the sense that programs can be broken down into logical units
and parts, has a rich library support and a variety of data-types.
•Machine Independent but Platform Dependent: A C++ executable is not platform-
independent (compiled programs on Linux won’t run on Windows), however they are machine
independent.
•Mid-level language: It is a mid-level language as we can do both systems-programming (drivers,
kernels, networking etc.) and build large-scale user applications (Media Players, Photoshop, Game
Engines etc.)
•Rich library support: Has a rich library support (Both standard ~ built-in data structures,
algorithms etc.) as well 3rd party libraries (e.g. Boost libraries) for fast and rapid development.
•Speed of execution: C++ programs excel in execution speed. Since, it is a compiled language,
and also hugely procedural. Newer languages have extra in-built default features such as garbage-
collection, dynamic typing etc. which slow the execution of the program overall. Since there is no
additional processing overhead like this in C++, it is blazing fast.

Pointer and direct Memory-Access: C++ provides pointer support which aids users to directly manipulate storage address.
This helps in doing low-level programming (where one might need to have explicit control on the storage of variables).
Object-Oriented: One of the strongest points of the language which sets it apart from C. Object-Oriented support helps C++
to make maintainable and extensible programs. i.e. Large-scale applications can be built. Procedural code becomes difficult
to maintain as code-size grows.
Compiled Language: C++ is a compiled language, contributing to its speed.
Platform Dependent
Platform dependent language means the language in which programs can be executed only on that operating system where
Procedure oriented Programming VS Object
Oriented Programming:-

• In this approach the problem is viewed as a sequence of things like


reading,printing ,calculating and so on.
• C,COBOL,FORTRAN and PASCAL are procedure oriented languages.
• Using programming procedure oriented approach the programmer views a
problem as a sequence of things to do.
• The programmer organizes the related data items and writes the necessary
functions to manipulate the data and the process ,complete the sequence of tasks
that solve the problem.
• POP(Procedure Oriented Programming) focus is on the functions.
• A program in a procedural language is a list of instructions.When program becomes
larger ,it is broken down into smaller units ,called functions or procedures.
• Each function has a clearly defined purpose and a clearly defined interface to the
other functions in the program. A number of functions can be grouped together into
a larger entity called a module.
• Fig. :- Typical Structure of Procedure Oriented Programs.
Main Program

Function-1 Function-2

Function-3 Function-4

Function-5
• A function is also called as procedure. A procedure call is used to invoke
the procedure. After the sequence is processed ,flow of control proceeds
right after the position where the call was made as shown in fig. below:-
• But there are problems with procedure oriented programming (i.e.
structured programming),as programs become larger and complex,they
become difficult to handle . There are two related reasons, first functions
have unrestricted access to global data and second ,unrelated functions
and data provide a poor model of the real world.
• Fig. : Flow of control in procedural programming:-
• Main Program

Procedure
• Let us see, what we mean by unrestricted access to global data .When we
concentrate on the development of functions, very little attention is given to
the data that are being used by these functions.
• There are two kinds of data :-Local data and Global data
1. Local Data:- It has restricted access in one function only .It is hidden inside a
function and can be used only within that function and it is safe from
modification by other functions.
2. Global Data :- When more than one function want to access the same data,
then the Global data is used. Global data can be accessed by any function in the
program.
• In a large Program , it is very difficult to identify what data is used by which
function.
• Also it makes the program difficult to modify .When some global data is
changed ,it may be necessary to modify all the functions that access this data .
• The scenario is depicted in Fig. :
• Fig. : Data access in procedural programming languages
Global data accessible
by any function

Function-A Function-B

Local data Local data


accessible accessible
only by only by
function-A function-B

• There is one more problem with procedural programming that it does a poor modeling of the things in the real
world because of it’s arrangement of separate functions and data.
• Real world objects have both attributes and behavior. Attributes like weight ,height, name etc. and behaviour
like eat,do,run,take ,stop etc. So neither data nor functions model real-world objects effectively.
• Characteristics of Procedural Oriented Programming:-
1. OOP’s focus is on the functions.
2. Procedural programming languages uses top-down programming
approach in program design,i.e. the main task is divided into subtasks
and functions are written to perform each subtask. Then what data is
needed is decided, i.e. emphasis is on algorithms instead of data.
3. Different functions share the data through global variables.
4. Data moves openly around the system from function to function
because of global data are shared.
5. The major trace is given on the algorithms(doing things).
6. Functions transform data from one form to another form.
7. Large programs are divided into smaller programs known as functions,
(modules).
• For example, in Figure Payroll system is explained.
Characteristics of procedure-oriented programming language:
• It emphasis on algorithm (doing this ). Payroll menu program is the main program .
• Large programs are divided into smaller programs known as Emp_detail, other detail are subprogramming, Add
view ,Delete,Modify,HRA,DA,CLA are different
functions.
• Function can communicate by global variable. functions.
• Data move freely from one function to another function.
• Functions change the value of data at any time from any
place. (Functions transform data from one form to another.)
• It uses top-down programming approach.
• Memory Management
C++ supports dynamic memory allocation. You can free the allocated memory at any time. Not only this C++ also provides
dynamic memory management techniques.
• Powerful & Fast
C++ is a fast language as compilation and execution time is less. Also, it has a wide variety of data types, functions & operators.
• Compiler based
C++ is a compiler-based programming language. Without compilation, no C++ program can be executed. The compiler first
compiles the C++ program and then it is executed.

Sr. No. Key OOP POP


POP stands for
OOP stands for
Procedural
1 Definition Object-Oriented
Oriented
Programming.
Programming.
OOP follows a
POP follows a top-
2 Approach bottom-up
down approach.
approach.
A program is A program is
divided into divided into
3 Division
objects and their functions and they
interactions. interact.
Inheritance Inheritance is Inheritance is not
4
supported supported. supported.
Access control is No access
5 Access control supported via modifiers are
access modifiers. supported.
No data hiding
• Drawbacks of Procedure Oriented Programming:-
1. We can modify data of any function from any other function.
2. Data can be accessed from any part of a program i.e. all
data is visible in the program.
• Object Oriented Programming :-
• Object oriented programming is an approach that provides a way of modularizing
programs by creating partitioned memory area for both data and functions that can be
used as templates for creating copies of such modules on demand.
• In OOP approach, the main focus is on the data that is to be used rather than the
function.
• Once the data , which is to be used , is decided then the different functions that will
operate on this data are defined .
• Many Drawbacks of procedural languages are eliminated by object oriented languages.
The object oriented languages combine’s data and functions that operate on that
data ,into a single unit called as object. This helps to achieve data encapsulation and
data hiding.
• An object encapsulates the data and it’s operations, thus hides the data from remaining
program. Member functions allow other part of the program to access the data
• The organization of data and function in object oriented program is
shown in Fig. :-
Object-1
Data

Member
Function

Object-2 Objetct-3
Data Data

Member Member
functions functions
Communication
• Characteristics of Object Oriented Programming :-
1. It uses bottom-up programming approach for design.
2. Object oriented programming emphasis is on data rather than
procedure .
3. Data and it’s operations are bound together into objects.
4. Data is hidden from the remaining part of the program.
5. Objects can communicate with each other through message
passing.
6. Data can be organized in a hierarchical structure using inheritance.
7. New data and functions can be easily added wherever necessary.
8. Data structures are designed such that they characterize the
objects.
Object Oriented programming Vs Procedure Oriented
programming:-
Sr. Object Oriented Programming (OOP) Procedure Oriented Programming (POP)
No.
1. Object oriented programming language is object Procedure Oriented Programming languages tend
oriented to be action oriented.
2. In OOP, the unit of programming is the class. In procedural programming , unit of programming is
the function.
3. OOP programmers concentrate on creating their In procedure Oriented programming programmers
own user-defined types called classes and concentrate on writing functions. Groups of actions
components. Each class contains data as well as that perform some common task are formed into
the set of functions that manipulate that data. functions and functions are grouped to form
The data components of a class are called data programs.
members .The function components of a class are
called member function. The instance of a class is
called an object.
4. OOP trace on data. POP trace on procedures.
5. Follow bottom-up approach in program design. Follow top-down approach in program design.

6. Basic building block of OOP is object. Basic building block of POP is function.
Sr. OOP POP
No.
7. OOP has access specifiers named Public, Private, POP does not have any access specifiers.
Protected etc.
8. Ex. of OOP are C++, JAVA, VB.NET, C#.NET. Ex. of POP are C, VB, FORTRAN, Pascal.
9. In OOP, importance is given to the data rather than In POP, importance is not given to data but to functions as
procedures or functions because it works as a real well as sequence of actions to be done.
world.
10. OOP provide Data Hiding so provides more security. POP does not have any proper way for hiding data so it is
less secure.
11. In OOP, overloading is possible in the form of In POP, Overloading is not possible.
Function Overloading and Operator Overloading.
12. In OOP, objects can move and communicate with In POP, data can move freely from function to function in
each other through member functions. the system.
S.no. On the basis of Procedural Programming Object-oriented programming
1 Definition It is a programming language that is derived from Object-oriented programming is a computer programming
structure programming and based upon the concept of design philosophy or methodology that organizes/ models
calling procedures. It follows a step-by-step approach in software design around data or objects rather than functions
order to break down a task into a set of variables and and logic.
routines via a sequence of instructions.

2. Security It is less secure than OOPs. Data hiding is possible in object-oriented programming due to
abstraction. So, it is more secure than procedural
programming.

3. Approach It follows a top-down approach. It follows a bottom-up approach.


4. Data movement In procedural programming, data moves freely within the In OOP, objects can move and communicate with each other
system from one function to another. via member functions.
5. Orientation It is structure/procedure-oriented. It is object-oriented.
6. Access modifiers There are no access modifiers in procedural The access modifiers in OOP are named as private, public, and
programming. protected.
7. Inheritance Procedural programming does not have the concept of There is a feature of inheritance in object-oriented
inheritance. programming.
8. Code reusability There is no code reusability present in procedural It offers code reusability by using the feature of inheritance.
programming.
9. Overloading Overloading is not possible in procedural programming. In OOP, there is a concept of function overloading and
operator overloading.
10. Importance It gives importance to functions over data It gives importance to data over functions.
11. Complex problems It is not appropriate for complex problems. It is appropriate for complex problems.
12. Data hiding There is not any proper way for data hiding. There is a possibility of data hiding.
13. Program division In Procedural programming, a program is divided into In OOP, a program is divided into small parts that are referred
small programs that are referred to as functions. to as objects.
14. Examples C, Fortran, Pascal, and VB. .NET, C#, Python, Java, VB.NET, and C++.
Basic Anatomy Of a C++
Program :-
• A Computer program is a sequence of instructions that tell the computer
what to do..
• A typical C++ program would contain four sections :
1. Include file(Header file).
2. Class declaration.
3. Member function definition.
4. Main function.
• C++ Program is a collection of functions.
Fig. Shows structure of a C++
program :-
• #include<iostream.h>
class test
{-------
}
Include files Function1()
Class declaration {-------
Member function definition
}
Main function
Function2()
{---------
}
int main()
{--------
}
• Simple “Hello World” Program.
1. #include <iostream.h>
2. using namespace std;
3. int main()
4. {
5. cout << “ Hello World !” << endl;
6. return 0;
7. }
• Output :
Hello World!
1. Comments :-
• Comments are parts of the source code disregarded by the compiler. They simply do
nothing.
• Their purpose is only to allow the programmer to insert notes or description
embedded within the source code.
• C++ supports two ways to insert comments :
i. // line comment and
ii. /* */ Block comment.
Program for comments
#include<iostream.h >
Using namespace std;
int main()
{
cout << “Hello World !” << endl; //Prints Hello World
cout << “Welcome to C++ !” << endl; //Prints Welcome to C++!
return 0;
• Output :-
Hello World!
Welcome to C++ !
Press any key to continue….
2. Header Files :-
• Each standard library has a corresponding header file containing the
function prototypes for all the functions in that library and definitions
of various data types and constants needed by those functions.
• Some common C++ header files that may be include in Standard C++
program, are given below :
• Table : Standard Library Header Files :-
Standard Library Explanation
header files
<assert.h > Contains macros and information for adding diagnostics that aid program debugging.
<ctype.h> Contains function prototypes for functions that test characters for certain properties and that can
be used to covert because lowercase letter to uppercase letters.
<float.h> Contains the floating point size limits of one program.
<limits.h > Contains integral size limits of the system.
<math.h> Contains function prototype for math library function.
<stdio.h> Contains function prototype for the standard input/output library function and information used by
them.
<stdlib.h> Contains function prototype for conversion of numbers to text ,text to numbers, memory
allocation , random numbers and various other utility functions.
<string.h> Contains function prototype for C-style string processing function.
<time.h> Contains function prototype for manipulating time and date.
<iostream.h> Contains function prototype for standard input and output functions.
<iomanip.h> Contains function prototype for the stream manipulators that enables formatting of streams ofdata.
<fstream.h > Contains function prototype for functions that perform input from files on disk and output to files
on disk
• The programmers can create custom header files. Programmer-defined
files should end in .h, which can be included by using the # include
preprocessor directive.
• For example, the header file square.h can be included in our program by
the directive, #include “square.h” at the top of the program.
3. Streams in C++ :-
• C++ support a set of functions for performing input and output operations.
The new features of C++ is called streams which is used to handle I/O
operations.
• C++ streams are of two types :-
i. Input stream and
ii. Output stream
• Stream refers to the flow of data from a particular source to a specified
destination.
• There are classes representing each stream istream is the class
representing the input stream (istream) and ostream is the class
representing the output stream.
• To achieve the console input/output operations, we use the objects of
these stream classes.
i. Input Stream :-
• The input stream is handling all input operations or reading
operations with input devices like keyboard, disk etc. “cin” is the
predefined object of the istream class. It is used for all console input
operations.
• Operator >>,(or the right shift operator in C) is called the extraction
operator, it gets the value from the stream object cin on its left and
places it in the variable on its right.
• Syntax :- cin>>variable;
• The use of cin is shown in Fig. :-
Extraction or get Variable of standard or
operator object User defined data type
cin >>variable

C++ cin >> name

Variable
insertion

• Example:
1. int a;
cin>>a;
2. float salary;
cin>>salary;
3. double area;
cin>>area;
4. char name[15];
cin>>name;
• We can input more than one item using cin object .Such input
operations are called cascaded input operations.
• cin will read read all the items from left to right.
• Syntax for more than one variable as:
• cin>>var1>>var2>>var3………>>varn;
• Examples :
• cin>>x>>y; //x read first then y
• cin>>name>>address;
• cin>>roll>>name>>marks;
ii) Output Stream :-
• The output stream handles the write operations on output devices like
screen ,disk etc. cout is the predefined object of the stream class and is
used for all console output operations.
• Operator <<, (or the left shift operator in C) is called insertion operator. It
directs the contents of the variable on its right to the object (cout) on its
left.
• Syntax :- cout<<variable;
• The use of cout is shown in Fig. :-
insertion

C++ cout << “C++”

Variable
• Examples:-
1. int a;
cout<<a;
2. float salary;
cout<<salary;
3. char name[15];
cout<<name;
• We can display more than one outputs called cascaded outputs.
• Syntax:
cout<<var1<<var2……<<varn;
• Examples:
1. cout<<“x=”<<x;
2. cout<<roll<<“ ”<<name;
3. cout<<roll<<“\t”<<name;
4. cout<<name<<endl;
Compiling,Linking and Running A C++
Program :-
• A C++ compiler is itself a computer program which only job is convert
the C++ program from our form to a form the computer can read and
execute.
• The original C++ program program is called the “source code” and the
resulting compiled code produced by the compiler is usually called an
“object file”.
• Before compilation the preprocessor performs preliminary operations
on C++ sources files. Preprocessed form of the source code is sent to
compiler.
• After compilation stage object files are combined with predefined
libraries by a linker, sometimes called a binder, to produce the final
complete file that can be executed by the computer.
• A library is a collection of pre-compiled “object-code” that provides
operations that are done repeatedly by many computer programs.
• Linking is the process of taking all the object files for a program and
combining them into a single executable.
• The compilation and linking process of a C++ program is shown in Fig.:
Applications of C++:
C++ finds varied usage in applications such as:

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

• The compilation of a C++ program consists of three steps −


Preprocessing − In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-
processing before the actual compilation. It handles preprocessing directives like #include, #define, etc.
Compilation − The compilation takes place on the preprocessed files. The compiler parses the pure C++ source code and converts
it into assembly code. This in turn calls the assembler that converts the assembly code to machine code(binary) as Object files.
These Object files can refer to symbols that are not defined. The compiler won't give an error unless the source code is not well-
formed. Syntax errors, failed overload resolution errors, etc occur in this step. Also note these object files can be used as static
libraries as well.
Linking − The linker is produces the final compilation output from the object files the compiler produced. This output can be a
shared (or dynamic) library or an executable. It links the object files by replacing the undefined references with the correct
addresses. These symbols should be defined in other object files or in the libraries. If they are defined in libraries other than the
standard library, you need to explicitly pass them to the compiler as an argument so that they can be found and linked.
• Compilation and Linking Process of C++ :-
Source code

PRE-PROCESSOR

Expanded Source code

COMPILER

Yes Is any
error
?
No
Object code Library files

LINKER

Executable code
The syntax is as follows C++ compiler: Create an MSDOS Window:
g++ -g Programname.cpp Click on START then PROGRAMS then the MS-DOS Command
Prompt
[If when you start the MSDOS window you do not find yourself
This will compile C++ source code & a.out is created which is in your home directory, then enter the command G:]
executable file. Edit the program, say hmw.cpp, using the command
NOTEPAD hmw.cpp
Compile the program with the command
CL hmw.cpp
To run the program as follows If there are error messages, edit the program again with
./a.out NOTEPAD and recompile.

Run the program with the command


hmw.exe
https://youtu.be/AA85xgK5WDw If the program does not run correctly, edit it with NOTEPAD, .. ,
recompile, ..

You might also like