Header Ads

Header ADS

Concept Of OOP'S (Object Oriented Programming System)

OOP’S concept

◼ What is object

◼ How to declare and calling an object

◼ What is class

◼ What is abstraction

◼ What is encapsulation

◼ OOP’S CONCEPT OR PRINCIPAL

It is necessary to understand some of the concepts used

extensively in object-oriented programming. These include:

1. Objects

2. Classes

3. Data abstraction and encapsulation

4. Inheritance

5. Polymorphism

6. Dynamic binding 

7. Message passing 

 


What is Objects

Objects are the basic run time entities in an object-oriented

system. They may represent a person, a place, a bank account,

a table of data or any item that the program has to handle. They

may also represent user-defined data such as vectors, time and

lists.

Objects take up space in the memory and have an associated

address like a record in Pascal, or a structure in c.


When a program is executed, the objects interact by sending

messages to one another. Foe example, if “customer” and

“account” are to object in a program, then the customer object

may send a message to the count object requesting for the

bank balance. Each object contain data, and code to

manipulate data. Objects can interact without having to know

details of each other’s data or code.

It is a sufficient to know the type of message accepted, and the

type of response returned by the objects



How To Create An Objects

An Object is an instance of a Class. When a class is defined, no

memory is allocated but when it is instantiated (i.e. an object is

created) memory is allocated.

Declaring Objects: When a class is defined, only the

specification for the object is defined; no memory or storage is

allocated. To use the data and access functions defined in the

class, you need to create objects.

Syntax:

Class_Name Object_Name;

For example

student st1;

Accessing data members and member functions using

object

The data members and member functions of class can be

accessed using the dot(‘.’) operator with the object. For

example

Syntax

object_name.function _name();

for example

st1.total();

Function Declation

 Syntax for Function declaration

datatype functionname(prototype);

int add( int , int );

void display(void);

float add(int , int, float);

Syntax For Function Defination

datatype functionname(argument list)

{

function body

}

int add(int p ,int q)

{

int r;

r=p+r;

return r;

}

Function calling

Syntax for function calling

Function name(actual parameter);

add(a,b);

add(a ,b, c);

◼ CLASS

We just mentioned that objects contain data, and code to

manipulate that data. The entire set of data and code of an

object can be made a user-defined data type with the help of

class. In fact, objects are variables of the type class.

declare an object syntaxclass_name object_name;

B.SCII student1;

Once a class has been defined, we can create any number of

objects belonging to that class. Each object is associated with

the data of type class with which they are created.


A class is thus a collection of objects similar types. For

examples, Mango, Apple and orange members of class fruit.

Classes are user-defined data types and behave like the built-in

data types of a programming language.

If fruit has been defines as a class, then the statement

Fruit Mango;

◼ HOW TO CREATE A CLASS

Class CLASS_NAME

{

DATA DECLARATION;

FUNCTION DECLARATION;

};

For EXAMPLE

Class student

{

int rollno;

int marks ;

int display();

};

Write a program using class and object

#include <iostream.h>

#include <conio.h>

class Mathematics //creating a class

{

int x, y;

public:

void input()

{

cout << "Input two integers“<<endl;

cin >> x >> y;

}

void add()

{

cout << "Result: " << x + y;

}

};

int main()

{

clrscr();

Mathematics m; // Creating an object of the class

m.input();

m.add();

getch();

return 0;

}


◼ ENCAPSULATION

 The wrapping up of data and function into a single unit (called

class) is known as encapsulation. Data and encapsulation is

the most striking feature of a class. The data is not accessible

to the outside world, and only those functions which are

wrapped in the class can access it. These functions provide the

interface between the object’s data and the program. This

insulation of the data from direct access by the program is

called data hiding or information hiding.

For EXAMPLE

Class student

{

char name[10];

int rollno;

int marks ;

int total(int ,int,int);

void display();

};

◼ Abstraction

Abstraction refers to the act of representing essential features

without including the background details or explanation.

Classes use the concept of abstraction and are defined as a list

of abstract attributes such as size, wait, and cost, and function

operate on these attributes. They encapsulate all the essential

properties of the object that are to be created.



◼ What is inheritance

The capability of a class to derive properties and

characteristics from another class is called Inheritance.

Inheritance is one of the most important feature of

Object Oriented Programming.

Sub Class: The class that inherits properties from

another class is called Sub class or Derived Class or

child class.

Super Class: The class whose properties are inherited by

sub class is called Base Class or Super class or parents

class or root class.

