17
OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming Concepts 4. Comparison with C 5. Overview of C++ 6. Pointers 7. Functions 8. Scope and Namespaces 9. Source files and programs. Object Oriented Programming Page 1

OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

  • Upload
    dodiep

  • View
    275

  • Download
    5

Embed Size (px)

Citation preview

Page 1: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

OBJECT ORIENTED PROGRAMMING

STUDY NOTES

UNIT 1 - INTRODUCTION TO OBJECT ORIENTED

PROGRAMMING

1. Object Oriented Programming Paradigms

2. Comparison of Programming Paradigms

3. Basic Object Oriented Programming Concepts

4. Comparison with C

5. Overview of C++

6. Pointers

7. Functions

8. Scope and Namespaces

9. Source files and programs.

Object Oriented Programming Page 1

Page 2: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

INTRODUCTION TO OBJECT ORIENTED PROGRAM

Procedure Oriented Programming (POP)

The high level languages, such as BASIC, COBOL, C, FORTRAN are commonly

known as Procedure Oriented Programming.

Using this approach, the problem is viewed in sequence of things to be done, like

reading, processing and displaying or printing. To carry out these tasks the function

concepts must be used.

♦ This concept basically consists of number of statements and these statements are

organized or grouped into functions.

♦ While developing these functions the programmer must care about the data that is

being used in various functions.

♦ A multi-function program, the data must be declared as global, so that data can be

accessed by all the functions within the program & each function can also have its

own data called local data.

♦ The global data can be accessed anywhere in the program. In large program it is

very difficult to identify what data is accessed by which function. In this case we

must revised about the external data and as well as the functions that access the

global data. At this situation there is so many chances for an error.

Object Oriented Programming Page 2

Page 3: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

Object Oriented Programming (OOP)

♦ This programming approach is developed to reduce the some of the drawbacks

encountered in the Procedure Oriented Programming Approach.

♦ The OO Programming approach treats data as critical element and does not allow

the data to freely around the program.

♦ It bundles the data more closely to the functions that operate on it; it also protects

data from the accidental modification from outside the function.

♦ The object oriented programming approach divides the program into number of

entities called objects and builds the data and functions that operates on data

around the objects.

♦ The data of an object can only access by the functions associated with that object.

Object Oriented Programming Page 3

Page 4: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

Comparison of Programming Paradigms

S.No.

Procedure Oriented Programming Object Oriented Programming(C) (C++)

1.

2.

3.

4.

5.

6.

7.

8.

9.

Programs are divided into smaller Sub-programs known as functions.

New data and functions can beadded easily.

It is a Top-Down Approach

Data cannot be secured andavailable to all the function

Here, the reusability is not possible, hence redundant code cannot be avoided.

It does not have any accessspecifier.

Data can move freely from function to function in the system.

Overloading is not possible.

Example of POP are : C, VB,FORTRAN, Pascal.

Programs are divides into objects & Classes.

New data and functions can be added easily.

It is a Bottom-Up Approach

Data can be secured and can be available in the class in which it is declared.

Here, We can reuse the existing oneusing the Inheritance concept.

It has access specifiers named Public, Private, Protected, etc.

Objects can move and communicate with each other through member functions.

Overloading is possible in the form of Function Overloading and Operator Overloading.

Example of OOP are : C++, JAVA,Small talk.

Object Oriented Programming Page 4

Page 5: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

Concepts of OOPS

The general concepts of OOPS comprises the following.

1. Object

2. Class

3. Data abstraction

4. Inheritance

5. Polymorphism

6. Dynamic Binding

7. Message passing.

1. Object

Object is an entity that can store data and, send and receive messages. They are

runtime entities; they may represent a person, a place a bank account, a table of data or

any item that the program must handle. It is an instance of a class.

They may also represent user-defined data such as vectors, time and lists. When a

program is executed, the object interacts by sending messages to one another. Each

object contain data and code to manipulate the data objects can interact without having

to know details of each other’s data or code. It is sufficient to know the type of message

accepted and the type of response returned by the objects.

2. Classes

A class is a collection of objects of similar type. Classes are user defined data

types and behave like the built in types of a programming language. For example

mango, apple and orange are members of the class fruit. Then the statement FRUIT

MANGO; will create an object mango belonging to the class fruit. The syntax used to

Object Oriented Programming Page 5

Page 6: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

create an object is no different than the syntax used to create an integer object in C. If

fruit has been defined as a class, then the statement

fruit mango;

will create an object mango belonging to the class fruit.

3. Data abstraction and encapsulation:

The wrapping up of data and its functions into a single unit (class) is known as

encapsulation. The data is not accessible to the outside world and only those functions

which are wrapped in the class can assess it> these functions provide the interface

between the objects data and the program> this insulation of data from direct access by

the program is called DATA HIDING (or data abstraction). Since the classes use the

concept of data abstraction they are known as ABSTRACT DATA TYPES (ADT)

4. Inheritance:

