Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 OOP using C++ 1

2 2

3 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input an output 3

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. 4

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? 5

6 Example 1 class Laptop { private: int cpu_speed; public: int display () { return cpu_speed; } void setup (int new_speed) { cpu_speed = x; } }; 6

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

8 Example 2 #include 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; } 8 cout<<&s<<endl; cout<<*s<<endl;

9 Inheritance Objects do not have to be instantiations of a single class. 9

10 Pointer / new / delete 10 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; *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;} { 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 11

12 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 13

14 Pointers an copy constructors (Discussion) 14

15 Pointers to Objects Dell 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. 15 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 display(); dell->setup(3); cout display(); delete dell; return 0; } 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 display(); dell->setup(3); cout display(); delete dell; return 0; }

16 Pointer to functions (self study) 16

17 Polymorphism different objects 17

18 18 Pointers to base class

19 Virtual members A 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

20 Virtual members cont’d cout << object-to-derived-class.function-in- derived-class() << endl; Rewriting cout Virtual-function-in- derived-or-base-class() << endl; 20

21 21 Example

22 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. 23

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. 24

25 Abstract base classes cont’d // abstract class Cpolygon class 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

26 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 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. 28

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. 29

30 The Standard Template Library Containers Iterates Algorithms 30

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 31

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. 32

33 ITERATES is an object used to reference an element stored in container. 33

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. 34

35 Read only 35


Download ppt "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."

Similar presentations


Ads by Google