OOP in C++

Post on 15-Nov-2014

194 views 13 download

Tags:

description

this is a presentation on object oriented programming in C++...

Transcript of OOP in C++

IN THE NAME OF GOD

OOPin C++Maysam Rajaati Farid Bekran

111/27/2008

Tabriz University - Iran

2

TABLE OF CONTENTS

1. History2. Philosophy of C++ & increments of C3. Classes in C++4. Objects in C++5. Access Specifiers6. Class & Object memory allocation7. Static data (In classes)8. Inheritance(Single)9. Inheritance(multiple)10.INCLUSIVE CLASSES11.POLYMORPHISM12.COMPILERS

3

HISTORY It was developed by Bjarne Stroustrup in1979 at Bell Labs. As an enhancement to the C programming language and originally named "C with Classes". It was renamed to C++ in 1983.

C++

C Simula CLUSmallTalkAlgol68ADA83 ML

C# JAVA DPerlPHPADA95

4

PHILOSOPHY OF C++ & INCREMENTS OF C C++ is designed to be a statically typed general-purpose language that is as efficient and portable as C

C++ is designed to directly and comprehensively support multiple programming styles (procedural programming, data abstraction, object-oriented programming, and generic programming)

C++ avoids features that are platform specific or not general purpose

CC++

Templates

class

Operator overloading

Virtual functions

Multiple inheritance

Exception handling

5

CLASSES IN C++ Classes are User defined types. Classes are for Encapsulate our data. Classes hold both data and function on data.

class CPolygon{ private: int Width; int Height; // You can remove “private”

public: Cpolygon() // Has no return value { } int Area()

{ } void Draw()

{ } ~Cpolygon() // Has no Parameter , Has no return value { } };

CPolygon

6

OBJECTS IN C++

Some Data

Definition of class

Some Data

1000

Some Data

10230

Objects of Class

Definition of a class only is the class Specification. Object of a class allocate real memory for members.

Example:…Cpolygon obj1; // call the user defined constructorObj1.Draw(); // draw the obj1 to screencout << Obj1.Area(); //prints the area of polygon obj1

// if scope ends the destructor called…

7

ACCESS SPECIFIERS

private

public

protected

Cpolygon objB

Class Cpolygon

NO

T A

LLOW

ED

Example 1 :…Cpolygon obj1;cout << Obj1.Width; // ERROR!obj.Draw(); //True! …

Example 2 :…class CPolygon{ void Draw() { cout << Width << Height; // True }…

8

CLASS & OBJECT MEMORY ALLOCATION

Data 1

Data 2

Data 1

Data 2

Data 1

Data 2

Function 1

Function 2

Object 1 Object 2 Object 3

9

STATIC DATAS(IN CLASSE)

Data 1

Data 2

Normal data

Data 3

Data 4

Static data

Object 1Object 2

Object 3

Data 1

Data 2

Normal data

Data 1

Data 2

Normal data

Class A{… //public Static int x;//declaration… };int A::x = 0;//definitionMain(){ cout << A::x; // access to static member …}

Static members use the access Secifiers of other members.

9

10

STRUCTURES & CLASSES

Structure members are by default public but in class members are by default private.

Structures inherited publicly by default but classes inherited privately.

Difference between class and struct

11

INHERITANCE

Single Inheritance

A

B C

Multiple inheritance

A

B C

12

INHERITANCE(SINGLE)

Feature A

Feature B

Feature C

Base Class

Feature A

Feature B

Feature CFeature D

Derived ClassD

efined in B

ase Class

Defined in

Derived C

lass

Arrow means Derived from

class CPolygon{ protected : int Wedth; int Height; public: int Area()

{ } int Draw()

{ } };class CRectangle : public CPolygon{ };

CPolygon

CRectangle

13

INHERITANCE(SINGLE)Access Specifier

private

public

protected

private

public

protected

Base objB

Class Derived

Derived objD

Class Base

Base class has no access to derived class

14

INHERITANCE(SINGLE)Public & Private Inheritance

Class C: private A

B objB

Class B : public A

private

public

protected

class A

private

public

protected

A objA

private

public

protected

C objC

NO

T A

LLO

WE

D

Public & Private Inheritance are methods that control Objects accessibility to parent public members.

15

INHERITANCE(SINGLE)Overloading functions in base and derived class

Inplemention…CRectangle objREC; Cpolygon objPOL;objREC.Draw(); objPOL.Draw();…

Class Cpolygon { … public: void Draw() { cout << “Drawing Cpolygon”<< “\n”;}};Class CRectangle : public Cpolygon{ … public: void Draw() { // use the suitable draw steps }}

16

INHERITANCE(MULTIPLE)

Base class A Base class B

Derived class C

class A{ };class B{ };class C : public A , public B{ };

Overloading functions in this kind of inheritance is like single inheritance.

17

INHERITANCE(MULTIPLE)Ambiguity

class A{ public: void show() { cout << “in A class”;}; };class B{ public: void show() { cout << “in B class”;}; };class C : public A , public B{ // this class has no method with show() signature };

Implementation :…C object;object.show(); // compile errorObject.A::show(); // TrueObject.B::show(); // True …

18

INCLUSIVE CLASSES

Class Wing

Class Bird

Class Wing

Class Hen

Class Wing

class wing { };class bird{

…wing obj; //bird has wing…

};class hen : public bird{ }; // hen derived from bird

19

POLYMORPHISM

Polymorphism

Dynamic Static

Uses function overloading Compile time

Uses virtual function members Run-time

class Cpolygon{ … virtual void Draw() { } …}Cpolygon* ptrs[100];… // fill the ptrs[100]for(int i=0 ; i<100 ; i++) ptrs[i] -> draw();

If you don’t use virtual keyword on base class function, call line of “for” statement will call the base class function in every step.

20

COMPILERS

Dev C++ (open source) - Free Microsoft Visual studio 2003 / 2005 / 2008 / 2010 - commercial Borland C++ builder - commercial Borland Turbo C++ explorer 2006 (Japanese , French , English ) - Free Apple C++ IBM C++ Intel C++ for Linux Sun Studio

21

REFERENCES

1. Lefor, Robert W. Object Oreinted Programming in C++, 3rd ed.2. pratt, terans w. Programming language.3. www.wikipedia.com4. www.cplusplus.com

22

????