1 / 35

OOP using C++

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 -

styron
Télécharger la présentation

OOP using C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOP using C++

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

  3. 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. 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. Example 1 class Laptop { private: intcpu_speed; public: intdisplay () { return cpu_speed; } void setup (intnew_speed) { cpu_speed = x; } };

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

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

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

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

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

  11. Pointers an copy constructors (Discussion)

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

  13. Pointer to functions (self study)

  14. Polymorphism different objects

  15. Pointers to base class

  16. 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); } };

  17. 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;

  18. Example

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

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

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

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

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

  24. Example

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

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

  27. The Standard Template Library • Containers • Iterates • Algorithms

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

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

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

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

  32. Read only

More Related