OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input,...

Preview:

Citation preview

1

OOP using C++

2

3

Abstract data types

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

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.

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?

6

Example 1

class Laptop{

private:int cpu_speed;

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

};

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

};

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;

9

Inheritance

Objects do not have to be

instantiations of a single

class.

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

11

12

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

14

Pointers an copy constructors (Discussion)

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

16

Pointer to functions (self study)

17

Polymorphism

different objects

18

Pointers to base class

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

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

21

Example

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

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.

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.

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

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

27

Example

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.

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.

30

The Standard Template Library

• Containers• Iterates• Algorithms

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

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.

33

ITERATES

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

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.

35

Read only

Recommended