Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

LECTURE NOTES

UNIT-1
Object Oriented Programming Concepts & Introduction to C++
1.1 Evolution of OOPS:
The OOP (Object Oriented Programming) approach is most commonly used approach now a days.
OOP is being used for designing large and complex applications. Before OOP many programming
approaches existed which had many drawbacks.

These programming approaches have been passing through revolutionary phases just like
computer hardware. Initially for designing small and simple programs, the machine language was
used. Next came the Assembly Language which was used for designing larger programs. Both
machine and Assembly languages are machine dependent. Next came Procedural Programming
Approach which enabled us to write larger and hundred lines of code. Then in 1970, a new
programming approach called Structured Programming Approach was developed for designing
medium sized programs. In 1980's the size of programs kept increasing so a new approach known
as OOP was invented.

1) Monolithic Programming Approach


2) Procedural Programming Approach
3) Structured Programming Approach
4) Object Oriented Programming Approach

Monolithic Programming Approach: In this approach, the program consists of sequence of


statements that modify data. All the statements of the program are Global throughout the whole
program. The program control is achieved through the use of jumps i.e. goto statements. In this
approach, code is duplicated each time because there is no support for the function. Data is not fully
protected as it can be accessed from any portion of the program. So this approach is useful for
designing small and simple programs. The programming languages like ASSEMBLY and BASIC follow
this approach.

Procedural Programming Approach: This approach is top down approach. In this approach, a
program is divided into functions that perform a specific task. Data is global and all the functions can
access the global data. Program flow control is achieved through function calls and goto statements.
This approach avoids repetition of code which is the main drawback of Monolithic Approach. The
basic drawback of Procedural Programming Approach is that data is not secured because data is
global and can be accessed by any function. This approach is mainly used for medium sized
applications. The programming languages: FORTRAN and COBOL follow this approach.

Structured Programming Approach: The basic principal of structured programming approach is to


divide a program in functions and modules. The use of modules and functions makes the program
more comprehensible (understandable). It helps to write cleaner code and helps to maintain control
over each function. This approach gives importance to functions rather than data. It focuses on the
development of large software applications. The programming languages: PASCAL and C follow this
approach.

1
Object Oriented Programming Approach: The OOP approach came into existence to remove the
drawback of conventional approaches. The basic principal of the OOP approach is to combine both
data and functions so that both can operate into a single unit. Such a unit is called an Object. This
approach secures data also. Now a days this approach is used mostly in applications. The
programming languages: C++ and JAVA follow this approach. Using this approach we can write any
lengthy code.

1.2 DESCRIBE PROCEDURE ORIENTED PROGRAMMING:

Procedural Programming Approach is top down approach in which program is divided into
functions
that perform a specific task. Data is global and all the functions can access the global data. Program
flow control is achieved throw the use of jumps and function calls. This approach avoid the
repetition of code. The programming languages like FORTRAN and COBOL follow this approach.

 In a multi-function program we use global variable to communicate between two functions.


 Global variable can be use by any function at any time while local variables are only used within
the function.
 But it creates problem in large program because we can’t determine which global variables (data)
are used by which function.
 Also global variables are accessed by all the function so any function can change its value at any
time so all the function will be affected.

Characteristics of procedure-oriented programming language:

1. It emphasis on algorithm.

2
2. Large programs are divided into smaller programs known as functions.
3. Function can communicate by global variable.
4. Data move freely from one function to another function.
5. Functions change the value of data at any time from any place. (Functions transform
data from one form to another.)
6. It uses top-down programming approach.

1.3 DESCRIBE OBJECT ORIENTED PARADIGM:

The Object Oriented Programming Approach came to remove some of the flaws encountered in
procedural approach for designing large and complex programs. The basic principal of Object
Oriented Programming Approach is to combine both data and functions that operate on that data
into a single unit. Such a unit is called an object.

It treats data as a critical element in the program development and does not allow it to flow freely
around the system. It tries data more closely to the functions that operate on it, and protects it from
accidental modifications from outside the functions . OOP allow decomposition of a problem into
number of entities called objects and build data functions around these objects. The data of a object
can be accessed only by the functions associated with that object. Functions of one object can access
the functions of another objects.

 Two objects can communicate via the function without knowing the data of each another.
 We can represent a class by two ways as shown in the below figure:
 Out of these two methods, first method to represent object is widely used.

Characteristics of object-oriented programming language:

1. It emphasis on data rather than algorithm.