In heritance is the process by which objects of one class acquire the

properties of objects of another class. It supports the concept of hierarchical

classification. For example the bird robin is a part of the class flying birds which again

a part of bird. As given in the diagram below each derived class shares common

characteristics with the class from which it is derived.

In OOP, the concept of inheritance provides the idea of reusability. This means that

we can add additional features to an existing class without modifying it. This is possible

by deriving a new class from the existing one. The new class will have the combined

features of both the classes. The real appeal and power of the inheritance mechanism

is that it allows the programmer to reuse a class that is almost, but not exactly, what he

wants.

Object Oriented Programming Page 6

Page 7: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

5. Polymorphism:

Polymorphism means the ability to take more than one form. For example an

operation may exhibit different behavior in different instances. The behavior depends

upon the types of data used in the operation. For example consider the operation

addition. For two numbers, the operation will generate a sum . if the operands are

strings, then the operation would produce a third string by concatenation.

Here in the below given diagram a single function draw () does different operation

according to the behavior of the type derived. I.e. Draw () function works in different

form.

Polymorphism plays an important role in allowing objects having internal structures

to share the same external interface. This means that a general class of operations may

Object Oriented Programming Page 7

Page 8: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

be accessed in the same manner even though specific actions associated with each

operation may differ. Polymorphism is extensively used in inheritance.

6. Dynamic Binding

Dynamic binding means that the code associated with a given procedure call is not

known until the time of the call at run-time. It is associated with polymorphism and

inheritance. A function call is associated with a polymorphic reference depends on the

dynamic type of that reference.

7. Message Communication

An object oriented program consists of a set of objects that communicate with each

other. Objects communicate with one another by sending and receiving information

much the same way as people pass messages to one another. Objects have a life

cycle. They can be created and destroyed. Communication with an object is feasible as

long as it is alive.

Benefits or Advantages of OOPS

♦ The complexity of software can be managed easily.

♦ Data hiding concept help the programmer to build secure programs

♦ Through the class concept we can define the user defined data type

♦ The inheritance concept can be used to eliminate redundant code

♦ The message-passing concept helps the programmer to communicate between

different objects.

♦ New data and functions can be easily added whenever necessary.

♦ OOPS ties data elements more closely to the functions that operates on.

Basics of C++ Programming:

C++ was developed by BJARNE STROUSSTRUP at AT&T BELL Laboratories in

Murry Hill, USA in early 1980’s.

Strousstrup combines the features of ‘C’ language and ‘SIMULA67’ to create more

powerful language that support OOPS concepts, and that language was named as “C

with CLASSES”. In late 1983, the name got changed to C++.

Object Oriented Programming Page 8

Page 9: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

The idea of C++ comes from ‘C’ language increment operator (++) means more

additions.

C++ is the superset of ‘C’ language or extension of ‘C’ language, most of the ‘C’

language features can also applied to C++, but the object oriented features (Classes,

Inheritance, Polymorphism, Overloading) makes the C++ truly as Object Oriented

Programming language.

Structure of C++ Program

Section 1 : Header File Declaration Section

1. Header files used in the program are listed in this section.

2. Header File provides Prototype declaration for different library functions.

3. We can also include user define header file.

4. Basically all preprocessor directives are written in this section.

Include files provides instructions to the compiler to link functions from the system

library.

Eg: #include <iostream.h>

#include – Preprocessor Directive

iostream.h – Header File

Object Oriented Programming Page 9

Page 10: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

Section 2 : Global Declaration Section

1. Global Variables are declared here.

2. Global Declaration may include

♦ Declaring Structure

♦ Declaring Class

♦ Declaring Variable

Section 3 : Class Declaration Section

1. Actually this section can be considered as sub section for the global declaration

section.

2. Class declaration and all methods of that class are defined here.

3. A class is a way to bind and its associated functions together. It is a user defined

datatype. It must be declared at class declaration part.

Section 4 : Main Function

1. Each and every C++ program always starts with main function.

2. This is entry point for all the function. Each and every method is called indirectly

through main.

3. We can create class objects in the main.

4. Operating system call this function automatically.

main( )

{

...........................

}

Program execution begins at the opening brace and ends at the closing brace. The

closing brace of the main function is the logical and of the program.

Section 5 : Method Definition Section

1. This is optional section . Generally this method was used in C Programming.

2. Member function definition describes how the class functions are implemented.

This must be the next part of the C++ program.

Object Oriented Programming Page 10

Page 11: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

Input / Output Statements

Input Stream

Syntax:

cin >> var1 >> var2 >>;

cin is a keyword, it is an object, predefined in C++ to correspond to the standard input

stream.

>> is the extraction or get from operator. Extraction operation (>>) takes the value from

the stream object on its left and places it in the variable on its right.

Eg:

cin>>x;

cin>>a>>b>>c;

Output Stream:

Syntax:

cout<<var1<<var2;

cout is a keyword, predefined object of standard output stream

<< is called the insertion or put to operator. It directs the contents of the variable on its

