0% found this document useful (0 votes)
14 views16 pages

C++ - Unit-1

This document discusses the differences between procedural programming languages like C and object-oriented programming languages like C++. It notes that C++ was created to overcome flaws in procedural languages by introducing object-oriented concepts like encapsulation, inheritance, and polymorphism. These concepts allow programs to be divided into objects rather than functions, make data more secure by hiding it within objects, and enable modeling of real-world problems more easily. The document provides examples of key differences between C and C++, as well as advantages of object-oriented programming over procedural programming.

Uploaded by

Ganesh Babu
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)
14 views16 pages

C++ - Unit-1

This document discusses the differences between procedural programming languages like C and object-oriented programming languages like C++. It notes that C++ was created to overcome flaws in procedural languages by introducing object-oriented concepts like encapsulation, inheritance, and polymorphism. These concepts allow programs to be divided into objects rather than functions, make data more secure by hiding it within objects, and enable modeling of real-world problems more easily. The document provides examples of key differences between C and C++, as well as advantages of object-oriented programming over procedural programming.

Uploaded by

Ganesh Babu
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/ 16

UNIT-1

Why Object Oriented Programming Concept is introduced


 C is a function oriented programming language.
 In function oriented programming language the total program is divided into
small pieces are called functions.
 In C-Language data is not secured. Because data is freely moved between the
functions.
 In C-Language it is difficult to develop secured applications because data is
visible to outside functions.
 In C-Language it is difficult to develop real world applications.
 In large program it is very difficult to identify what data is used by which
function.
Due to the above difficulties Object Oriented Programming concept
(OOP) is introduced to overcome flaws in POP.

Difference between C and C++

SNO C C++

1 C was developed in 1969 at C++ was developed in 1979 by


AT&T Bell laboratories by Bjarne Stroustrup
Dennis Ritchie
2 C uses top-down approach C++ uses the bottom-up approach

3 C is a subset of C++ language C++ is a superset of C

4 Procedural Programming is C++ is an object driven language


supported in C
5 Data is secured in C++
In C-Language data is not secured
Functions can be defined inside a
6 Functions cannot be defined inside the structure called class.
structure

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


7 scanf() is used for input, printf() Cin>> is used for input and cout<<
is used for output. is used for output.

8 Reference variables are not Reference variables are supported


supported
Virtual and friend functions are not Virtual and friend functions are
9 supported supported
malloc() and calloc() functions are New and delete operators are used for
10 used for DMA. memory allocation and de-allocation.
C does not support polymorphism,
encapsulation and inheritance C++ support polymorphism, encapsulation
11 concepts and inheritance concepts
Main() function can be called in the We cannot call main() function from other
12 declaration part only functions.
All variables must be defined in the
13 declaration part only We can declare variables anywhere in C++
Exception Handling is not supported
14 by C Exception Handling is supported by C++

Difference between POP and OOP

SNO POP OOP

1 Real world problems cannot be Real world problems can be solved


Solved using POP. Using OOPS concepts.
2 It deals with algorithms. It deals with data.
3
Programs are divided into functions. Programs are divided into objects.
Data and functions are separate and
4 free entities. Encapsulation hides the data which
can be used for information hiding.

5 It is top down approach It is bottom up approach

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


6 Function and operator Function and operator overloading
overloading is not supported is supported.
Namespace is allowed to avoid name
7 collisions.
Name space feature is not provided
8 It needs very less memory It needs more memory that POP
9 Example are C, Fortran Examples are C++, Java, .NET etc.

It has access specifies like private, public,


10 It does not have any access specifies protected.

11 It is less secure It is more secure

Evolution of C++
C++ is a middle-level programming language and 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.

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 ++. Therefore, C++ is an extended version of C. C++ is a
superset of C. All the concepts of C are applicable to C++ also.

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


Fig 1.1 Evolution of C++

Object Oriented Technology

Nature is composed of various objects. Living beings can be categorized with different objects.
Let us consider an example of a teaching institute having two different 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.

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
employees. The sports section is entrusted the responsibility of sports activities, the Registrar for
administration and his staff for dealing with administrative- matters of the Institute and so on are
required. Each department has an In-Charge who has clear-cut responsibilities. Every department
has its own work as stated above. When the work is distributed to different departments as
shown in Figure 1.2, it becomes easy to accomplish goals and objectives. The activities are
carried out smoothly. The burden of one particular department has to be shared among personnel.

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

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


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.

