oops concepts in c++ documentation

27
OOPS Concept in C++ programming Language 1 OOPS Concept in C++ Programming Language Farouq Umar Idris Introduction to Programming Logic (CIS142) 30/09/2013 Prof. R.K Sharma

Upload: farouq-umar

Post on 19-Jan-2015

1.019 views

Category:

Education


2 download

DESCRIPTION

complete documentation

TRANSCRIPT

Page 1: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 1

OOPS Concept in C++ Programming Language

Farouq Umar Idris

Introduction to Programming Logic (CIS142)

30/09/2013

Prof. R.K Sharma

Page 2: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 2

ABSTRACT

OOPS concepts (object, class, encapsulation, abstraction, polymorphism and inheritance) have

tremendous programming consequences for programmers using structure languages. This project

shows how important oops concepts are in C++ and what the major difference between C

programming and C++ is. More importantly, it explains where and how to use the concepts (OOPS

concepts) by giving few self-explanatory examples. The entire project is solely based on the use of

OOPS concept in C++ and the difference between C++ and C programming.

Page 3: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 3

Table of Contents

Acknowledgments...................................................................................................................... 4

Scope of project ......................................................................................................................... 4

Chapter 1: Introduction .............................................................................................................. 5

Objectives ............................................................................. Error! Bookmark not defined.

Definition ............................................................................................................................... 5

Difference between C Programming and C++ Based On Oops Concepts ............................. 6

CHAPTER 2: OPPS COMPONENTS ............................................. Error! Bookmark not defined.

Object ..................................................................................................................................... 7

Class ..................................................................................................................................... 10

Example of class and object ................................................................................................. 11

Encapsulation ....................................................................................................................... 12

Data abstraction .................................................................................................................... 15

Difference between Encapsulation and Abstraction in Oops ............................................... 16

Conclusion ............................................................................................................................... 17

Page 4: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 4

ACKNOWLEDGMENTS

As a work group that we use to do in the end of a quarter, we can’t finish our work or write

our final project without expressing our great gratitude to MAII Stratford-university, that

throughout its managerial staff, by the work they are doing for education. Especially we give

also thanks to Professor R.K Sharma who led us during the course of introduction to

Programming and we come to end by giving also thanks to the entire group Member

Muhunga David Eleca, Farouq Umar Idris and Faso Mushigo Heritier.

Page 5: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 5

1.0 INTRODUCTION

Definition

C programming is a general-purpose programming language developed between 1969 and

1973. Like most imperative languages in the ALGOL tradition, C has facilities for structured

programming and allows lexical variable scope and recursion, while a static type system

prevents many unintended operations. Its design provides constructs that map efficiently to

typical machine instructions, and therefore it has found lasting use in applications that had

formerly been coded in assembly language, most notably system software like the Unix

computer operating system.

C++ is a programming language that is general purpose, statically typed, free-form, multi-

paradigm and compiled. It is regarded as an intermediate-level language, as it comprises both

high-level and low-level language features. It was developed in 1979s and was originally

named C with Classes, adding object oriented features, such as classes, and other

enhancements to the C programming language.

Page 6: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 6

DIFFERENCE BETWEEN C PROGRAMMING AND C++ BASED ON OOPS

CONCEPTS

First off, C++ is a non-procedural language but C programming is a totally procedural

language that doesn’t use the concepts of virtual functions in its programs. Polymorphism and

overloading concepts are also not applicable in C programming language but in C++

programming, virtual functions, polymorphism as well as operator overload are used. The

main difference between C programming and C++ is the concepts of OOPs that are used in

C++ and not in C programming.

Page 7: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 7

2.0 OPPS CONCEPTS

2.1 Object

In fact, we are surrounded by objects. In fact, everything we know (almost) can be

considered as an object. The idea of object-oriented programming is handled in its source

code elements that are called "objects."

Imagine you want to create a complex application with many variables and functions that can

be called between them. At first view, it'll look like an experience of a scientist in which we

can’t understand anything as showed in the image below.

Sure you have created a set of code that is working but imagine that you want to publish it on

internet for everyone who wants to use it without to code again, they must be expert for

understanding directly what you have done, what function to call first, what value to send to

each function and so one. To discover all these things will take surly time, so that object

Page 8: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 8

oriented programming became important, that is to say, all the staff represented by functions

and variables in our case will be made in a kind of cube; and this cube represent an object for

us. Let us illustrate by the image bellow.

Here, part of the cube (that represents an object) was deliberately set transparency to show

you that our chemical flasks (That represent functions and variables of our complex

application) are located inside the cube. But in reality, the cube is completely opaque; you

can’t see anything inside like represented in the next image.

