360 likes | 386 Views
OOP using C++. Abstract data types. How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input an output. Encapsulation. Class. Object. Private. Public. Inside a class : variable and operations -
E N D
Abstract data types • How to accomplish the task??? • Requirements • Details • Input, output, process • Specify each task in terms of input an output
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.
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?
Example 1 class Laptop { private: intcpu_speed; public: intdisplay () { return cpu_speed; } void setup (intnew_speed) { cpu_speed = x; } };
Constructor class Laptop { private: intcpu_speed; public: Laptop () // Zero-parameter constructor { cpu_speed = 0; } Laptop (intinitial_speed) // One-parameter { cpu_speed = initial_speed; } intdisplay () { return cpu_speed; } void setup (intnew_speed) { cpu_speed = new_speed; } };
Example 2 #include <iostream> using namespace std; class C { public: char getchar [20]; intintval; C (char *s="", inti=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;
Inheritance Objects do not have to be instantiations of a single class.
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. { intx;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;} { intx;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;}
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
Pointers to Objects class Laptop { private: intcpu_speed; public: Laptop (intinitial_speed = 0) { cpu_speed= initial_speed; } intdisplay () { return cpu_speed; } void setup (intnew_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; } 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.
Polymorphism different objects
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: virtualtype-in-derived-classfunction-in-derived-class() { return (0); } };
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;
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.
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 functionis 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.
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 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
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. In main function
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.
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.
The Standard Template Library • Containers • Iterates • Algorithms
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
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.
ITERATES • is an object used to reference an element stored in container.
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.