Fig. 1.2 Relationship between different sections

DISADVANTAGE OF CONVENTIONAL PROGRAMMING

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
Figure 1.4. Generally in a program of large size, it becomes difficult to understand the various
data used in different functions. As a result, a program may have several logical errors.
Following are the drawbacks observed procedural and structured

programming languages:
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.

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


Fig. 1.3 Flow of functions in non-OOP languages

Fig. 1.4 sharing of data by functions in non-OOP languages

Concepts of Object Oriented Programming/ Key Concepts of OOP

There are several fundamental or key concepts in object-oriented programming.

(1) Objects:
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.
M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT
Fig. 1.9 Features of object-oriented programming

Given below are some objects that we use in our daily life.

Fig. 1.10 Commonly available 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 thatact 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 objectcan be built as a user-defined data type using class.

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


Fig. 1.11 Objects and their properties

In Figure 1.11Some common objects, their properties, and the actions they perform have
been described. A car can be distinguished from another car on the basis of its properties,
for example, company, model, color, capacity etc. and also on the basis of its actions for
example, speed, average, break etc.
 Class is a user defined data type
 Class is a complex data type
 It is a collection of data members and member functions
 Class is a container
 Class provides the concept of encapsulation
 Class provides the concept of data hiding with access specifies.
 Class is blueprint to construct objects
 It is a plan before constructing objects.
 Class is a logical copy to create objects
 No memory is allocated when a class is created.

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


Real Life Examples

 For example consider a class called vehicle under which car, truck and bus are represents
individual object.
 For example I B. Tech. (CSE) is a class and all sixty four students are objects of the above said class.
 For example consider a blueprint of a house considered as a class with the same blueprint, we can
create several houses and these can be considered as object.

Method
An operation required for an object or entity when coded in a class is called a method.
An operation required for an object or entity when coded in a class is called a method.
The operations that are required for an object are to be defined in a class. All objects in a
class perform certain common actions or operations. Each action needs an object that
becomes a function of the class that defines it and is referred to as a method.

Example:

Class A
{
Private:
data member1;
data member2; Members
data member(n);
public:
method1(){ }
method2(){ }
method_n(){ }
}
(3) Data Abstraction

Abstraction refers 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.

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


Real Life Examples

 Yahoo mail…
When we provide the username and password to yahoo mail and click on the submit
button. It will show compose, inbox, outbox, sent mails… so when we click on compose
it will open…but user doesn’t know what are the actions performed internally… It just
opens… That is essential; user doesn’t know internal actions… that are non-essential
thing.
 TV Remote
Remote is an interface between end user and TV. Which has buttons like 0 to 9, ON/OFF
etc. but we doesn’t know circuits inside the remote… user does not need to know. Just
end user is using an essential thing that is remote.

(4) Data Hiding

It means protecting the members of a class from an illegal or unauthorized access. The
mechanism is used to hide data is to put in a class and make it private.

Private and public are two types of protection available within a class. Items marked with
“Private” can only be accessed by methods defined as part of the class. Data is most often defined
as private.

Private members are can be accessed by members and objects of the class.

Protected is another type of protection available within a class. Items declared as protected are
private within a class and are available for private access in the derived class.

The main advantage of data hiding is we can achieve security by using private modifier.

Real Life Examples

 If we go to a bank and access the staff rather than the computers and files over there. The
bank staff knows how to access those computers and files.

SNO Data Abstraction Data Hiding

1 Abstraction is hiding the It is hiding the data which is being affected by that
implementation details by providing implementation. Use of private and public under
a layer over the basic functionality. this feature.
2 It is an act of representing only the It is a concept that can be used for hiding data from
essential things without including external world and this can be achieved using
background details. encapsulation and abstraction.
3 Customers are dealing with the end Let us take an example cooking of a dish in hotel.
quality of the dish and not how it is Chefs who are master in making a particular dish
made. So chef never tells us how did most of the time have their secret ingredients.
they managed to make it. So here These ingredients are something that they do not
comes data abstraction. want to reveal. There comes data hiding.

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


(5) Encapsulation (Data abstraction + Data hiding)
The packing of data and functions into a single component is known as encapsulation.
C++ supports the features of encapsulation using classes. The packing of data and functions into
a single component is known as encapsulation. The data is not accessible by outside functions.
Only those functions that are able to access the data are defined within the class. These functions
prepare the interface between the object's data and the program.