Page 9: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 9

Clearly: object-oriented programming so is to create the source code (possibly complex), but

that is masked by placing it inside a cube (representing an object) through which we see

nothing. For the programmer who will use it, he or she will work with a much simpler object

than before: he or she will just have to press buttons and to use it do not need a degree in

chemistry.

Briefly, we can define an object as a collection of functions and variables that has a name.

Page 10: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 10

2.2 CLASS

Class is like a plan that describe object. We can compare it for example to a plan to build a

house.

The object is an instance of the class, that is to say, a concrete implementation of the plan. In

the above example, the object is the house. You can create several houses based on a

construction plan. We can therefore create multiple objects from a class.

Page 11: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 11

Example of class and object

#include <iostream.h>

#include <conio.h>

Class Personage // creation of a class

{

int main()

{

Personnage david, goliath;

//Creation of 2 objets : david et goliath

goliath.attack(david); //goliath attack david

david.takePotionoflife(20); //david will take 20 of life in drinking a potion

goliath.attack(david); //goliath attack again david

david.attack(goliath); //david will do same

return 0;

}

}

Page 12: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 12

2.3 ENCAPSULATION

Encapsulation can be defined as a method in which data and function are combined inside a

class. This process hides data and also protects it from being accessed from outside world or

unauthorised user. To access it, the user has to go through the functions within the class.

Encapsulation is a concept of object oriented programming which binds data and functions

together by manipulating the data and ends up keeping both the data and functions safe from

the outside world. With data encapsulation, data hiding came into existence. C++ generally

supports all properties of encapsulation and hiding data by the creation of a user defined

types which are referred to as classes. All items in C++ by default are private.

class Box

{

public:

doublegetVolume(void)

{

return length * breadth * height;

}

private:

double length; // this is the Length

double breadth; // this is the Breadth

double height; //and this, the Height

};

Page 13: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 13

This means that access is restricted to only members of the class Box not to any other part of

a programme but just the class Box. By doing this, encapsulation is achieved. In order to

make them accessible by other members, we need to declare them public.

Example:

In C++ program where a class with public and private members are implemented are example

of example of data encapsulation and data abstraction.

#include <iostream.h>

class Adder{

public:

//this side of the programme is accessed by all

Adder(int x = 0)

{

total = x;

}

// this interface is all accessible by outside world

voidaddNum(int no)

{

total += no;

}

intgetTotal()

{

return total;

};

private:

// from here, data are hidden from outside world

Page 14: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 14

int total;

};

int main( )

{

Adder y;

y.addNum(10);

y.addNum(20);

y.addNum(30);

cout<< "Total " <<y.getTotal() <<endl;

return 0;

}

Page 15: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 15

2.4 DATA ABSTRACTION

Data abstraction can be considered as the process of showing only the essential part of a

program to the outside world and hiding the details. It is a process of representing only the

needed information within a program and leaving the details out. Data abstraction can be

considered also as the process or a technique used in separation of interface and

implementation.

For instance, in our television set that we use almost every day. We know how we can turn it

on and off, adjust the volume of the speaker and other external components of the television

set. But we have no idea how things inside the TV set work. How signals are sent and

received from where they are sent and how it modulates the signals. All this are not known by

us.

By this, it is seen that a television set is meant to separate its external interface from internal

implementation and we can do what we want with only the external part of it like change

channel and increase or decrease volume etc. This is a typical example of abstraction.

In terms of C++, classes are used to provide desired level of data abstraction. They provide

great level of public methods for the outside world or user to play with as well as

manipulating the object data and functionality of the object that is stating without knowing

how the class was implemented.

Page 16: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 16

DIFFERENCE BETWEEN ENCAPSULATION AND ABSTRACTION IN OOPS

The two are a great deal in object oriented programming concepts and at some point are

interrelated.

1. Encapsulation simply means to hide something like in a capsule or something of that

nature. Therefore it is more like wrapping or simply hiding methods and properties.

Programmers use encapsulation in hiding data and codes within a single unit for the

purpose of protecting the data from outside world. Example of encapsulation is classes

while abstraction is the process of showing only what needs to be shown and hiding

details to the user. Abstraction as the name implies means abstract form of a thing. The

uses of abstraction arise in programming when we need to create an abstract class where

the abstract class represents properties of a class and methods.

2. Encapsulation is done by the use of protected and private access modifiers while

abstraction is done using abstract class and interface.

3. Integrity are enforced in OOPs by the use of encapsulation (i.e. making sure appropriate

use of data is done and in an appropriate manner), it is done by preventing programmers

access to data in an unintended manner.

4. Differentiation by example

Encapsulation

Class Encapsulation