2. Program is divided into multiple portions known as Class/objects.
3. Class: We are using class as user defined data type to create object.
4. Data encapsulation: Data and function both are combined into one portion known as class.
5. Data hiding: Data cannot be accessed outside the class and provides the fundamental of
private and public data.
6. Interface: Object can communicate via the Interface.
7. Inheritance: We can create one object which acquire properties and method of another
object.
8. Polymorphism: We can use one method for the multiple uses.
9. Dynamic binding: Two procedures are linked during the time of execution.
10. Follows up bottom-up

3
1.4 state basic concepts of object oriented programming languages:

Object-Oriented Programming is a methodology to design a program using classes and objects.


It simplifies the software development and maintenance by providing some concepts:

o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation

Object:

Any entity that has state and behavior is known as an object. Here, state means data and behavior
means functionality. .

For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

 Object is a runtime entity, it is created at runtime.


 Object is an instance of a class. All the members of the class can be accessed through
object.
Example to create object of student class using s1 as the reference variable.
Student s1; //creating an object of Student class
In this example, Student is the type and s1 is the reference variable that refers to the instance of
Student class.
Class:
Collection of objects is called class. It is a logical entity. It is a template from which objects are
created. It can have fields, methods, constructors etc.
Ex:
class Student
{
public:
int id; //field or data member
float salary; //field or data member
String name;//field or data member
} ;
/* c++ program on usage of class and object*/

4
#include <iostream>
using namespace std;
class Student
{
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
};
int main() {
Student s1; //creating an object of Student
s1.id = 001;
s1.name = "red";
cout<<s1.id<<endl;
cout<<s1.name<<endl;
return 0;
}
Output:
001
red
/* Initialize and Display data through method*/
#include <iostream>
using namespace std;
class Student
{
public:
int id;
string name;
void insert(int i, string n)
{
id = i;
name = n;
}
void display()
{
cout<<id<<" "<<name<<endl;
}
};
int main( )
{
Student s1;
Student s2;
s1.insert(001, "red");
s2.insert(002, "black");
s1.display();
s2.display();
return 0;
}
Output:
001 red
002 black

5
Inheritance:

 Inheritance is the process by which one class inherits the properties and method of another
class. This is based on the hierarchical classification.
 For example, we can categories the ‘animal’ into two categories: ‘wild animal’ and ‘pet
animal’. Also we can categories ‘wild animal’ into ‘tiger’, ‘lion’, ‘leopard’ and ‘pet animal’
into ‘cat’, ‘dog’, ‘bull’.

 It provides code reusability.

 It is used to achieve runtime polymorphism.


 Here we can add new features (new data and function) into existing class without modifying
it. This is done by deriving new class (subclass or child class) from existing class (super class
or parent class). The sub class contains the facility of super class as well as its own features.

Polymorphism:

 Polymorphism is a Greek work and its mean is to take more than one form. An operations show
different behavior in different condition.
 The behavior is used on the data type of variable and number of arguments.
 For example following function calls are different in terms of number of arguments.

sum(int a, int b, int c)

sum(int a, int b)

 The above fundamental is known as the function overloading.


 Similar way we can perform operator overloading in which one operator is used for multiple
operation.
 For example ‘+’ is a operator that is used for arithmetic operation the same operator we can use for
the concatenation of string and add two structure elements.

Dynamic Binding:

 Binding means linking of two functions.


 There are two types of binding technique: one is compile time binding in which linking is performed
during the time of compilation.

6
 It is also known as early binding.
 Another technique of binding is dynamic binding in which linking of function is performed during the
execution when function is called.
 It is also known as the late binding. Dynamic binding provides the facility of the polymorphism.

Message Passing:

 Message passing is important technique for communication between objects.


 When two objects want to be communicate i.e. we have to send the message from one object two
another objects we are use some special method and that method is known as interface.

1.5 Benefits of oops:


 Reusability: In OOP‟ s programs functions and modules that are written by a user can be
reused by other users without any modification.
 Inheritance: Through this we can eliminate redundant code and extend the use of existing
classes.
 Data Hiding: The programmer can hide the data and functions in a class from other classes.
It helps the programmer to build the secure programs.
 Reduced complexity of a problem: The given problem can be viewed as a collection of
different objects. Each object is responsible for a specific task. The problem is solved by
interfacing the objects. This technique reduces the complexity of the program design.
 Easy to Maintain and Upgrade: OOP makes it easy to maintain and modify existing code
