1 / 33

Introduction to Object-oriented Programming

Introduction to Object-oriented Programming. OOP Concepts. Object-oriented vs Procedure-oriented Programming Objects and Classes Encapsulation and Information Hiding. Procedure-Oriented Programing.

audi
Download Presentation

Introduction to Object-oriented Programming

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. Introduction toObject-oriented Programming

  2. OOP Concepts • Object-oriented vs Procedure-oriented Programming • Objects and Classes • Encapsulation and Information Hiding

  3. Procedure-Oriented Programing • In procedure-oriented programs (FORTRAN, COBOL, C, Pascal, BASIC, etc.), emphasis is on sequence of commands (procedure) to the computer. • For example, the following algorithm • reads a list of items from a file, • sorts the list, and • prints the sorted list.

  4. Read, Sort, Print (procedural) Open(aFile) cout 0 while (not isEOF (aFile)) Read(anItem from aFile) count  aList (count)  anItem end While Close(aFile) Sort(aList) Print ( elements of aList)

  5. Object-oriented Programming • As programs become complex, procedural programming becomes inadequate—too many data and actions to keep track of. • Many program behaviors are not simply sequential but are concurrent. • In OOP, a program consists of a collection of objects that interact with each other.

  6. Emphasis on Objects aFile.open() while( aFile.EOF() is not true)    anItem = aFile.read() aList.insertNewItem(anItem) end while aFile.close() aList.sort() aList.print()

  7. In OOP Program, note… • Objects—e.g., aList and aFile—are the focus of program, not procedures. • Each object possesses operations peculiar to it—e.g., aFile.open(), aFile.close(), aList.read(), aList.insertNewItem(), aList.sort() • Objects are called (asked) to perform its operation to get a job done—e.g., aFile.open. • In OOP, the inner structure of aList is not revealed to the user of the object—user need not know whether it is an array, linked list, or something else.

  8. Objects and Classes • An OO program consists of a collection of objects interacting with each other. • Class • A template for creating objects • It consists of attributes (characteristics) and methods (operations) • Object • An instance of a class • Many object can be created from a class

  9. Objects and Classes • Suppose you are writing a game that involves several dogs, cats, and cars. (Use your imagination.) • You need to define a Dog class. • Here are some dog objects: • Fido—a big brown bulldog • Lassie—a tall collie dog • Taro—a small, white akita dog

  10. Objects Dog object Dog object Attributes: name = Fido breed = bulldog color = brown weight = 30 kg height = 50 cmOperations: bark() display() Dog object Attributes: name = Lassie breed = collie color = brow weight = 25 kg height = 70 cmOperations bark() display() Attributes: name = Taro breed = akita color = white weight = 10kg height = 30 cmOperations bark() display()

  11. Class • A class • Is an abstraction of the objects of the same type. Fido, Lassie, and Taro are all instances of the Dog class. • Is a template for creating (specifying) objects of a particular type. • Is like a blueprint for creating objects • Specifies attributes and methods (operations) • Each objects has • Different attributes values • Same operations

  12. Class (cont.) • A class is composed of • Class Name • Attributes • Operations • Once a class is defined, objects can be created (instantiated) from it.

  13. Class name Attributes Operations Dog breedcolorweightheight Bark()display() Car makeengineSizecolormaxSpeed Start()accelerate()stop()display() Class Diagram

  14. Practice • Specify some relevant attributes and operations for the following classes. • Bank Account • Book • Browser window • Circle (geometric figure) • Rectangle (geometric figure)

  15. OOP Terminology • Abstraction: a definition that captures general characteristics without details • An abstract triangle is a 3-sided polygon. A specific triangle may be scalene, isosceles, or equilateral • Data hiding: restricting access to certain members of an object. The intent is to allow only member functions to directly access and modify the object’s data

  16. OOP Terminology (cont.) Encapsulation: the bundling of an object’s data and procedures into a single entity

  17. Object Example Square Member variables (attributes) double side; Member functions void setSide(double s) { side = s; } int getSide() { return side; } Square object’s data item: side Square object’s functions: setSide() getSice()

  18. C++ Class class Square {private: double side;public: double getSide(){return side;} void setSide(double s){side = s;}};

  19. Class Example Accessspecifiers class Square { private: int side; public: void setSide(int s) { side = s; } double getSide() { return side; } };

  20. More on Access Specifiers Can be listed in any order in a class Can appear multiple times in a class If not specified, the default is private

  21. 7.4 Introduction to Objects • An object is an instance of a class • Defined just like other variables Square sq1, sq2; • Can access members using dot operator sq1.setSide(5); cout << sq1.getSide();

  22. Types of Member Functions • Acessor function:uses but does not modify a member variable ex: getSide • Mutator function: modifies a member variable ex: setSide • Constructor: creaes an instance of class

  23. Defining Member Functions • Member functions are part of a class declaration • Can place entire function definition inside the class declaration (inline function) or • Can place just the prototype inside the class declaration and write the function definition after the class

  24. Inline Functions • Member functions defined inside the class declaration are called inline functions • Only very short functions should be inline functions class Square{ private: . . . public: double getSide(){ return side;} . . . };

  25. Member Functions Defined After the Class Declaration • Put a function prototype in the class declarationdouble getSide(); • In the function definition, precede function name with class name and scope resolution operator (::) double Square::getSide() { return side; }

  26. Constructors A constructor is a member function that is used to initialize data members of a class Is called automatically when an object of the class is created Must be a public member function Must be named the same as the class Must have no return type

  27. Constructors--Examples Class Declaration(square.h) Function Definitions (square.cpp) #include “square.h”Square::Square(){ side = 0;} Square::Square(double s){ side = s;} class Square {private: double side;public: Square(); Square(double s); . . .};

  28. Invoking Constructor Client Program (squareTest.cpp) #include <iostream>#include “square.h”using namespace std;int main(){ Square sq1; // invoking 1st constructor Square sq2(5); // invoking 2nd constructor cout << “sq1 side: “ << sq1.getSide() << endl; cout << “sq2 side: “ << sq2.getSide() << endl; return 0; }

  29. Passing Class Object As Function Argument A class object can be passed as an argument to a function When passed by value, function makes a local copy of object. Original object in calling environment is unaffected by actions in function When passed by reference, function can use ‘set’ functions to modify the object.

  30. Notes on Passing Objects • Using a value parameter--can slow down a program and waste space • Using a reference parameter --speeds up program, but allows the function to modify data in the structure • To save space and time, while protecting structure data that should not be changed, use a const reference parameter void showData(const Square &s) // header

  31. Include Guards • Used to prevent a header file from being included twice • Format: #ifndef symbol_name #define symbol_name . . . (normal contents of header file) #endif • symbol_nameis usually the name of the header file, in all capital letters: #ifndef SQUARE_H #define SQUARE_H . . . #endif

  32. Array of Class Objects • Class objects can also be used as array elements class Square { private: int side; public: Square(int s = 1) { side = s; } intgetSide() { return side; } }; Square shapes[10]; // Create array of 10 // Square objects

  33. Arrays of Class Objects (cont.) • Use an array subscript to access a specific object in the array • Then use dot operator to access member methods of that object for (i = 0; i < 10; i++) cout << shapes[i].getSide() << endl;

More Related