oop using c++ 1. 2 abstract data types how to accomplish the task??? requirements details input,...

35
OOP using C++ 1

Upload: emerald-reed

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

1

OOP using C++

Page 2: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

2

Page 3: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

3

Abstract data types

• How to accomplish the task???• Requirements• Details• Input, output, process• Specify each task in terms of input an output

Page 4: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

4

Encapsulation

• Class.• Object.• Private.• Public.• Inside a class : variable and operations -

– Variables (data) are called data members.– Functions (operations ) are called member functions,

function members or methods.

• This combining of data and operations is called Encapsulation.

Page 5: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

5

Introduction to Classes• An “object” is an instantiation

of a class.

• Information Hiding:• Private members can only be

accessed by methods in its class.

• Public members can be accessed by methods in any class.

• Development issues:• What the program should do?• How it could be done?

Page 6: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

6

Example 1

class Laptop{

private:int cpu_speed;

public:int display () { return cpu_speed; }void setup (int new_speed) { cpu_speed = x; }

};

Page 7: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

7

Constructor class Laptop

{

private:

int cpu_speed;

public:

Laptop () // Zero-parameter constructor

{ cpu_speed = 0; }

Laptop (int initial_speed) // One-parameter

{ cpu_speed = initial_speed; }

int display () { return cpu_speed; }

void setup (int new_speed) { cpu_speed = new_speed; }

};

Page 8: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

8

Example 2#include <iostream>using namespace std;class C { public:

char getchar [20]; int intval;C (char *s="", int i=0) { strcpy(getchar,s); intval=i;}void fun(int i, char *s) {cout<<i<<" "<<s<<endl;}

};

int main () { C obj1("IN", 10); obj1.fun(50,”DATA”); return 0; }

cout<<&s<<endl;cout<<*s<<endl;

Page 9: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

9

Inheritance

Objects do not have to be

instantiations of a single

class.

Page 10: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

10

Pointer / new / delete • A pointer is an address in memory.• The & operator (ampersand) changes a thing into a

pointer (the address of the thing).• The * operator changes a pointer into a thing.

{ int x;int *x_ptr; x = 5;

cout<<x<<endl;cout<<&x<<endl;x_ptr = &x;*x_ptr = 10;cout<<x<<endl;cout<<&x<<endl;cout<<*x_ptr<<endl;cout<<&x_ptr<<endl;}

{ int x;int *x_ptr; x = 5;

cout<<x<<endl;cout<<&x<<endl;x_ptr = &x;cout<<x<<endl;cout<<&x<<endl;*x_ptr = 10;cout<<*x_ptr<<endl;cout<<&x_ptr<<endl;}

Page 11: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

11

Page 12: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

12

Page 13: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

13

Pointers an Arrays

• Pointer refers to a block of memory that holds on integer

• Array “is a pointer” refers to a block of memory that holds more than one integer

Page 14: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

14

Pointers an copy constructors (Discussion)

Page 15: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

15

Pointers to ObjectsDell is a pointer to a

Laptop. To allocate space, a “new” operator must be used. To reclaim the space use the “delete” operator. Use the “→ ” operator to access the methods.

class Laptop{ private:

int cpu_speed; public: Laptop (int initial_speed = 0)

{ cpu_speed = initial_speed; } int display ()

{ return cpu_speed; } void setup (int new_speed)

{ cpu_speed = new_speed; }};int main() {Laptop *dell = NULL;dell = new Laptop(2);cout<< dell->display();dell->setup(3);cout<< dell->display();delete dell; return 0; }

Page 16: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

16

Pointer to functions (self study)

Page 17: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

17

Polymorphism

different objects

Page 18: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

18

Pointers to base class

Page 19: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

Virtual membersA member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare a member of a class as virtual, we must precede its declaration with the keyword virtual:class Base-class { protected: public:

virtual type-in-derived-class function-in-derived-class() { return (0); }

}; 19

Page 20: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

Virtual members cont’d

cout << object-to-derived-class.function-in-derived-class() << endl;

Rewriting

cout << object-to-base-class -> Virtual-function-in-derived-or-base-class() << endl;

20

Page 21: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

21

Example

Page 22: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

Note • The member function area() has been declared as

virtual in the base class because it will be redefined in each derived class.

• If the virtual keyword is removed from the declaration of area() within base class, and then the program is implemented, the result will be 0 for the three polygons instead of 20, 10 and 0.

• A class that declared or inherited a virtual function is called a polymorphic class.

22

Page 23: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

23

Abstract Classes and Pure virtual Functions

• A class is made abstract by declaring one or more of its virtual functions to be "pure." A pure virtual function is specified by placing "= 0" in its declaration, as in:

• Example: virtual void draw() const = 0; // pure virtual function

• The "=0" is known as a pure specifier. Pure virtual functions do not provide implementations.

Page 24: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

24

Abstract Classes and Pure virtual Functions (cont’d)

• The difference between a virtual function and a pure virtual function is that a virtual function has an implementation and gives the derived class the option of overriding the function; by contrast, a pure virtual function does not provide an implementation and requires the derived class to override the function.

• Pure virtual functions are used when it does not make sense for the base class to have an implementation of a function, but the programmer wants all concrete derived classes to implement the function.

Page 25: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

Abstract base classes cont’d

// abstract class Cpolygonclass Cpolygon

{ protected: int width, height;public:void set_values (int a, int b) { width=a; height=b; }virtual int area () =0;

};This type of function is called a pure virtual function, and all classes that contain at least one pure virtual function are abstract base classes 25

Page 26: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

Abstract base classes cont’d

• But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take advantage of all its polymorphic abilities. Therefore a declaration like:

CPolygon poly;

would not be valid for the abstract base class we have just declared, because tries to instantiate an object. Nevertheless, the following pointers:

CPolygon * ppoly1;CPolygon * ppoly2;

would be perfectly valid.26

In main function

Page 27: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

27

Example

Page 28: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

28

Dynamic binding or Late binding (cont’d)

• If the program invokes a virtual function through a base-class pointer to a derived-class object (e.g., shapePtr->draw ()), the program will choose the correct derived-class draw function dynamically (i.e., at execution time) based on the object type not the pointer type.

• Choosing the appropriate function to call at execution time (rather than at compile time) is known as dynamic binding or late binding.

Page 29: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

29

Dynamic binding or Late binding (cont’d)

• When a virtual function is called by referencing a specific object by name and using the dot member-selection operator (e.g., squareObject.draw()), the function invocation is resolved at compile time (this is called static binding) and the virtual function that is called is the one defined for (or inherited by) the class of that particular object this is not polymorphic behavior. Thus, dynamic binding with virtual functions occurs only off pointer handles.

Page 30: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

30

The Standard Template Library

• Containers• Iterates• Algorithms

Page 31: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

31

CONTAINER

• is a class, a data structure, or an abstract data type (ADT) whose instances are collections of other objects. In other words; they are used for storing objects in an organized way following specific access rules.

• a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently

Page 32: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

32

stack data structure

• could be defined by three operations: – push, that inserts some data item onto the

structure– pop, that extracts an item from it – peek, that allows data on top of the structure to

be examined without removal.

Page 33: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

33

ITERATES

• is an object used to reference an element stored in container.

Page 34: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

34

ALGORITHMS

• The STL provides 70 generic functions, caled algorithms, that can be applied to the STL containers and to arrays

• example:

• Indicating the position of element e1 in the rang i1 up to excluding i2.

Page 35: OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input

35

Read only