bài giảng chú giải trình hướng đối tượng bài 1

42 284 0
bài giảng chú giải trình hướng đối tượng bài 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

What is Object Oriented Programming? An object is like a black box. The internal details are hidden. • Identifying objects and assigning responsibilities to these objects. • Objects communicate to other objects by sending messages. • Messages are received by the methods of an object 1 What is an object? • Tangible Things as a car, printer, • Roles as employee, boss, • Incidents as flight, overflow, • Interactions as contract, sale, • Specifications as colour, shape, … 2 So, what are objects? • an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. Or • An "object" is anything to which a concept applies. Etc. 3 Why do we care about objects? • Modularity - large software projects can be split up in smaller pieces. • Reuseability - Programs can be assembled from pre-written software components. • Extensibility - New software components can be written or developed from existing ones. 4 Example: The Person class #include<string> #include<iostream> class Person{ char name[20]; int yearOfBirth; public: void displayDetails() { cout << name << " born in " << yearOfBirth << endl; } // }; private data public processes The two parts of an object Object = Data + Methods or to say the same differently: An object has the responsibility to know and the responsibility to do. 6 = + Basic Terminology • Abstraction is the representation of the essential features of an object. These are ‘encapsulated’ into an abstract data type. • Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects. 7 Basic Terminology: Inheritance • Inheritance means that one class inherits the characteristics of another class. This is also called a “is a” relationship: 8 A car is a vehicle A teacher is a person A dog is an animal Basic Terminology: Polymorphism • Polymorphism means “having many forms”. It allows different objects to respond to the same message in different ways, the response specific to the type of the object. 9 E.g. the message displayDetails() of the Person class should give different results when send to a Student object (e.g. the enrolment number). Basic Terminology: Aggregation • Aggregation describes a “has a” relationship. One object is a part of another object. • We distinguish between composite aggregation (the composite “owns” the part) and shared aggregation (the part is shared by more then one composite). 10 A car has wheels. [...]... that f() can be called safely for a volatile object, it's declared volatile too Pointers and objects int x = 10 ; int *p; p p = &x; p gets the address of x in memory 10 x What is a pointer? int x = 10 ; int *p; p p = &x; *p = 20; *p is the value at the address p 20 x What is a pointer? int x = 10 ; int *p = NULL; p = &x; Declares a pointer to an integer & is address operator gets address of x *p = 20;... memory Point *p = new Point(5, 5); // free the memory delete p; For every call to new, there must be exactly one call to delete Using new with arrays int x = 10 ; int* nums1 = new int [10 ]; int* nums2 = new int[x]; // ok // ok • Initializes an array of 10 integers on the heap ... procedure value, i.e., a method) 12 The two steps of Object Oriented Programming • Making Classes: Creating, extending or reusing abstract data types • Making Objects interact: Creating objects from abstract data types and defining their relationships 13 Classes (syntax simplified) A class is also a value that can be in an expression position class $ attr 〈AttrName1〉 : 〈AttrNamen〉 meth 〈Pattern〉 〈Statement〉... visible among all instances of a given class, but not to subclasses • A public member is visible anywhere in the program • By default, attributes are private and methods are public 16 Function and data member 17 continue 18 Default Arguments • • A default argument is a value given in the function declaration that the compiler automatically inserts if the caller does not provide a value for that argument... Terminology: Behaviour and Messages • The most important aspect of an object is its behaviour (the things it can do) A behaviour is initiated by sending a message to the object (usually by calling a method) 11 Abstract classes • ListClass is an abstract class, i.e., a class in which some methods are left undefined (such as init and append) • Abstract classes are not intended to be instantiated, but inherited... arguments after it must have default values Once an argument is defaulted in a function call, all the remaining arguments must be defaulted int f(int x, int y=0, int n) // illegal int f(int x, int y=0, int n =1) // legal Static Members 22 Continue 23 Function overloading • Function redefined with different set of arguments EX: • add(float, float) • Add(int, int) • Add (int, int, int) • Function overloading... languages use words like public, private, and protected to define visibility • Unfortunately, different languages use these keywords to define different scopes – Source of enormous confusion! Be careful! 15 Public and private scopes • In Smalltalk and Oz, a private member is one which is only visible in the object instance – The object instance can see all the private members in its class and its super... 13 Classes (syntax simplified) A class is also a value that can be in an expression position class $ attr 〈AttrName1〉 : 〈AttrNamen〉 meth 〈Pattern〉 〈Statement〉 end : meth 〈Pattern〉 〈Statement〉 end end 14 Controlling visibility • Visibility is the control given to the user to limit access to members of a class (attributes and methods) • Each member (attribute or method) is defined with a scope (part . in the program • By default, attributes are private and methods are public 16 Function and data member 17 continue 18 Default Arguments • A default argument is a value given in the function. types and defining their relationships. 13 Classes (syntax simplified) A class is also a value that can be in an expression position class $ attr 〈AttrName1〉 : 〈AttrNamen〉 meth 〈Pattern〉 〈Statement〉. do). A behaviour is initiated by sending a message to the object (usually by calling a method). 11 Abstract classes • ListClass is an abstract class, i.e., a class in which some methods are left

Ngày đăng: 24/10/2014, 16:24

Từ khóa liên quan

Mục lục

  • What is Object Oriented Programming?

  • What is an object?

  • So, what are objects?

  • Why do we care about objects?

  • Example: The Person class

  • The two parts of an object

  • Basic Terminology

  • Basic Terminology: Inheritance

  • Basic Terminology: Polymorphism

  • Basic Terminology: Aggregation

  • Basic Terminology: Behaviour and Messages

  • Abstract classes

  • The two steps of Object Oriented Programming

  • Classes (syntax simplified)

  • Controlling visibility

  • Public and private scopes

  • Function and data member

  • continue

  • Default Arguments

  • Default Arguments (Examples)

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan