Вы находитесь на странице: 1из 41

Programming in C++

Introduction
About C++
 C++ is based on the C language, and it was developed in
early 1980's by Bjarne Stroustrup at AT&T Bell Laboratories,
Here "++" use for the extension because "++" is a syntactic
construct used in C to increment a variable. Most of the C++
content is the super-set of "C", Due to this extension most C
programs can be compiled using a C++ compiler.

A C++ program is a collection of commands, which tell the


computer to do "something." This collection of commands is
usually called C++ source code
 C++ is the Mid-Level programming language because it
acquires the feature of Low level as well as high-level
programming language.
 C++ is an object-oriented programming language and is
considered to be an extension of C
Programming paradigms
Programming is nothing but a process of
organizing data and instructions according
to given problem.
Programming principle defnes set of rules
and regulations for the organization of
data and instructions
Programming elements are
◦ Data
◦ instruction
To write a program to required Data and
instructions.
Any programming languages are
implemented for achieving the following
 Readability
Paradigm: Set of instructions or collection
of instructions and data.
Monolithic programming
Process oriented programming
Modular oriented programming
Structure oriented programming
Object oriented programming
Monolithic programming
The data and instructions are organized in
sequential manner called Monolithic
programming
A sequence is set of instructions used to solve a
given problem. (Assembly level language, Basic)
Program execution is controlled by using a goto
statement
This type of program are also called as
Unstructured programming
Disadvantages
◦ Code redundancy: the same operation has to be more
than one time, the same sequence has to copy at some
places.
◦ Size of program is incremental
◦ Efciency of the program is decreased
◦ subprograms does not exist
Process oriented programming
 COBOL and Pascal are called as procedural oriented
programming languages.
 In this approach instruction organized according to their
operations by dividing into small programs or small pieces
called the subroutine.
 A subroutine is a small program within a program
 This subroutine can be procedure or function
Advantages
◦ Modularity: Dividing a program into small pieces
or also called as divide and conqueror,
 According to their operations into small
modular.
◦ Re-usability: write code once and use more than
one time.
◦ Readability:  Easy to read or understand.
Disadvantages
◦ Data is global or local
◦ Its emphasis on instructions, but not on data.
Modular programming
In modular programming, a module is a
programming
In this approach application contains one or more
module
Each program is divided into a subroutine.
A program which contains a main is called an
executable module which is compiled as .ex
A program which has doesn’t have main is called
reusable module which is compiled
as .LIB(Library)
Structured programming
C’ and Pascal are called structured programming
language
Characters of structured programming
◦ Modular programming
◦ User defned data types (Pascal)
◦ scoped variables (local & Global)
◦ One subroutine communicates with another by
passing values
◦ Top-down approach
◦ Each procedure has to perform diferent tasks.
◦ Each module has its own set of local variables
and program code
Control Structure
Sequential Structure
Selection Structure
Loop Structure
Advantages
◦Emphasis is laid on the algorithms.
◦Large and complex programs are divided
into functions.
◦Generally functions share global data.
◦Data is passed from function to function.
◦Top–down approach is used in program
design.
Disadvantages
◦ Data is global and global data does not have
security
◦ Unrelated operation access global data, if
leads and logical errors.
◦ Debugging application is complex
◦ In a large program identifying which
operation operates what data becomes
Disadvantages of conventional programming
Traditional programming languages such as COBOL,
FORTRAN etc. are commonly known as procedure
oriented languages.
These languages consists of a sequence of instructions
that tells the compiler or interpreter to perform a given
task.
When program code is large then it becomes
inconvenient to manage it. To overcome this problem,
procedures or subroutines were adopted to make a
program more understandable to the programmers.
A program is divided into many functions. Each function
can call another function. Each function has its own
task.
Important data variables are declared as global
A programs containing several functions, every function
can access the global data as simulated/modify.
The drawbacks observed in monolithic,
procedural, and structured programming
languages:
◦ Complexity increases as the programs grow larger and
complex.

◦ Maximum focus is on the development of the function


and very little importance is given to the data itself.

◦ Since many functions access the same data, it is


difcult to fnd out which data is being used by which
function. In order to make changes in the external data,
all functions using these data will have to be revised.

◦ Global data could also get corrupted by a function.

◦ Procedural programs do not model real world problems


as close to user’s perspective as possible.
The Object Oriented Technology
 Nature is composed of various objects. Living beings can be