Fig. 1.15 Encapsulation: Private and Public sections

Real Life Examples

 A Car is having multiple parts… like steering, wheels, engine etc... Which binds together
to form a single object that is car. So here multiple parts of car encapsulate it together to
form a single object that is car.

(6) Inheritance

Inheritance is the method by which objects of one class get the properties of objects of another
class. Inheritance is the method by which objects of one class get the properties of objects of
another class. In object-oriented programming, inheritance provides the thought of reusability.
The programmer can add new properties to the existing class without changing it is called
extendibility. This can be achieved by deriving a new class from the existing one.

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


(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.17 Polymorphism in OOP


(8) Dynamic Binding

Binding means connecting one program to another program that is to be executed in reply to the
call. Binding means connecting one program to another program that is to be executed in reply to
the call. Dynamic binding is also known as late binding. The code present in the specified
program is unknown till it is executed. It is analogous to inheritance and polymorphism.
(9) Message Passing

Object-oriented programming includes objects which communicate with each other. Data is
transferred from one object to another. A message for an object is the demand for
implementation of the process.
Programming with these objects should be followed in steps shown below:
(1) Declaring classes that define objects and their actions.
(2) Declaring objects from classes.
(3) Implementing relation between objects.

Fig. 1.18 Message passing

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


(11) Reusability

Object-oriented technology allows reusability of the classes by extending them to other classes
using inheritance. Object-oriented technology allows reusability of classes by extending them to
other classes using inheritance. Once a class is defined, the other programmer can also use it in
their programs and add new features to the derived classes. Class A is reused and class B is created.
Again class B is reused and class C is created.

Reusability

(12) 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.

Delegation

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


(13) 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
canbe used for any type of data. The template feature in C++ allows generic programming.
Generic programs can be written once, compiled once and used for different data types.

Advantages of OOP

Object-oriented programming provides many advantages to the programmer and the user.
This technology solves many problems related to software development, provides improved
quality, and low cost software.

OOP has the following advantages:

1. Object-oriented programs can be comfortably upgraded.


2. Using inheritance, redundant program codes can be eliminated and the use of
previouslydefined classes may be continued.
3. The technology of data hiding facilitates the programmer to design and develop
safeprograms that do not disturb code in other parts of the program.
4. The encapsulation feature provided by OOP languages allows programmer to define the
classwith many functions and characteristics and only few functions are exposed to the
user.
5. All object-oriented programming languages can create extended and reusable
parts ofprograms.
6. Object-oriented programming enhances the thought process of a programmer leading to
rapiddevelopment of new software in short span of time.

Applications of OOP
OOP has become one of the programming buzzwords today. Applications of OOP are
beginning to gain importance in many areas. The most popular application of Object-oriented
programming, up to now, has been in the area of user interface design such as windows.
Hundreds of windowing systems have been developed using the OOP Techniques.

Real-business systems are often much more complex and contain many more objects with
complicated attitudes and methods. OOP is useful in these types of applications because it
can simplify a complex problem. The promising areas of applications of OOP include:

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


 Real-Time systems
 Simulation and modeling
 Object-oriented databases
 Hypertext and hypermedia
 AI and expert systems
 Neural Networks and Parallel Programming
 Decision Support and office automation systems
 CIM/CAM/CAD Systems.

The richness of OOP environment has enabled the software industry to improve not only
the quality of software systems but also its productivity. Object-oriented technology is
certainly changing the way the software engineers think, analyze, designed and implement
systems.

DATA TYPES:
A data type is used to indicate the type of data value stored in a variable. All C compilers support a
variety of data types. This variety of data types allows the programmer to select the type appropriate to
the needs of the application as well as the machine. ANSI C supports the following classes of data types:

1.Primary (fundamental) data types.


2. Derived data types.
3. User-defined data types C++ data types

Primary data types Derived data types User defined data types

int Array
Pointer Structur
char Reference eUnion
Class
float enumeration

double

bool

void

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT


Operators in c++: All operators of c language are also valid in c++.New operators introducedin
c++ are

Sno Operator Symbol


1. Scope resolution operator ::
2. Pointer to a member declarator ::*
3. Pointer to member operator ->*,->
4. Pointer to member operator .*
5. new Memory allocating operator
6. delete Memory release operator
7. endl Line feed operator
8. setw Field width operator
9. insertion <<
10. Extraction >>

M.Srinivasa Rao, Asst.Prof, Dept of CSE, VIIT

You might also like