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

CPP-Unit 1-A4

The document provides an overview of procedural programming and object-oriented programming. It notes that procedural programming focuses on functions and treats data as secondary, while object-oriented programming focuses on data as the primary element and ties data closely to the functions that operate on it. The document then discusses some key characteristics of each paradigm such as functions vs objects, global vs encapsulated data, and representation of real-world problems.

Uploaded by

VOJE DIF
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CPP-Unit 1-A4

The document provides an overview of procedural programming and object-oriented programming. It notes that procedural programming focuses on functions and treats data as secondary, while object-oriented programming focuses on data as the primary element and ties data closely to the functions that operate on it. The document then discusses some key characteristics of each paradigm such as functions vs objects, global vs encapsulated data, and representation of real-world problems.

Uploaded by

VOJE DIF
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Page 1 of 7

Procedural Programming (or Structured Programming) vs. Object-oriented Programming

In 1970 and 1980, popular programming methodology was procedural programming. Procedural
programming methodology is based on following process –

1) Break down a large problem into several sub-parts


2) Treat each part as a new problem and work on it
3) Repeat the process with each new part until each part can be solved independently, without further
decomposition

Procedural programming is based around data structures and subroutines. The functions are where stuff
actually "happens", and the data structures are simply containers for the information needed by those
functions.

While we concentrate on functions, very less importance is given to the data which are being used various
functions. What happens to the data? How are they affected by functions which operate on them?

Further, in a multi-function program, many important data are stored as global variables so that they may
be accessed by all the functions. Global variables can be accidentally changed by functions. In a large
program it is difficult to identify & understand which functions use which data. This may result into bugs (i.e.
errors)

Another serious problem with procedural program is that it does not represent real-world problem. This is
so because procedural programs do not focus on real-world entities in the problem.

Some of the important characteristics of procedural programming are:

o Functionsare more important than data.


o Programs are divided into functions.
o Most of the functions share data thorough global variables.
o Functions transfer data from one to another.

Object oriented programming, on the other hand, shifts your primary focus to the data itself. Instead of
asking "what do I want to do and what will I need to know to do it", you ask "what kind of things do I want to
have and what can those things do for me".

Instead of designing your functions first and then coming up with data structures to support them, you
design types first and then come up with the operations needed to work with them.

OOP treats data as the most important element in the program and does not allow them to flow freely
around the system. It ties data more closely to the functions that operate on it, and protects it from
accidental modifications outside the functions.

OOP allows decomposition of a problem into a number of entities called objects and build data and
functions around these objects. The data of an object can be accessed only by functions associated with
that object.
Some of the important characteristics of OOP are:

o Data is more important than functions.


o Programs are divided into objects.
o Data structures are designed to represent entities in problem.
o Data and function which operate on the data are ties together.
o Data is kept hidden from other functions.
o Objects may communicate with each other through functions.
o New data and functions can be easily added whenever necessary.

Object-oriented programming is a programming methodology in which problem is solved in terms of real


world objects and their interaction.
Prepared by: Mr. Jay Nanavati
Page 2 of 7

Object-oriented Programming - Basic Concepts

Objects
o are basic runtime entities in OOP.
o may represent a student, an employee, a vehicle, a bank account etc.
o are variables of user-defined datatype-class.
o occupy space in memory, have life-time & scope.
o interact with each other through functions.

Classes
o are user-defined datatype.
o define attributes & behavior of objects.
o contain variable that may store value of attributes.
o also contain code to assign, modify or access the value of attributes.

Data Abstraction
o Data Abstraction refers to the act of representing main features without showing details.
o To work with objects, only object name & function name are required. It is not necessary to know &
understand how functions operate on data.

Encapsulation
o The wrapping up of data and functions together inside a class, is known as Encapsulation.
o Encapsulation ties data to functions which operate on the data.
o The data can not be directly accessed/modified by outside world and only those functions which are
wrapped inside a class can do so.

Inheritance
o Inheritance is the process by which objects of one class acquire the properties of another class.
o It supports hierarchical classification.
o In OOP, a new class is defined with the help of one or more existing classes.
o The new class will have its own features as well as some or all features of other classes.
o Features implemented by one class are re-used in other classes. Thus, inheritance supports
reusability.

