Escolar Documentos
Profissional Documentos
Cultura Documentos
INTRODUCTION:
C++ is an object-oriented programming language and is considered to be an extension of C.
Bjarne Stroustrup at AT&T Bell Laboratories, Murray Hill, New Jersey (USA) developed it in
the early eighties of twentieth century. Stroustrup, a master of Simula67 and C, wanted to
combine the features of both the languages into a more powerful language that could support
object-oriented programming with features of C.
The outcome was C++ as per Figure 1.1. Various ideas were derived from SIMULA67 and
ALGOL68. Stroustrup called the new language ‘C with classes’. However, in 1983, the name
was changed to C++. The thought of C++ came from the C increment operator ++. Rick Mascitti
coined the term C++ in 1983. Therefore, C++ is an extended version of C. C++ is a superset of
C. All the concepts of C are applicable to C++ also.
S.
C C++
No.
language. language.
any problem.
3 Functions are the fundamental building Objects are the fundamental building blocks.
blocks.
external functions.
6 C uses scanf() and printf() function for C++ uses cin>> and cout<< for standard
7 Variables must be defined at the beginning in Variables can be defined anywhere in the
Usually, an institute has faculties for different departments. Also, laboratory staff for the
assistance in conducting of practical’s and site development section for beautification of the
campus are essential. The Principal is a must for the overall management of the Institute. The
accounts department is also required for handling monetary transactions, and salaries of the
As shown in Figure 1.2 an institute is divided into different 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.
Traditional programming languages such as COBOL, FORTRAN etc. are commonly known as
procedure oriented languages. The program written in 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 as shown in
Figure1.3. Each function has its own task. If the program is too large the function also creates
problems. In many programs, important data variables are declared as global. In case of
programs containing several functions, every function can access the global data as simulated in
1. Large size programs are divided into smaller programs known as functions. These
functions
2. can call one another. Hence, security is not provided.
3. Importance is not given to security of data but on doing things.
4. Data passes globally from one function to another.
5. Most functions have access to global data.
PROGRAMMING PARADIGMS
1. Monolithic Programming
2. Procedural Programming (functions)
3. Modular Programming (namespaces)
4. Object Oriented Programming (classes)
5. Generic Programming (templates)
(1) Monolithic Programming:
1. In structured programming languages such as PASCAL and C larger programs are developed.
These programs are divided into multiple submodules and procedures.
2. Each procedure has to perform different tasks.
3. Each module has its own set of local variables and program code as shown in Figure 1.7.
Here, the different modules are shown accessing the global data.
4. User-defined data types are introduced.
(1) Objects:
Objects are primary run-time entities in an object-oriented programming.
Objects are primary run-time entities in an object-oriented programming. They may stand
for a thing that has specific application for example, a spot, a person, any data item
related to program, including user-defined data types. Programming issues are analyzed
in terms of object and the type of transmission between them. Figure 1.10 describes
various objects.
(2) Classes
A class is grouping of objects having identical properties, common behavior, and shared
relationship.
A class is the accomplishment of abstract data type. It defines the nature and methods that
act on the data structure and abstract data type respectively. Specimens are also known as
objects. In other words, a class is a grouping of objects having identical properties,
common behavior, and shared relationship. The entire group of data and code of an object
can be built as a user-defined data type using class.
Class A
{
Private
data member1;
data member2; Members
data member(n);
public
method1(){ }
method2(){ }
method_n(){ }
}
(4) Data Abstraction
Abstraction directs to the procedure of representing essential features without including the
background details.
Abstraction directs to the procedure of representing essential features without including the
background details. Classes use the theory of abstraction and are defined as a list of abstract
properties such as size, cost, height, and few functions to operate on these properties. Data
abstraction is the procedure of identifying properties and methods related to a specific entity as
applicable to the application.
(5) Encapsulation
(7) Polymorphism
Polymorphism allows the same function to act differently in different classes. Polymorphism
makes possible the same functions to act differently on different classes as shown in Figure 1.17.
It is an important feature of OOP concept and has the ability to take more than one form.
Polymorphism accomplishes an important part in allowing objects of different classes to share
the same external interface. It is possible to code a non-specific (generic) interface to a set of
associated actions.
Fig. 1.19 (a) Reusability (b)Relationships between two classes(kind of (inhiretance)and has a
(delegation) relationships)
(11) Delegation
In OOP, two classes can be joined either by - inheritance or delegation, which provide reusability
of the class. In inheritance, one class can be derived from other class and relationship between
them is known as kind of relationship. For example, if class Y is derived from class X, then class
Y is known as kind of X, as shown in Figure 1.19 (b).The second type of relationship is has a
relationship. When object of one class is used as data member in other class, such composition of
objects is known as delegation. As shown in Figure 1.19 (b) class C has two data members that
are objects of class A and B. Such a relationship between two classes is known as has a
relationship.
(12) Genericity
The software components of a program have more than one version depending on the data types
of arguments. This feature allows declaration of variables without specifying exact data type.
The compiler identifies the data type at run-time. The programmer can create a function that can
be used for any type of data. The template feature in C++ allows generic programming.
Lecture-7: ADVANTAGES OF OOP
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
Let us look various parts of the above program:
• The C++ language defines several headers, which contain information that is either
necessary or useful to your program. For this program, the header <iostream> is needed.
• The line using namespace std; tells the compiler to use the std namespace. Namespaces
are a relatively recent addition to C++.
• The next line // main() is where program execution begins. is a single-line comment
available in C++. Single-line comments begin with // and stop at the end of the line.
• The line int main() is the main function where program execution begins.
• The next line cout << "This is my first C++ program."; causes the message "This is my
first C++ program" to be displayed on the screen.
• The next line return 0; terminates main( )function and causes it to return the value 0 to
the calling process.
$ g++ hello.cpp
$ ./a.out
Hello World
Make sure that g++ is in your path and that you are running it in the directory containing file
hello.cpp.
C++ Standard Library header files
As programs grow larger and larger (and include more files), it becomes increasingly tedious to
have to forward declare every function you want to use that lives in a different file. Wouldn’t it
be nice if you could put all your declarations in one place?
C++ code files (with a .cpp extension) are not the only files commonly seen in C++ programs.
The other type of file is called a header file, sometimes known as an include file. Header files
usually have a .h extension, but you will sometimes see them with a .hpp extension or no
extension at all. The purpose of a header file is to hold declarations for other files to use
The interface of C++ standard library is defined by the following collection of header files.
Utilities library
General purpose utilities: program control, dynamic memory
<cstdlib>
allocation, random numbers, sort and search
<csetjmp> Macro (and function) that saves (and jumps) to an execution context
Numeric limits
<cinttypes> (since C++11) formatting macros , intmax_t and uintmax_t math and conversions
Error handling
Strings library
Containers library
Algorithms library
<execution_policy> (C++17) Predefined execution policies for parallel versions of the algorithms
Iterators library
Numerics library
Input/output library
Localization library
<regex> (since C++11) Classes, algorithms and iterators to support regular expression processing
Experimental libraries
Standard libraries extensions and Extensions for
<experimental/algorithm> (library fundamentals TS)
Parallelism
C compatibility headers
<ciso646> empty header. The macros that appear in iso646.h in C are keywords in C++
simply includes the headers <ccomplex> and <cmath>: the overloads equivalent to the
<ctgmath> (since C++11)
contents of the C headertgmath.h are already provided by <cmath> and <complex>
Deprecated headers
<assert.h> (deprecated) behaves as if each name from <cassert> is placed in global namespace
<complex.h> (deprecated) behaves as if each name from <ccomplex> is placed in global namespace
<ctype.h> (deprecated) behaves as if each name from <cctype> is placed in global namespace
<errno.h> (deprecated) behaves as if each name from <cerrno> is placed in global namespace
<fenv.h> (deprecated) behaves as if each name from <cfenv> is placed in global namespace
<float.h> (deprecated) behaves as if each name from <cfloat> is placed in global namespace
<inttypes.h> (deprecated) behaves as if each name from <cinttypes> is placed in global namespace
<iso646.h> (deprecated) behaves as if each name from <ciso646> is placed in global namespace
<limits.h> (deprecated) behaves as if each name from <climits> is placed in global namespace
<locale.h> (deprecated) behaves as if each name from <clocale> is placed in global namespace
<math.h> (deprecated) behaves as if each name from <cmath> is placed in global namespace
<setjmp.h> (deprecated) behaves as if each name from <csetjmp> is placed in global namespace
<signal.h> (deprecated) behaves as if each name from <csignal> is placed in global namespace
<stdalign.h> (deprecated) behaves as if each name from <cstdalign> is placed in global namespace
<stdarg.h> (deprecated) behaves as if each name from <cstdarg> is placed in global namespace
<stdbool.h> (deprecated) behaves as if each name from <cstdbool> is placed in global namespace
<stddef.h> (deprecated) behaves as if each name from <cstddef> is placed in global namespace
<stdio.h> (deprecated) behaves as if each name from <cstdio> is placed in global namespace
<stdlib.h> (deprecated) behaves as if each name from <cstdlib> is placed in global namespace
<string.h> (deprecated) behaves as if each name from <cstring> is placed in global namespace
<tgmath.h> (deprecated) behaves as if each name from <ctgmath> is placed in global namespace
<time.h> (deprecated) behaves as if each name from <ctime> is placed in global namespace
<uchar.h> (deprecated) behaves as if each name from <cuchar> is placed in global namespace
<wchar.h> (deprecated) behaves as if each name from <cwchar> is placed in global namespace
<wctype.h> (deprecated) behaves as if each name from <cwctype> is placed in global namespace