CH 1- Introduction to Cpp
CH 1- Introduction to Cpp
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.
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:-
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
• 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.
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.
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
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:
PRE-PROCESSOR
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.