support for object-oriented programming (oop) in c++

16
Support for OOP in C++

Upload: ameen-shaarawi

Post on 22-Jan-2017

53 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++

Page 2: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++▸ General Characteristics:

1- Evolved from SIMULA 67. • Simula is a name for two simulation

languages, Simula I and Simula 67, Developed in the 1960s, by Ole-Johan Dahl and Kristen Nygaard.

• Simula is considered the first Object-Oriented Programming language.

• Simula provided the framework for many of the OOP languages today.

2- Most widely used OOP language.

2

Page 3: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++3

3- Mixed typing system (Retains C types, adds classes).• Static typing: A variable has a single type associated with it throughout it is

life at run time (Types of all variables/expressions are determined at compile time.

• Dynamic typing: Allow the type of a variable. As well as it is value, to change as the program runs.

4- Constructors and Destructors (Implicitly called when objects are created/cease to exist).• Constructor is a special member function of a class that is executed

whenever we create new objects of that class.A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.• Destructor is a special member function that is called when the lifetime of an

object ends.The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.

Page 4: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++4

5- Elaborate access controls to class entities.

Private Protected Public

Same Class YES YES YES

Derived Class NO YES YES

Friend Class YES YES YES

Other class NO NO YES

Access Modifiers

Access Locations

“Access controls”

Page 5: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++5

Inheritance• A class need not be the subclass of any class• Access controls for members are

– Private (visible only in the class and friends) (disallows subclasses from being subtypes)

– Public (visible in subclasses and clients)– Protected (visible in the class and in subclasses, but not clients)

Page 6: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++6

Public base classes in C++ has the class declaration: class < derived > : public < base > { < member-declarations > };

Private base classes in C++ has the class declaration:

class < derived > : private < base > {

< member-declaration > };

Page 7: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++7

Inheritance example in C++ : class base_class { private: int a; float x; protected: int b; float y; public: int c; float z;};

class subclass_1 : public base_class { … };//b and y are protected and c and z are public

class subclass_2 : private base_class { … };

//b, y, c, and z are private, and no derived class of//subclass_2 has access to any member of base_class

//Note that a and x are not accessible in either//subclass_1 or subclass_2

class subclass_3 : private base_class { base_class :: c; }//Instances of subclass_3 can access c.

An object is an instance of a class, and

may be called a class instance or class object.

Page 8: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++8

In addition, the subclassing process can be declared with access controls (private or public), which define potential changes in access by subclasses.

> Private derivation: inherited public and protected members are private in the subclasses (Does not represent an is-a relationship (Inheritance).

> Public derivation: public and protected members are also public and protected in subclasses.

In Private derivation: - By default, all members inherited from < base > become private members of <derived > .

• Privacy principle: - The private members of a class are accessible only to member functions of the class. - Functions in a derived class cannot access the private members of it’s base class.

Page 9: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++9

Multiple inheritance is supported> If there are two inherited members with the same name, they can both be referenced using the scope resolution operator.> Multiple inheritance allows a new class to inherit from two or more classes.

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

Page 10: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++10

Common problem with multiple Inheritance

> Diamond ProblemIs an ambiguity that arises when two classes B and C inherited from class A, and class D inheritedfrom both class B and class C.

If a method in D calls a method defined in A , and B and C overridden that method differently, then from which class does it inherit: B or C?

D

B C

A

Page 11: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++11

Dynamic Binding> Method can be defined to be virtual, which means that they can be called through polymorphic variables and dynamically bound to messages.

> A pure virtual function has no definition at all. “It cannot be called, unless it is redefined in the derived class.”> A class that has at least one pure virtual function is an abstract class. “An abstract class cannot be instantiated.”

Page 12: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++12

class shape{ public: virtual void draw()=0;};

Dynamic Binding Example

class rectangle : public shape{ public: void draw() { cout<<"rect \n"; }};

class square : public rectangle{ public: void draw() { cout<<"square \n"; }};

virtual void draw()=0;

“=0” in function definition indicates a pure virtual function.

Page 13: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++13

Dynamic Binding Example

int main(){ square *sq = new square; rectangle *rect = new rectangle; shape *ptr_shape = sq; ptr_shape -> draw(); //Square rect ->draw(); //Rect rect = sq; rect ->draw(); //Square square sq2; rectangle r2 = sq2; r2.draw(); //Rect}

Even thought it contains a square

Page 14: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++14

Evaluation> C++ provides extensive access controls (unlike other OO language such as Smalltalk).> C++ provides multiple inheritance.> In C++, the programmer must decide at design time which methods will be statically bound and which must be dynamically bound. - Static binding is faster! - Design Decision may be wrong, requiring change later. - Dynamic binding in C++ is faster than Smalltalk.

Page 15: Support for Object-Oriented Programming (OOP) in C++

Support for OOP in C++14

Static binding

 The choice of which function

to call in a class that employs

inheritance is made at compile

time.

Dynamic binding

The choice is made at run

time.

“If you use dynamic binding, The program will look up which function to use in a virtual function table, which takes a little time, making dynamic binding slower.”

Page 16: Support for Object-Oriented Programming (OOP) in C++

THANKS!Any questions?

Prepared by: Ameen [email protected]