as new objects can be created with small differences to existing ones. Software complexity
can be easily managed.
 Message Passing: The technique of message communication between objects makes the
interface with external systems easier.
 Modifiability: it is easy to make minor changes in the data representation or the
procedures in an OO program. Changes inside a class do not affect any other part of a
program, since the only public interface that the external world has to a class is through the
use of methods.
 We can build the programs from standard working modules that communicate with one
another, rather than having to start writing the code from scratch which leads to saving of
development time and higher productivity,
 OOP language allows to break the program into the bit-sized problems that can be solved
easily (one object at a time).
 The new technology promises greater programmer productivity, better quality of software
and lesser maintenance cost.

Usage of C++

7
By the help of C++ programming language, we can develop different types of secured and robust
applications:

o Window application
o Client-Server application
o Device drivers
o Embedded firmware etc

1.6 List different types of object oriented programming languages:

while SIMULA 67 is credited as the first object oriented programming language, the popular
languages are:

C++ C# Java

JavaScript PHP Perl

Python Ruby small talk

1.7 write the structure of C++ program:


The program written in C++ language follows this basic structure. The sequence of sections
should be as they are in the basic structure. A C program should have one or more sections but
the sequence of sections is to be followed.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section & class
declarations 5.Member function
definition
6. Main function
section main()
{
Declaration section Executable section
}
1. DOCUMENTATION SECTION : comes first and is used to document the use of logic or reasons in
your program. It can be used to write the program's objective, developer and logic details. The
documentation is done in C language with /* and */ . Whatever is written between these two are
called comments.
2. LINKING SECTION : This section tells the compiler to link the certain occurrences of keywords or
functions in your program to the header files specified in this section.
e.g. #include<iostream>
using namespace std;
 directive causes the preprocessor to add the contents of the iostream file to the program.
It contains declarationsfor cout and cin.
 cout is a predefined object that represents the standard output stream. The operator << is
an insertion operator, causes the string in double quotes to be displayed on the screen.

8
screen

cout << “C++”

Object Insertion Operator variable

The statement cin>>n; is an input statement and causes the program to wait for the user to type
in a number. The number keyed is placed on the variable “n”. The identifier cin is a predefined
object in C++ that corresponds to the standard input stream. The operator >> is known as
extraction operator. It extracts the value from the keyboard and assigns it to the value variable on
its right.

Object Extraction operator variable


cin 45.5
>>

Keyboard

3. DEFINITION SECTION : It is used to declare some constants and assign them some value.
e.g. #define MAX 25
Here #define is a compiler directive which tells the compiler whenever MAX is found in the
program replace it with 25.
4. GLOBAL DECLARATION SECTION : Here the variables and class definations which are used through
out the program (including main and other functions) are declared so as to make them global(i.e
accessible to all parts of program). A CLASS is a collection of data and functions that act or
manipulate the data. The data components of a class are called data members and function
components of a class are called member functions
A class ca also termed as a blue print or prototype that defines the variable or functions
common to all objects of certain kind. It is a user defined data type

e.g.

int i; //this declaration is done outside and before main()

5. SUB PROGRAM OR FUNCTION SECTION : This has all the sub programs or the functions which
our program needs.
void display()
{
cout<<”C++ is better that C”;
}

9
SIMPLE C++ PROGRAM:
#include<iostream> using namespace std; void display()
{
cout<<”C++ is better that C”;
}
int main()
{
display()
return 0;
}

6. MAIN FUNCTION SECTION : It tells the compiler where to start the execution
from main()
{
point from execution starts
}
main function has two sections
•declaration section : In this the variables and their data types are declared.
• Executable section or instruction section : This has the part of program which
actually performs the task we need.

namespace: namespace is used to define a scope that could hold global identifiers.
ex:-namespace scope for c++ standard library.
A classes ,functions and templates are declared within the namespace
named
std using namespace std;
user defined name space:
syntax for defining name space is