Polymorphism
o Polymorphism is a Greek term meaning the ability to take more than one form.
o An operation may behave in different ways depending on the type of data used in the operation.
o Two types: Compile-time polymorphism and Run-time polymorphism
o Function overloading and operator overloading are example of compile-time polymorphism.
o Use of virtual function is an example of run-time polymorphism.

Dynamic Binding
o Binding means linking the function call to the code to be executed.
o Dynamic binding (also known as late binding) means the code to be executed is not known until the
execution time.
o It is associated with inheritance & polymorphism.
o Virtual function is used for implementing dynamic binding.

Introduction to C++
o C++ is an object-oriented programming language.
o It was developed by BjarneStroustrup at AT&T Bell Labs (USA) in early 1980s.
o Initial name was C with classe, later renamed to C++.
o Combines features of C and Simlula67.
o C++ is a superset of C.

Prepared by: Mr. Jay Nanavati


Page 3 of 7

Structure of a C++ program

// File inclusion using #include

// Constant declaration using #define or const - optional

// class definition - optional

// member functions defined outside the class - optional

// main()

As shown above a typical C++ program starts with inclusion of required header files. iostream.h and
conio.h are most frequently used.

Next, constant is declared using either #define pre-processor directive or using const keyword.

A class is defined just below constant declaration. Member functions of a class may be defined outside the
class. If a programmer decides to do so, member functions are defined after the class definition and before
main().

At last, main(), which is the starting point of the program execution, is defined.

Example: A program without class

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
cout<<”Hello World”;

getch();
}

A C++ program must be stored in a file with .CPP extension. Also, note that the default return-type of
main() in C++ is int, so either a return 0; statement must be present in main() or void should be used as the
return-type.

Prepared by: Mr. Jay Nanavati


Page 4 of 7

Basic datatypes

User-defined Datatypes
o struct and union are valid user-defined datatypes in C++ also.
o C++ allows creation of a class- which is also a user-defined datatype.
o More about classes is discussed later.

Derived Datatypes

Arrays
o Arrays are also supported in C++.
o The only exception is the way a character array is declared in C++; C++ requires that the size of the
array must be one more than the string assigned. e.g. char str[6] = “hello” ; is correct whereas char
str[5] = “hello” ; is incorrect.
Functions
o Functions in C++ are different than in C.
o You can define more than one functions with same name but different type and no. of arguments.
This concept is known as Function Overloading and is discussed later.
o It is optional to pass one or more arguments when calling a function.
o Functions can return address.
Pointers
o C++ introduces two types of pointers: (1) Constant pointer (2) Pointer to a constant

Symbolic Constants

o Symbolic constants can be declared using const keyword or using enum.


o A constant is declared as follows: const<datatype><name> = <value>;e.gconstint MAX = 5;
o Such a constant can be used in array declaration. e.g. int a[MAX};This is not allowed in C.
o C++ requires that a const must be initialized. It is not compulsory in C and a constant is initialized to
0.

Prepared by: Mr. Jay Nanavati


Page 5 of 7

o An enumeration can be defined as: enum {X, Y, Z} ;This defines X, Y & Z as integer constants with
value 0, 1 & 2 respectively..
o We can assign values to X, Y & Z explicitly. e.g. enum {X=100, Y=200, Z=300} ;

Variables
o In C all variables must be declared in the beginning of the scope. C++ allows declaration of variable
at the place of use.
o In C, a variable must be initialized using a constant value. However, C++ allows initialization of
variable at runtime. e.g the following statements are valid in C++:
int n = strlen(s1) ;
float area = 3.14 * radius * radius ;
o Thus, both declaration & initialization can be done simultaneously at the place where a variable is
used for the first time.

Reference Variables
o A reference variable provides an alias i.e. an alternate name for an existing variable.
o It can be declared as follows: <datatype>&<ref. variable name> = <variable name> ;
o e.g. float total = 100;
float&sum = total;
Here, total is a float variable that already exists.sum is an alternate name for total.
Both total and sum refer to the same variable in the memory.

cout<<total ; // Displays 100


cout<<sum ; // Displays 100

total = 200;

cout<<total ; // Displays 200