{

Private float division;

Public float division

{

get { return division; }

Page 17: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 17

set {division = value;}

}

}

Abstraction

abstract class Abstraction

{

public abstract void do Abstraction();

}

public class AbstractionImpl: Abstraction

{

public void doAbstraction()

{

// its Implemented it

}

}

Page 18: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 18

2.5 INHERITANCE

Inheritance is one of main techniques used in OOOPS concept, that an Object gain the

proprieties of another without resetting to create a well-defined categories or class. This

technique also supports the hierarchical classification in which each object would not need to

be redefined by all its characteristics explicitly. It prevents the programmer's unnecessary

work and this is the concept of inheritance that does this job. Heritage gives us the

opportunity to object to inherit only the qualities that make it unique in its class. Generally it

can inherit all the attributes of its parent.

As we know there is:

1. Public Inheritance:

Is when a class is deriving from a public base class, the public members of the base class will

become the public members of the derived class and protected members of the base class

become protected members of the derived class. A base class's private members are never

accessible directly from a derived class, but can be accessed through calls to the public

and protected members of the base class.

2. Protected Inheritance:

Is when a deriving from a protected base class, public and protected members of the base

class become protected members of the derived class.

3. Private Inheritance:

Is when we deriving from a private base class, the public and the protected members of the

base class become private members of the derived class.

Page 19: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 19

An example for better understanding.

Let specify that a class derives from another one, the class you create gain all the features of

the base class. If the derived class has some extra features but is otherwise the same as the

base class, this is a good oncoming.

access public protected private

Same class yes yes yes

Derived

classes

yes yes no

Outside classes yes no no

• • • •

Page 20: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 20

#include <iostream>

// Base class

class Shape

{

public:

void setWidth(int w)

{

width = w;

}

void setHeight(int h)

{

height = h;

}

protected:

int width;

int height;

};

// Derived class

class Rectangle: public Shape

{

Page 21: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 21

public:

int getArea()

{

return (width * height);

}

};

int main(void)

{

Rectangle Rect;

Rect. setWidth(5);

Rect. setHeight(7);

// Print the area of the object.

cout << "Total area: " << Rect.getArea() << endl;

return 0;

}

Page 22: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 22

2.6 POLYMORPHISM

In 1967 Polymorphism was identified by Christopher Strachey then developed by Hindley

and Milner. The important thing is not the understanding of it implementation but how this

concept works. C++ programming language provide us two manners of retrieving the objects

and expression when the program is in the running process. Those two ways are Run time

and Compile time. The interesting fundamental idea in polymorphism is at the compile time,

the compiler doesn’t know which function to call before. But it’s done at time of running

(Run time).by other way the run time is called as late binding.

Example where a base class has been derived by other two classes:

#include <iostream>

class Shape {

protected:

int width, height;

public:

Shape( int a=0, int b=0)

{

width = a;

height = b;

}

int area()

{

Page 23: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 23

cout << "Parent class area :" <<endl;

return 0;

}

};

class Rectangle: public Shape{

public:

Rectangle( int a=0, int b=0)

{

Shape(a, b);

}

int area ()

{

cout << "Rectangle class area :" <<endl;

return (width * height);

}

};

class Triangle: public Shape{

public:

Triangle( int a=0, int b=0)

{

Page 24: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 24

Shape(a, b);

}

int area ()

{

cout << "Rectangle class area :" <<endl;

return (width * height / 2);

}

};

// Main function for the program

int main( )

{

Shape *shape;

Rectangle rec(10,7);

Triangle tri(10,5);

// store the address of Rectangle

shape = &rec;

// call rectangle area.

shape->area();

// store the address of Triangle

Page 25: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 25

shape = &tri;

// call triangle area.

shape->area();

return 0;

}

Page 26: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 26

3.0 CONCLUSION

OOPS is undoubtedly one of the most complicated programming techniques to explain. In

fact, it's a whole new way to see the programming rather than ‘technical’. Before

understanding OOPS, you must first understand what programming is.

At this time, the basic definition of the programming was as follows: a sequence of logical

instructions followed by the computer. In languages Object Oriented programming, the

emphasis is on data, or 'objects' used and how the programmer manipulates it. Actually with

OPPS concept, the program is now a solution to any problem that may be encountered, but

whatever it is developed in relation to the objects that define your problem and using the

functions associated with these objects.

Page 27: Oops concepts in c++ documentation

OOPS Concept in C++ programming Language 27

Reference

1. Mark Allen Weiss, 2006, Data structures and algorithm analysis in C++ 3rd edition

2. Henry C. Lucas jr. Information Technology for Management 7th Edition

3. www.tutorialspot.edu

4. www.cplusplus.com