namespace namespace_name
{
//declarations of variables.functions,classes etc...
}
ex: #include<iostream> using
namespace std;
namespace sample
{`
int m;
void display(int n)
{
cout<<"in namespace N="<<n<<endl;
}
}

using namespace sample; int main()


{
int a=5; m=100;
display(200);
cout<<"M in sample name space:"<<sample::m;
return 0;}

#include<iostream>
This directive causes the preprocessor to add content of iostream file to the program. some old
versions of C++ used iostream.h .if complier does not support ANSI (american nation standard

10
institute) C++ then use header file iostream.

Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming
(OOP)
Procedure Oriented Programming Object Oriented Programming
program is divided into small parts
1 program is divided into parts called objects.
called functions.
Importance is not given to data but to Importance is given to the data rather than
2 functions as well as sequence of procedures or functions because it works as
actions to be done. a real world.
3 POP follows Top Down approach. OOP follows Bottom Up approach.
4 OOP has access specifiers named
It does not have any access specifier.
Public, Private, Protected, etc.
5 Data can move freely from function objects can move and communicate with
to function in the system. each other through member functions.
6 To add new data and function in OOP provides an easy way to add new
POP is not so easy. data and function.
Most function uses Global data for In OOP, data can not move easily from
sharing that can be accessed freely function to function,it can be kept public
7 from function to function in the or private so we can control the access of
system. data.
8 It does not have any proper way OOP provides Data Hiding so provides
for hiding more
data so it is less secure. security.
In OOP, overloading is possible in the
Overloading is not possible. form of Function Overloading and
9 Operator Overloading.
Example of Procedure Oriented
Programming are : C, VB, Example of Object Oriented Programming
10 FORTRAN, Pascal. are : C++, JAVA, VB.NET, C#.NET.

Principles( or features) of object oriented programming:


1. Encapsulation
2. Data abstraction
3. Polymorphism
4. Inheritance
5. Dynamic binding
6. Message passing
Encapsulation: Wrapping of data and functions together as a single unit is known as
encapsulation. By default data is not accessible to outside world and they are only accessible
through the functions which are wrapped in a class. prevention of data direct access by the
program is called data hiding or information hiding

Data abstraction :Abstraction refers to the act of representing essential features without
including the back ground details or explanation. Classes use the concept of abstraction and are
defined as a list of attributes such as size, weight, cost and functions to operate on these
attributes. They encapsulate all essential properties of the object that are to be created. The
attributes are called as data members as they hold data and the functions which operate on these
data are called as member functions.Class use the concept of data abstraction so they are
called abstract data type (ADT)

11
Polymorphism: Polymorphism comes from the Greek words “poly” and “morphism”. “poly”
means many and “morphism” means form i.e.. many forms. Polymorphism means the ability to
take more than one form. For example, an operation have different behavior in different
instances. The behavior depends upon the type of the data used in the operation.
Different ways to achieving polymorphism in C++ program:
1) Function overloading 2)
Operator overloading #include<iostream>
using namespace std;
int main(){
int a=4;
a=a<<2;
cout<<”a=”<<a<<endl; return 0;
}

Inheritance: Inheritance is the process by which one object can acquire the properties of another.
Inheritance is the most promising concept of OOP, which helps realize the goal of constructing
software from reusable parts, rather than hand coding every system from scratch. Inheritance
not only supports reuse across systems, but also directly facilitates extensibility within a
system. Inheritance coupled with polymorphism and dynamic binding minimizes the amount of
existing code to be modified while enhancing a system.
When the class child, inherits the class parent, the class child is referred to as derived
class (sub class) and the class parent as a base class (super class). In this case, the class child has
two parts: a derived part and an incremental part. The derived part is inherited from the class
parent. The incremental part is the new code written specifically for the class child.

Dynamic binding: Binding refers to linking of procedure call to the code to be executed in
response to the call. Dynamic binding(or late binding) means the code associated with a given
procedure call in not known until the time of call at run time.

Message passing: An object oriented program consists of set of object that communicate with
each other.
Objects communicates with each other by sending and receiving information .A message for an
object is a request for execution of a procedure and there fore invoke the function that is called
for an object and generates result.

1.8 CREATE,COMPLE AND EXECUTE A C++ PROGRAM

1.9 DIFFERENTIATE C ANDC++

C C++

C is structured programming language C++ is object-oriented programming language .


C supports static binding . C++ supports static binding and dynamic
binding .
C does not support template C++ supports template
C does not inline function . C++ supports inline function.

C input and output functions are printf and C ++ input and output functions are cout and cin
scanf.
C uses header file stdio.h C++ uses header file iostream.h
C is top to bottom approach C++ is bottom to top approach.

12
C does not support inheritance C++ supports inheritance.

C does not create objects C++ does create objects

C supports encapsulation but not member C++ supports encapsulation and member
functions functions
C does not support function and operator C++ supports function and operator over loading .
over loading .

1.10 EXPLAIN C++ I/O OPERATION ITH EXAMPLE


To read and write the information we use the console input and output . In C++ we
use the header file that is <iostream.h> .
Stream is a abstraction of a construct that allows you to send or receive unknown number of
bytes .

To handle the input and output operations we use pre-defined stream objects .
Input Central Outp

devic Console input: To handle console
processin input we use cin object. ut
Cin : It is a input stream object which is available in<iostream.h> header file .
e >> this symbolgisunit
called as extractor . devic
This is used to extract input from the keyboard and place it into specified variable
name
.
Syntax: cin >> variable _name ;
For example :
cin >> a ;
This is used to read the input from user with the specified name .

 Console Output:To handle console output we use cout object.

Cout: it is a output stream object which is available in <iostream.h> header file.


<< this symbol is called insertion operator.
This is used to display the output on the monitor with the specified variable
name. Syntax :
Cout << variable name ;
For example :
Cout << a ;
This is used to display the information of specified variable name.

1.11 EXPLAIN SYNTAX OF COMMENT STATEMENT IN C++


Comments are used to describe the statements. And improve the readability of
the statements. While executing they are ignored by the compiler . They are not even
translated by the complier.
Program comments are explanatory statements that you can include in the C+
+ code that you write and helps anyone reading it's source code. All programming languages
allow for some form of comments.
These are of two types:
1. Single line comment.
2. Multi line comment.

Single Line Comment

13
These are used to describe only single line.
It is denoted by two back slash “ // ”.
For example:
#include <iostream>

using namespace std;

main() {
cout << "Hello World"; // prints Hello World
return 0;
}
MULTI LINE COMMENT:
These are used to describe multi statements at a time.
It is denoted by stating with /* and ending with */
characters. For example:
/* This is a comment */
/* C++ comments can also
* span multiple lines
*/

14
1.12 LIST KEYORDS OF C++ OTHER THAN C
The keywords implement specific c++ language features they are explicitly reserved identifiers
and cannot be used as names for the program variables or other user defined program elements.
Different keywords available in c+ + other than C
 Asm  Friend
 Catch  Static
 Template
 Throw  New
 Try  Delete
 Public  Operator
 Private  Enum
 Protected  Virtual
 Inline Class

15
16
1.13 EXPLAIN C++ OPERATORS

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
C++ is rich in built-in operators and provides the following types of operators:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators

Arithmetic Operators
They are the types of operators used for performing mathematical/arithmetic operations.

Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


A + B will give 30
+ Adds to operands.

A - B will gives -10


– Subtracts 2nd operand from 1st operand.

A * B will give 200


* Multiplies 2 operands.

B/A will give 2


/ Divides numerator by denominator.

B%A will give 0


% Returns remainder after division.

A + + will give 11
++ Increases an integer value by 1.

A -- will give 9
-- Decreases an integer value by 1.

17
Relational Operators:
There are following relational operators supported by C++ language.

Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


Checks if the values of two operands are equal A == B is not true.
== or not, if yes then condition becomes true
Checks if the values of two operands are equal A ! = B is true.
!= or not, if values are not equal then condition
becomes true.

Checks if the value of left operand is greater A > B is not true.


> than the value of right operand, if yes then
condition becomes true.

Checks if the value of left operand is less than the A < B is true.
< value of right operand, if yes then condition
becomes true.
A >= B is not true.
Checks if the value of left operand is greater
>= than or equal to the value of right operand, if
yes then condition becomes true.

A <= B is true
Checks if the value of left operand is less than
<= or equal to the value of right operand, if yes
then condition becomes true.

Logical Operators:
There are following logical operators supported by C++

language Assume variable A holds 1 and variable B holds 0,

then:

Operator Description Example


Called Logical AND operator. If both the operands is false.
A && &&
are non-zero, then condition becomes true.
Called Logical OR Operator. If any of the two A | | B is true.
||
operands is non-zero, then condition becomes true.

A && Called Logical NOT Operator. Use to ! is true.


reverses the logical state of its
! operand. If
a condition is true, then Logical
NOT operator will make false.

18
Bitwise Operators:
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^
are as follows:

p q p&q p|q p^q

0 0 0 0 0
0 1 0 1 1

1 1 1 1 0

1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows: A = 0011 1100

B = 0000 1101

19
A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

20
21
CM-404 C++

22
CM-404 C++

23
CM-404 C++

24
CM-404 C++

25

You might also like