DIAGRAM OF BASE CLASS AND SUB CLASS

Inheritance is the process by which objects of one class acquired

the properties of objects of another classes. It supports the

concept of hierarchical classification. For example, the bird,

‘robin’

is a part of class ‘flying bird’ which is again a part of the class

‘bird’. The principal behind this sort of division is that each

derived class shares common characteristics with the class from

which it is derived as illustrated in fig 1.6.

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 feature of both the classes. The real appeal and power

of the inheritance mechanism is that it.


Why and when to use linheritance

Consider a group of vehicles. You need to create

classes for Bus, Car and Truck. The methods

fuelAmount(), capacity(), applyBrakes() will be same

for all of the three classes. If we create these classes

avoiding inheritance then we have to write all of these

functions in each of the three classes as shown in

below

Example

You can clearly see that above process results in

duplication of same code 3 times. This increases the

chances of error and data redundancy. To avoid this

type of situation, inheritance is used. If we create a

class Vehicle and write these three functions in it and

inherit the rest of the classes from the vehicle class,

then we can simply avoid the duplication of data and

increase re-usability. Look at the below diagram in

which the three classes are inherited from vehicle

class

Example inheritance

Using inheritance, we have to write the functions only

one time instead of three times as we have inherited

rest of the three classes from base class(Vehicle)

TYPES OF INHERITANCE

1.Single Inheritance

2.Multiple Inheritance

3.Multilevel Inheritance

4.Hybried Inheritance

5.Hierarical Inheritance

1.Single inheritance:

In single inheritance, a class is allowed to inherit from only

one class. i.e. one sub class is inherited by one base class

only.


2.Multiple Inheritance:

Multiple Inheritance is a feature of C++ where a class can

inherit from more than one classes. i.e one sub class is

inherited from more than one base classes.


3.MultilevelInheritance:

In this type of inheritance, a derived class is created from another

derived class.


4. Hybrid (Virtual) Inheritance:

Hybrid Inheritance is implemented by combining more than one type

of inheritance. For example: Combining multilevell inheritance

and Multiple Inheritance.


5. Hierarchical Inheritance:

In this type of inheritance, more than one sub class is

inherited from a single base class. i.e. more than one

derived class is created from a single base class.



◼ Polymorphism

The term "Polymorphism" is the combination of "poly" + "

morphs" which means many forms. It is a greek word.

In object-oriented programming, we use 3 main

concepts: inheritance, encapsulation, and

polymorphism.

Real Life Example Of Polymorphism

Let's consider a real-life example of polymorphism. A

lady behaves like a teacher in a classroom, mother or

daughter in a home and customer in a market. Here, a

single person is behaving differently according to the

situations.

◼ Types of polymorphism


 1.  Compile time polymorphism

 2.  Run Time polymorphism


1 .Compile time polymorphism: 

The overloaded functions

are invoked by matching the type and number of

arguments. This information is available at the compile

time and, therefore, compiler selects the appropriate

function at the compile time. It is achieved by function

overloading and operator overloading which is also

known as static binding or early binding. Now, let's

consider the case where function name and prototype

is same.

Example of compile time

polymorphism

Definition syntax

int add(int a ,int b , int c)

{

}

int add(int a ,float b)

{

}

// Calling syntax

add(3,9.2);

add(3 ,4 , 5);


2. Run time polymorphism: 

Run time polymorphism is

achieved when the object's method is invoked at the

run time instead of compile time. It is achieved by

method overriding which is also known as dynamic

binding or late binding.

example

void add(int , int)

void add(int ,int)


1. Dynamic Binding

2. Message binding

3. Benifits of OOP’s

4. Application of OOP’s


1. Dynamic Binding

Binding refers to the linking of a procedure call to the

code to be executed in response to the call. 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 associated with a

polymorphic reference depends on the dynamic type of

that reference.

Consider the procedure “draw” in fig. 1.7. by inheritance,

every object will have this procedure. Its algorithm is,

however, unique to each object and so the draw

procedure will be redefined in each class that defines the

object. At run-time, the code matching the object under

current reference will be called


Message Passing

An object-oriented program consists of a set of

objects that communicate with each other. The

process of programming in an object-oriented

language, involves the following basic steps:

1. Creating classes that define object and their

behavior,

2. Creating objects from class definitions, and

3. Establishing communication among objects


◼ Objects communicate with one another by sending

and receiving information much the same way as

people pass messages to one another. The concept

of message passing makes it easier to talk about

building systems that directly model or simulate their

