Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
523 KB
Nội dung
Basic Object Oriented Concepts Session Object Oriented Programming with C++/ Session 1/ of 50 Session Objectives s Discuss the following: • The Object-Oriented approach • Drawbacks of traditional programming • Object-Oriented programming s Discuss basic Object-Oriented concepts such as: • Objects • Classes • Properties Object Oriented Session Objectives (Contd.) • Methods • Abstraction • Inheritance • Encapsulation • Polymorphism s Compare Classes with Structures s Describe Private and Public sections of Classes Object Oriented Session Objectives (Contd.) s Define Member functions s Use the Objects and Member functions of a Class • Define Objects • Access Member Functions • Pass and return Objects s Discuss briefly the features of C++ and another OO language (Smalltalk) Object Oriented The Object-Oriented approach s Can classify living beings as objects just as we classify things we use as objects of different types s Any application can be defined in terms of entities or objects so that the process replicates human thought process as closely as possible Object Oriented Departments as objects in an organisation Personnel Accounts Sales s People in each department control and operate on that department's data Object Oriented Traditional programming: Drawbacks s Unmanageable programs • Traditional programs: List of instructions written in a language that tells the computer to perform some actions • When programs become larger they become unmanageable • Functions/procedures/subroutines adopted to make programs more comprehensible • As programs grow larger and more complex, even use of functions can create problems Object Oriented Problems in modification of data s Data plays a crucial role in traditional programming techniques s Adding new data items means modifying all the tasks or functions that access the data • As program size increases it is difficult to locate and to modify all functions which use the data s Restrict access to the data so that only a few critical functions act upon it s Next to impossible to separate parts of a program from revisions made in another part Object Oriented Difficulty in implementation s Focus of traditional programming approach: On implementation details s Focus of a person’s thinking: In terms of things or entities, their properties and actions s OOP techniques help to correspond real-world entities and actions to functions and data at the programming level Object Oriented Introduction to OOP s OOP allows for analysis and design of an application in terms of entities or objects s Process replicates the human thought process as closely as possible s Code and data are merged into a single indivisible thing an object s Close match between objects in the programming sense and objects in the real world Object Oriented Member functions (Contd.) s Member functions can be more complex as they can have local variable, parameters etc s Should not have the same name as a data member Object Oriented Using the class begin program class exampleclass{ // specify a class private: object_data is an integer; // class data public: member_function1(parameter1, parameter2…) {assign value to object_data} member_function2(){display data} }; Object Oriented Using the class (Contd.) main program{ //define the objects of class exampleclass exampleclass object1,object2; //call member fn to assign value 200 //to object_data object1.member_function1(200); //call member function to display data object1.member_function2(); object2.member_function1(350); object2.member_function2(); } Object Oriented Defining Objects s exampleclass object1,object2; defines two objects, object1 and object2, of class exampleclass s The definition actually creates objects that can be used by the program s When we define an object, space is set aside for it in memory Object Oriented Calling member functions s We communicate with objects by calling their member functions object1.member_function1(200); object1.member_function2(); s A member function is always called to act on a specific object, not on the class in general s Associated with a specific object with the dot operator ( the period) Object Oriented Calling member functions s The general syntax for accessing a member function of a class is class_object.function_member() s The dot operator is called the class member operator Object Oriented Two objects with different values object_data object_data 200 object1 Objects of the class exampleclass Specifications for exampleclass objects object_data 350 object2 exampleclass class specifier Object Oriented Passing Objects s Objects can be passed to a function and returned back just like normal variables s The compiler creates another object as a formal variable in the called function It copies all the data members from the actual object int function1(exampleclass obj1) { return object_data;} Formal argument Object Oriented Passing Objects (Contd.) s The above function can be invoked using a function call such as, int variable = object1.function1(object2); actual argument s When a member function is called, it is given access to the object of which the function is a member Object Oriented Returning Objects s A return statement in a function is considered to initialise a variable of the returned type exampleclass object3 = object1.function1(object2); s Passing and returning of objects is not very efficient since it involves passing and returning a copy of the data members Object Oriented Object-Oriented Languages s C++ , Smalltalk , Eiffel, CLOS, Java s Vary in their support of object-oriented concepts s No single language that matches all styles and meets all needs Object Oriented An introduction to C++ s Developed by Bjarne Stroustrup at AT&T Bell Laboratories s Derived from the C language s It is compatible with C (it is actually a superset) Most important elements added to C to create C++ are concerned with classes, objects and object-oriented programming Object Oriented An introduction to C++ (Contd.) s C++ programs are fast and efficient but it sacrifices some flexibility in order to remain efficient s C++ uses compile-time binding Provides high run-time efficiency and small code size, but it trades off some of the power to reuse classes Object Oriented Smalltalk s Pure object-oriented language s While C++ makes some practical compromises to ensure fast execution and small code size, Smalltalk makes none s Uses run-time binding, which means that nothing about the type of an object need be known before a Smalltalk program is run s Significantly faster to develop than C++ programs Object Oriented Smalltalk s Has a rich class library that can be easily reused via inheritance s Has a dynamic development environment s It is not explicitly compiled, like C++ s Smalltalk generally takes longer to master than C++ It is syntactically very simple Object Oriented .. .Session Objectives s Discuss the following: • The Object- Oriented approach • Drawbacks of traditional programming • Object- Oriented programming s Discuss basic Object- Oriented concepts. .. object1 .member_function2(); object2 .member_function1(350); object2 .member_function2(); } Object Oriented Defining Objects s exampleclass object1 ,object2 ; defines two objects, object1 and object2 , of class... operator Object Oriented Two objects with different values object_ data object_ data 200 object1 Objects of the class exampleclass Specifications for exampleclass objects object_ data 350 object2