cout<<sum ; // Displays 200

sum = 300;

cout<<total ; // Displays 300


cout<<sum ; // Displays 300
o A reference variable must be initialized at the time of declaration.
o Reference variables are used in copy constructor and pass-by-reference.

Operators in C++
o All C operators are valid in C++.
o In addition, C++ introduces following new operators:
1) :: scope resolution operator
2) new memory allocation operator
3) delete memory release operator
4) ->* pointer to member operator
5) .* pointer to member operator
6) << insertion operator
7) >> extraction operator

Scope Resolution Operator


o Scope of a variable starts with its declaration and extends till the end of the block or scope in which
it is declared.
o In C, global variable can not be accessed from inner block. C++ resolves this problem by
introducing :: scope resolution operator.
o :: <global variable>
o Consider the following code

Prepared by: Mr. Jay Nanavati


Page 6 of 7

#include <iostream.h>
#include <conio.h>

int x=1000; //global variable

void main(void)
{
cout<<"\n x = "<<x; //Refers to global x , x=1000

int x=100;

cout<<"\n x = "<<x; //Refers to x declared in main(), x=100

{
cout<<"\n\n Inside scope...";
cout<<"\n x = "<<x; //Refers to x declared latest i.e. in main(), x=100

int x = 10;

cout<<"\n x = "<<x; //Refers to x declared latest i.e. inside block, x=10

cout<<"\n ::x = "<<::x; //Refers to x global x i.e. x = 1000

cout<<"\n Leaving scope...";


}

cout<<"\n In main(), x = "<<x; // Refers to x declared in main(), x=100


cout<<"\n ::x = "<<::x; //Refers to x global x i.e. x = 1000
}

o Scope resolution operator has two more uses: To define member function outside the class and to
access static members of the class.

Dynamic Memory Management using new and delete operators

o Many times you are not aware in advance how much memory you will need to store particular
information in a defined variable and the size of required memory can be determined at run time.
o You can allocate memory at runtime for the variable of a given type using a special operator in C++
which returns the address of the space allocated. This operator is called new operator.

Synatx-1: <datatype> *<pointer name> = new <datatype>;


e.gint *ip = new int; // An integer is created & is initialized to garbage value

Synatx-2: <datatype> *<pointer name> = new <datatype>(initial value);


e.gint *ip = new int(10); // An integer is created & is initialized to 10

Synatx-3: <datatype> *<pointer name> = new <datatype>[No. of elements];


e.gint *arr = new int[5]; // An array of 5 integers is created & array elements are initialized to zero.

o If you are not in need of dynamically allocated memory anymore, you can use delete operator,
which de-allocates memory previously allocated by new operator.

Syntax-1 delete <pointer name>;


e.g. delete ip; // Releases memory occupied by a single variable

Syntax-2 delete [ ]<pointer name>;


e.g. delete [ ]arr ; // Releases memory occupied by entire array
new and delete operator can also be used with objects of classes.

Prepared by: Mr. Jay Nanavati


Page 7 of 7

o Advantages of using new/delete over malloc()/calloc() etc./free()


 Automatically computes correct size of the variable, sizeof() operator is not to be used.
 Automatically return correct pointer type, so type casting is not required.
 It is possible to assign some value while creating a variable.
 new and delete operator can be overloaded to behave in different manner.

Manipulators
o Manipulators are operators that are used to format the data display.
o The most commonly used manipulators are endl and setw.
o The endl is used to insert a newline in the output.
o It can be used as: cout<<”some string<<endl;
o By default, << operator displays left-justified values.
o If you want to display values using some specific space and left-justification, setw() operator can be
used.
o e.g. cout<<setw(5)<<345;
It will display : 345 i.e. left-justified.

Usage of header files

The following table lists most of the frequently used header files:

Header file Content and Purpose


iostream.h Console i/o using cout, cin, <<, >> etc.
conio.h Console i/o using put(), get(), getch() etc
string.h String manipulation functions
stdlib.h Utility functions like conversion from number to
text, text to number, memory allocation etc.
math.h Mathematical functions
ctype.h Character-related functions
fstream.h File i/o functions

Prepared by: Mr. Jay Nanavati

You might also like