Top Banner
IN THE NAME OF GOD OOPin C++ Maysam Rajaati Farid Bekran 1 11/27/2008 Tabriz University - Iran
22

OOP in C++

Nov 15, 2014

Download

Documents

faridprogrammer

this is a presentation on object oriented programming in C++...
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OOP in C++

IN THE NAME OF GOD

OOPin C++Maysam Rajaati Farid Bekran

111/27/2008

Tabriz University - Iran

Page 2: OOP in C++

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

Page 3: OOP in C++

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

Page 4: OOP in C++

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

Page 5: OOP in C++

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

Page 6: OOP in C++

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…

Page 7: OOP in C++

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 }…

Page 8: OOP in C++

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

Page 9: OOP in C++

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

Page 10: OOP in C++

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

Page 11: OOP in C++

11

INHERITANCE

Single Inheritance

A

B C

Multiple inheritance

A

B C

Page 12: OOP in 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

Page 13: OOP in C++

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

Page 14: OOP in C++

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.

Page 15: OOP in C++

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 }}

Page 16: OOP in C++

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.

Page 17: OOP in C++

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 …

Page 18: OOP in C++

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

Page 19: OOP in C++

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.

Page 20: OOP in C++

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

Page 21: OOP in C++

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

Page 22: OOP in C++

22

????