real-world counterparts.

A Message for an object is a request for execution of

a procedure, and therefore will invoke a function

(procedure) in the receiving object that generates the

desired results. Message passing involves specifying

the name of object, the name of the function

(message) and the information to be sent. Example:

Employee. Salary (name);


Benefits of OOP

OOP offers several benefits to both the program

designer and the user. Object-Orientation contributes

to the solution of many problems associated with the

development and quality of software products. The

new technology promises greater programmer

productivity, better quality of software and lesser

maintenance cost.

The principal advantages are:

1.Through inheritance, we can eliminate redundant code

extend the use of existing Classes.

2. We can build programs from the standard working

modules that communicate with one another, rather than

having to start writing the code from scratch. This leads

to saving of development time and higher productivity.

3. The principle of data hiding helps the programmer to

build secure program that can not be invaded by code in

other parts of a programs.

4. It is possible to have multiple instances of an object to

co-exist without any interference.

5. It is possible to map object in the problem domain to

those in the program.

6. It is easy to partition the work in a project based on

objects.

7. The data-centered design approach enables us to

capture more detail of a model can implemental form

8. Object-oriented system can be easily upgraded from

small to large system.

9. Message passing techniques for communication

between objects makes to interface descriptions

with external systems much simpler.

10. Software complexity can be easily managed.

 Application of OOP

Applications of OOP are beginning to gain importance in

many areas. The most popular application of objectoriented programming, up to now, has been in the area of

user interface design such as window. Hundreds of

windowing systems have been developed, using the OOP

techniques.

The promising areas of application of OOP include:

1. Real-time system

2. Simulation and modeling

3.Object-oriented data bases

4. Hypertext, Hypermedia, and expertext

5. AI and expert systems

6. Neural networks and parallel programming

7. Decision support and office automation systems

8. CIM/CAM/CAD system

 C++ HISTORY

C++ is an object-oriented programming language. It

was developed by Bjarner Stroustrup at AT&T Bell

Laboratories in Murray Hill, New Jersey, USA, in the

early 1980’s. Stroustrup, an admirer of Simula67 and

a strong supporter of C, wanted to combine the best

of both the languages and create a more powerful

language that could support object-oriented

programming features and still retain the power and

elegance of C.

The result was C++. Therefore, C++ is an extension

of C with a major addition of the class construct

feature of Simula67. Since the class was a major

addition to the original C language, Stroustrup initially

called the new language ‘C with classes’. However,

later in 1983, the name was changed to C++. The

idea of C++ comes from the C increment operator ++,

thereby suggesting that C++ is an augmented

version of C

C+ + is a superset of C. Almost all c programs are

also C++ programs. However, there are a few minor

differences that will prevent a c program to run under

C++ complier

The most important facilities that C++ adds on to C

care classes, inheritance, function overloading and

operator overloading. These features enable creating

of abstract data types, inherit properties from

existing data types and support polymorphism,

thereby making C++ a truly object-oriented language.

Application of C++

Mainly C++ Language is used for Develop Desktop

application and system software. Some application of

C++ language are given below.

For Develop Graphical related application like computer

and mobile games.

To evaluate any kind of mathematical equation use C++

language.

C++ Language are also used for design OS. Like window

xp.

Google also use C++ for Indexing

Few parts of apple OS X are written in C++ programming

language.

All major applications of adobe systems are

developed in C++ programming language. Like

Photoshop, Image Ready, Illustrator and Adobe

Premier.

Some of the Google applications are also written in

C++, including Google file system and Google

Chromium.

C++ are used for design database like My SQL.

Internet browser Firefox are written in C++

programming language


 Advantages of C++

1. Portability

C++ offers the feature of portability or platform

independence which allows the user to run the same

program on different operating systems or interfaces at

ease.


2. Object-oriented

One of the biggest advantages of C++ is the feature of

object-oriented programming which includes concepts

like classes, inheritance, polymorphism, data

abstraction, and encapsulation that allow code

reusability and makes a program even more reliable.


3. Multi-paradigm

C++ is a multi-paradigm programming language. The

term “Paradigm” refers to the style of programming. It

includes logic, structure, and procedure of the program.

Generic, imperative, and object-oriented are three

paradigms of C++.


4. Low-level Manipulation

Since C++ is closely associated with C, which is a

procedural language closely related to the machine

language, C++ allows low-level manipulation of data at

a certain level. Embedded systems and compiler are

created with the help of C++.


5. Memory Management