right to the object on its left. Output stream can be used to display messages on output

screen.

Eg:

cout<<a<<b; cout<<”value of x is”<<x;

cout<<”Value of x is”<<x<<”less than”<<y;

Tokens

The smallest individual units in a program are known as tokens. C++ has the

following tokens

♦ Keywords

♦ Identifiers

♦ Constants

♦ Strings

♦ Operators

Object Oriented Programming Page 11

Page 12: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

Keywords

• It has a predefined meaning and cannot be changed by the user

• Keywords cannot be used as names for the program

variables. Keywords supported by C++ are:

asm auto break case catch char

class const continue default delete do

double else enum extern float for

friend goto if inline int long

new operator private protected public register

return short signed sizeof static struct

switch template this throw try typedef

union unsigned virtual void volatile while

The specific C++ Keywords

There are several keywords specific to C++

asn catch class delete

friend inline new operator

private protected public template

this throw try virtual

Identifiers

Identifiers refer to the names of variables, functions, arrays, classes, etc. created by

the programmer.

Rules for naming these identifiers:

1. Only alphabetic characters, digits and underscores are permitted.

2. The name cannot start with a digit.

3. Uppercase and lowercase letters are distinct.

4. A declared keyword cannot be used as a variable name.

Object Oriented Programming Page 12

Page 13: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

(i) Variables:

It is an entity whose value can be changed during program execution and is known

to the program by a name.

A variable can hold only one value at a time during program execution.

Eg:

Allowable variable names Invalid names

i 1_B – 1st letter must be alphabet

sum $xy - 1st letter must be alphabetA_B x+b - special symbol ‘+’ not allowed

A-1B

Declaration of Variables

Syntax

datatype variablename;

There are 2 features of variables which are supported by C++. They are,

(i) Dynamic Initialization of Variable

(ii) Reference Variable

(i) Dynamic initialization of variable

In C language, al the variables should be declared only at the beginning of

the program. But in C ++, they can be initialized wherever it is necessary in

the program.

Eg. Program:

void main()

{

int sum=0;

for(int i=1;i<=10;i++)

{

sum=sum+i;

}

int avg=sum/10; //Dynamic initialization of avg

cout<<”Sum is”<<sum;

cout<<”Average is”<<avg;

Object Oriented Programming Page 13

Page 14: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

}

Output:

Sum is 55

Average is 5.5

(ii) Reference Variable:

A reference variable provides an alias (alternative name) for a previously

defined variable.

For example, if we make the variable total a reference to the variable sum,

then total & sum can be used interchangeably to represent that variable.

A reference variable is created as follows:

datatype & ref_name = var_name;

Eg:

float sum = 100; float

& total = sum; cout

<< sum << total;

Both the variables refer to the same data object in the memory

i.e. total sum

100

Eg. Program:

void main()

{

int sum=0;

for(int i=1;i<=10;i++)

{

sum=sum+i;

}

int avg=sum/10; //Dynamic initialization of avg

int &total=sum; //Alias name for sum is total

cout<<”Sum is”<<total;

cout<<”Average is”<<avg;

}

Object Oriented Programming Page 14

Page 15: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

Output:

Sum is 55

Average is 5.5

Datatype

It is the type of data, that is going to be processed within the program. A variable can be

declared anywhere in the program before its first use.

Type Conversion:

(i) Implicit type conversion

(ii) Explicit type conversion

Implicit type conversion

It will be done by the compiler, by following the rule of lower type converted to higher

type.

Eg: int y = 10;

float z = 10.5,x;

x = y+z; (y is converted to float type by compiler)

Object Oriented Programming Page 15

Page 16: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

x = 10.0 + 10.5

x= 20.5 (result var. x is must be float)

Explicit type conversion

It will be performed by the programmer. According to the need of this in the

program.

Syntax: datatype

(var) Eg: int y = 10;

float z = 2.5;(resultant type of y+z is float, that is converted explicitly to int

type) x = int (y + z);

Now the result is of int type.

Constants

A quantity that does not change is known as constants.

Types of constants:

Integer constants- - Eg: 123, 25 – without decimal point

Character constants - Eg: ‘A’, ‘B’, ‘*’, ‘1’

Real constants - Eg: 12.3, 2.5 - with decimal point

Strings

A sequence of characters is called string. String constants are enclosed in double

quotes as follows

“Hello”

Operators

This is a SAMPLE (Few pages have been extractedfrom the complete notes:-It’s meant to show youthe topics covered in the full notes and as per thecourse outline

Download more at our websites:www.naarocom.com

To get the complete notes either in softcopy form or in Hardcopy

Page 17: OBJECT ORIENTED PROGRAMMING STUDY … ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2.Comparison of

(printed & Binded) form, contact us on:

Call/text/whatsApp +254 719754141/734000520

Email:

[email protected]@[email protected]

Get news and updates by liking ourpage on facebook and follow us on

Twitter

Sample/preview is NOT FOR SALE