categorized with diferent objects.
 Object-oriented programming language is a feature that
allows a mode of modularizing programs by forming
separate memory area for data as well as functions that is
used as object for making copies of modules as per
requirement.
 Let us consider an example of a teaching institute having
two diferent working sections. They are teaching and non-
teaching. Further sub-grouping of teaching and non-teaching
can be made for the ease of management. The various
departments of any organization can be thought as objects
working with certain goals and objectives.
 An institute is divided into diferent departments such as
library, classroom, computer laboratory, etc. Each
department performs its own activities in association with
other departments. This theory of objects can be extended
to every walk of life and can be implemented with the help
of software. In general, objects are in terms of entities.
 OOP has data as a critical component in the program
development.
 It does not let the data fow freely around the systems.
 It ties data more frmly to the functions that operate on it
and prevents it from accidental change due to external
functions.
 OOP permits us to analyze a problem into a number of
items known as objects and then assembles data and
functions around these items
 OOP is to treat data and the program as individual objects.
 OOP pays more importance to data than to function.
 Programs are divided into classes and their member
functions.
 New data items and functions can be comfortably added
whenever essential.
 Data is private and prevented from accessing external
functions.
 Objects can communicate with each other through
functions.?
Object Oriented Programming
Object-Oriented Programming (OOP) is the term
used to describe a programming approach based
on objects and classes.
The object–oriented approach is to combine both data
and the functions that operate on that data, into a single
unit called an object.
OOPS Concepts
Classes
Objects
Data abstraction
Encapsulation and Data hiding
Inheritance
Polymorphism
Dynamic binding
Message Communication
Creating new data types
Re-usability
Class and Object
A class is a collection of objects of similar type. It having
identical properties, common behavior, and shared
relationship.
◦ Example: mango, apple and orange are members of the class
fruit.
 Classes
are user-defned data type and built-in type of a
programming language.
◦ fruit mango: will create an object mango belonging to the class
fruit

 Object are the basic run time entity in an object –oriented


system.
 They may represent a person, a place, a bank account, a
table of data or any item that program has handle.
 An object is a specifc item that belongs to a class ; it is
called an instance of a class.
 An object is a specimen of a class
Class

Object
Data abstraction
 Defnition- The act of representing essential features
without including the background details or explanations.
 Data abstraction defnes an abstract data type (ADT) by
designing a class.
 Classes uses the concept of abstraction and are defned list
of abstract attributes.
 The attributes are sometime called as data members
because they hold information.
 The function that operates on these data are sometimes
called methods or member function.
 Therefore data abstraction is:
Essential features of an object.
Distinguishes it from other kinds of objects.
Provides well–defned conceptual boundaries.
Encapsulation and Data hiding
 Defnition :- The wrapping up of data and functions into a
single unit.
 Data encapsulation is the most striking feature of class.
 Encapsulation means that some or all of an object's internal
structure is "hidden" from the outside world.
 The data is accessible only through the functions. Otherwise
it remains hidden from users. This insulation of data from
direct access by the program is called data hiding or
information hiding
 Class Method: An operation required for an object or entity
when coded in a class is called a method.
Inheritance
 Inheritance is a process by which objects inherit the
properties of another object belonging to another class
 You can create new classes that extend or are descendent's
of existing classes. The descendant or derived classes can
inherit all the attributes of the original (or parent) class or
they can override some attributes
Polymorphism
A Greek term, means the ability to take more than on
form. An operation may exhibit diferent behavior is
diferent instances. The behavior depends upon the types
of data used in the operation.
 It is the ability of objects of diferent types to respond
to functions of the same name.
 Polymorphism in C++ is accomplished with the help of
Virtual functions.
Dynamic binding
 Binding refers to the process of resolving the function to
be associated with the function call. There are two types
of binding:
Static binding
Dynamic binding
 Static Binding is the binding that takes place during
compilation. It is also known as early binding or compile-
time binding.
 Dynamic Binding or late binding means that the code
associated with a given function call is not known until
run–time. Since the function is linked with a class much
later after compilation, it is known as late binding or run-
time binding.
Message Communication
 An object–oriented program contains a set of objects which
can communicate with each other.
 The steps involved in writing an object–oriented program
are:
creating classes
creating objects from the classes defned and
Establishing communication between objects.
 Objects communicate with each other by sending and