C++ gives the programmer the provision of total

control over memory management. This can be

considered both as an asset and a liability as this

increases the responsibility of the user to manage

memory rather than it being managed by the

Garbage collector. This concept is implemented with

the help of DMA (Dynamic memory allocation) using

pointers.


6. Compatibility with C

C++ is pretty much compatible with C. Virtually, every

error-free C program is a valid C++ program. Depending

on the compiler used, every program of C++ can run on

a file with .cpp extension.


7. Scalability

Scalability refers to the ability of a program to scale. It

means that the C++ program is capable of running on a

small scale as well as a large scale of data. We can

also build applications that are resource intensive.


◼ Disadvantages of C++

1. Use of Pointers

Pointers in C/C++ are a relatively difficult concept to

grasp and it consumes a lot of memory. Misuse of

pointers like wild pointers may cause the system to

crash or behave anomalously.


2. Security Issue

Although object-oriented programming offers a lot of

security to the data being handled as compared to

other programming languages that are not objectoriented, like C, certain security issues still exist due to

the availability of friend functions, global variables and,

pointers.


3. Absence of Garbage Collector

C++ gives the user complete control of managing

the computer memory using DMA. C++ lacks the

feature of a garbage collector to automatically filter

out unnecessary data.


4. Absence of Built-in Thread

C++ does not support any built-in threads. Threads is

a relatively new concept in C++ which wasn’t initially

there.


 Structure of c++ program

Programs are a sequence of instructions or statements. These

statements form the structure of a C++ program. C++ program

structure is divided into various sections, namely, headers,

class definition, member functions definitions and main

function.

Structure of c++

1. //The first program of c++

2. #include <iostream.h>

3.

4. int main()

5. {

6. Cout<<“hello”;

7. }

Line 1:

C++ introduces a new comment symbol // (double slash).

Comment start with a double slash symbol and terminate at the

end of the line. A comment may start anywhere in the line, and

whatever follows till the end of the line is ignored. Note that

there is no closing symbol.

The double slash comment is basically a single line comment.

Multiline comments can be written as follows:

 The C comment symbols /* ,  */ are still valid and are more

suitable for multiline comments. The following comment is

allowed:

/* This is an example of C++ program to illustrate some of its

features */

Line 2

1) Preprocessor directive:

The preprocessor directive is an instruction gives to compiler

before the execution. It is also known as the compiler directive.

The preprocessor directives start with the Hash symbol ‘#’.

These directives write at the start of the program. The

include preprocessor directive uses to add header files in the

program. For example:

#include<iostream.h>

This statement is showing that compiler includes the header

file iostream.h in the program. Other some examples are:

#include<stdio.h> and

#include<dos.h> etc

2) Header files:

Header files are the collection of library functions to perform

specific tasks. Different header files use for different purpose.

Each header file contains the predefined functions. We can add

more than one header files to the program. The extension of a

header file is ‘.h‘. The header files normally store in ‘include’. The

name of the header files writes in angle brackets.

Syntax:

#include<header_file_name>

Example:

#include<iostream.h>

Here,

‘iostream’ stands for input/output stream. It is used to add

predefined input/output functions and objects.

 Line 3:

A blank line.Blank lines have no effect on a program. They

simply improve readability of the code

Line 4:

The main() function is the starting point of a C++ program. When

the program run, the compiler execute the code from the main()

function. Each program must contain the main() function. If a

program does not contain the main() function, it cannot be

executed. Any number of statements written in the main()

function.


Lines 5 and 7:

{ and }The open brace ({) at line 5 indicates the beginning

of main's function definition, and the closing brace (}) at line 7,

indicates its end. Everything between these braces is the

function's body that defines what happens when main is called.

All functions use braces to indicate the beginning and end of

their definitions.

Line 6 : cout<<“hello”;

This statement has three parts: First, cout, which identifies

the standard character output device (usually, this is the

computer screen). Second, the insertion operator (<<), which

indicates that what follows is inserted into cout. Finally, a

sentence within quotes ("Hello world!"), is the content inserted

into the standard output. the statement ends with a semicolon (;

). This character marks the end of the statement, just as the

period ends a sentence in English. All C++ statements must end

with a semicolon character. One of the most common syntax

errors in C++ is forgetting to end a statement with a semicolon.

/*WAP FOR AVERAGE OF TWO NUMBERS*/

#include<iostream.h> // include header

file

#include< conio.h> //include header

file

int main()

{

Float number1, number2,sum, average;

Cin >> number1; // Read Numbers

Cin >> number2; // from keyboard

Sum = number1 + number2;

Average = sum/2;

Cout << ”Sum =

“ << sum << endl;

Cout << “Average =

“ << average << “\n”;

Return 0;

} //end of example

The output would be:

Enter two

numbers: 6.5 7.5

Sum = 14

Average = 7


Subject: OOP’s USING C++


1. C++ Character Set

2. Tokens in C++

3. C++ Keywords

4. C++ Identifiers

5. C++ Constants

6. C++ Strings

7. Special Symbols

8. C++ Operators


1. C++ Character Set

C++ Character set is basically a set of valid characters that

convey a specific connotation to the compiler. We use

characters to represent letters, digits, special symbols, white

spaces, and other characters.

The C++ character set consists of 3 main elements. They are:

Letters: These are alphabets ranging from A-Z and a-z (both

uppercase and lowercase characters convey different

meanings)

Digits: All the digits from 0 – 9 are valid in C++.

Special symbols: There are a variety of special symbols

available in C++ like mathematical, logical and relational

operators like 

+,-,

*

, /, \,

^

, %, !, @, #,

^

, &, (, ), [, ], ; and many more.


2. Tokens in C++

A token is the smallest element of a program that is meaningful

to the compiler. Tokens can be classified as follows:

Keywords

Identifiers

Constants

Strings

Special Symbols

Operators

keywords

Keywords are reserved words which have fixed meaning, and

its meaning cannot be changed. The meaning and working of

these keywords are already known to the compiler. C++ has

more numbers of keyword than C, and those extra ones have

special working capabilities.

Keywords in C++ refer to the pre-existing, reserved words, each

holding its own position and power and has a specific function

associated with it.

It is important to note that we cannot use C++ keywords for

assigning variable names as it would suggest a totally different

meaning entirely and would be incorrect.


Here is a list of keywords available in C++ according to the

latest standards:

asm Else New This Auto enum

throw operator Bool Break Case catch

Char class const Const_cast Continue default

delete do double Dynamic_cast Explicit Export

extern false float For Friend goto

if inline int Long Mutaable namespace

private public protected Register Interpret_cast Return

short signed sizeof static Static_cast struct switch template true try typedef typeid

typename union Unsigned using virtual Void

volitile Wchar_t while


4. C++ Identifiers


1. C++ allows the programmer to assign names of his own choice

to variables, arrays, functions, structures, classes, and various

other data structures called identifiers. The programmer may

use the mixture of different types of character sets available in

C++ to name an identifier.

Rules for C++ Identifiers

There are certain rules to be followed by the user while naming

identifiers, otherwise, you would get a compilation error. These

rules are:


First character: The first character of the identifier in C++

should positively begin with either an alphabet or an

underscore. It means that it strictly cannot begin with a

number.


2. No special characters: C++ does not encourage the use of

special characters while naming an identifier. It is evident that

we cannot use special characters like the exclamatory

mark or the “@” symbol.


3. No keywords: Using keywords as identifiers in C++ is strictly

forbidden, as they are reserved words that hold a special

meaning to the C++ compiler. If used purposely, you would get

a compilation error.


4. No white spaces: Leaving a gap between identifiers is

discouraged. White spaces incorporate blank spaces, newline,

carriage return, and horizontal tab.


5. Word limit: The use of an arbitrarily long sequence of identifier

names is restrained. The name of the identifier must not

exceed 31 characters, otherwise, it would be insignificant.


6. Case sensitive: In C++, uppercase and lowercase characters

connote different meanings.

Identifier Name

5th_element Invalid

_delete Valid–

–school.fee Invalid

register[5] Invalid

Student[10] Valid–

employee name Invalid

perimeter() Valid

CONSTANT

 Constants:

Constants are also like normal variables. But, the only

difference is, their values can not be modified by the program

once they are defined. Constants refer to fixed values. They are

also called literals.

Constants may belong to any of the data type.

Syntax:

const data_type variable_name;

(or) const data_type *variable_name;

Types of Constants:

Integer constants – Example: 0, 1, 1218, 12482

Real or Floating-point constants – Example: 0.0, 1203.03,

30486.184

Octal & Hexadecimal constants – Example: octal: (013 )

8

= (11)

10,

Hexadecimal: (013)

16

= (19)

10

Character constants -Example: ‘a’

,

‘A’

,

‘z’

String constants -Example: “RAJENDRA PANCHOLI"

1 Comments

Post a Comment

Previous Post Next Post