receiving messages.
 A message in nothing but execution of a procedure.
Message communication involves specifying the name of
the object, name of the function and the information to be
sent.
Creating new data types and
Re-usability
 One of the benefts of objects is that they give the
programmer a convenient way to construct new data types.
 Object-oriented technology allows re-usability of the
classes by extending them to other classes using
inheritance.
 Once a class is created and tested and ready to use, it can
be distributed to other programmers for using in their own
programs. This is known as re-usability
ADVANTAGES OF OOP's
l
Object-oriented programs can be comfortably upgraded.

Using inheritance, redundant program codes can be eliminated
and the use of previously defned classes may be continued.

The technology of data hiding facilitates the programmer to
design and develop safe programs that do not disturb code in
other parts of the program.

Data hiding helps programmers with data security.

The encapsulation feature provided by OOP languages allows
programmer to defne the class with many functions and
characteristics and only few functions are exposed to the user.

All object-oriented programming languages can create extended
and reusable parts of programs.

Object-oriented programming enhances the thought process of a
programmer leading to rapid development of new software in
short span of time.

The standard Template Library (STL) contains standard functions
for linked lists, queues, stacks etc. making programming really
easy. It also shortens the programs and reduces compilation
errors.
CHARACTERISTICS OF OOP
l
Objects with which you are working are analyzed—both the
attributes of those objects and the tasks that need to be
performed with and on those objects.

Emphasis is laid on data.

Programs are divided into objects.

Objects contain data and the functions that operate on the
data.

Data is hidden and cannot be accessed by outside functions.

Objects are allowed to communicate with each other through
functions. Messages are passed to objects, requesting the
objects to take action.

The same message works diferently (and appropriately) when
applied to the various objects.

A method can work appropriately with diferent types of data
it receives, without the need for separate method names.

Objects can assume or inherit properties of previously created
objects, thereby reducing the time it takes to create new
objects.

Bottom-up approach is used in program design.

Information hiding is more complete than in procedural
Diferences Between C And C++
Characteristics C C++

Type of programming Procedural language Object-Oriented programming language.

Programming Approach Top-down approach Bottom-up approach

Application development Good for embedded devices, system-level coding Good for networking, server-side applications,
etc. gaming, etc.
File Extension .c .cpp

Compatibility with each other Not Compatible with C++. Compatible with C as C++ is a subset of C.

Compatibility with other languages Not compatible Compatible

Ease of coding Allows us to code everything. Comes with highly advanced Object-Oriented
concepts.
Data Security Negligible High

Program division Program divided into functions. Program divided into classes and objects.

Standard I/O operations scanf/printf cin/cout

Focus/emphasis Emphasizes on functions and/or processes. Emphasizes on data rather than functions.

The main() function Can call main through other functions. Not possible to call main from any point.

Variables To be declared at the beginning of the function. Can be declared anywhere in the program.
Global variables Multiple declarations No multiple declarations.

Reference Variables and pointers Only Pointers Both

Enumerations Only integer types. Distinct type

Strings Supports only char[] Supports string class which is immutable.

Inline function Not supported Supported

Default arguments Not supported Supported

Structures Cannot have functions as structure members. Can have functions as structure members.

Classes and Objects Not supported Supported

Data Types Only built-in and primitive data types are supported. Boolean and string types supported in addition
No Boolean and string types. to built-in data types.

Function overloading Not supported Supported

Inheritance Not supported Supported

Functions Does not support functions with default Supports functions with default arrangements.
arrangements.
Namespace Not supported Supported

Source code Free-format Originally taken from C plus object-oriented.

Abstraction Not present Present

Information hiding Not supported Supported


Encapsulation Not supported Supported

Polymorphism Not supported Supported

Virtual function Not supported Supported

GUI programming Using the Gtk tool. Using the Qt tools.

Mapping Cannot easily map data and functions. Data and functions can be easily mapped.

Memory management Malloc(), calloc(), free() functions. New() and delete() operators.

Default headers Stdio.h iostream header

Exception/error handling No direct support. Supported

Keywords Supports 32 keywords. Supports 52 keywords.

Templates Not supported Supported


Structure of C++ Program

A Programming solution should be...

Small

Fast

Correct

Readable

Maintainable

C++ Programm style be....

Procedural programming

Data Abstraction

OOP

Generic programming

Functional programming

C++ is not restricted to any programming
style

Вам также может понравиться