Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 8 лет назад пользователемГлеб Стогов
1 1 What is Object Oriented Programming? An object is like a black box. The internal details are hidden. z Identifying objects and assigning responsibilities to these objects. z Objects communicate to other objects by sending messages. z Messages are received by the methods of an object
2 2 What is an object? zTangible Things as a car, printer,... zRoles as employee, boss,... zIncidents as flight, overflow,... zInteractions as contract, sale,... zSpecifications as colour, shape, …
3 3 So, what are objects? zan object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. Or zAn "object" is anything to which a concept applies. Etc.
4 4 Why do we care about objects? zModularity - large software projects can be split up in smaller pieces. zReuseability - Programs can be assembled from pre-written software components. zExtensibility - New software components can be written or developed from existing ones.
5 Example: The Person class #include class Person{ char name[20]; int yearOfBirth; public: void displayDetails() { cout << name << " born in " << yearOfBirth << endl; } //... }; private data public processes
6 6 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. = +
7 7 Basic Terminology zAbstraction is the representation of the essential features of an object. These are encapsulated into an abstract data type. zEncapsulation 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.
8 8 Basic Terminology: Inheritance zInheritance means that one class inherits the characteristics of another class. This is also called a is a relationship: A car is a vehicle A teacher is a person A dog is an animal
9 9 Basic Terminology: Polymorphism zPolymorphism 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. E.g. the message displayDetails() of the Person class should give different results when send to a Student object (e.g. the enrolment number).
10 10 Basic Terminology: Aggregation zAggregation describes a has a relationship. One object is a part of another object. zWe distinguish between composite aggregation (the composite owns the part) and shared aggregation (the part is shared by more then one composite). A car has wheels.
11 11 Basic Terminology: Behaviour and Messages zThe 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).
12 12 Abstract classes ListClass is an abstract class, i.e., a class in which some methods are left undefined (such as init and append ) zAbstract classes are not intended to be instantiated, but inherited from zInheritance fills in the blanks by adding the missing methods zThe result is a concrete class, i.e., a class that can be instantiated since all its methods are defined zNilClass and ConsClass are concrete classes zThis technique is an example of higher-order programming (namely genericity: passing a procedure value, i.e., a method)
13 13 The two steps of Object Oriented Programming zMaking Classes: Creating, extending or reusing abstract data types. zMaking Objects interact: Creating objects from abstract data types and defining their relationships.
14 14 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
15 15 Controlling visibility zVisibility is the control given to the user to limit access to members of a class (attributes and methods) zEach member (attribute or method) is defined with a scope (part of program text that the member can be accessed by name) zProgramming languages use words like public, private, and protected to define visibility zUnfortunately, different languages use these keywords to define different scopes ySource of enormous confusion! Be careful!
16 16 Public and private scopes zIn Smalltalk and Oz, a private member is one which is only visible in the object instance yThe object instance can see all the private members in its class and its super classes zIn C++ and Java, a private member is visible among all instances of a given class, but not to subclasses zA public member is visible anywhere in the program zBy default, attributes are private and methods are public
17 17 Function and data member
18 18 continue
19 19 Function overloading zWhen a function is refined with different set of arguments,then it is known as overloaded function.This process is known as function overloading. zExample: int add(); int add(int,int); int add(float,float);
20 20 Friend Function
21 21 Const and volatile function zC++ allows you to restrict the use of a particular member function so, as the programmer, you can insure that the user can only use it in the appropriate context. This is accomplished with the keywords const and volatile. zconst objects zconst tells the compiler that a variable will not change throughout its lifetime. This applies to variables of built-in types, as you've seen. When the compiler sees a const like this, it stores the value in its symbol table and inserts it directly, after performing type-checking (remember that this is an improvement in C++, which acts differently than C). In addition, it prevents you from changing the value of a const. The reason to declare an object of a built-in type as const is so the compiler will insure that it isn't changed. zYou can also tell the compiler that an object of a user-defined type is a const. Although it is conceptually possible that the compiler could store such an object in its symbol table and generate compile-time calls to member functions (so all the values associated with a const object would be available at compile-time), in practice it isn't feasible. However, the other aspect of a const that it cannot be changed during its lifetime is still valid and can be enforced.
22 22 const member functions zThe compiler can tell when you're trying to change a simple variable, and can generate an error. The concept of constness can also be applied to an object of a user-defined type, and it meanst the same thing: the internal state of a const object cannot be changes. This can only be enforced if there is some way to insure that all operations performed on a const object won't it. C++ provides a special syntax to tell the compiler that a member function doesn't change an object. zThe keyword const placed after the argument list of a member function tells the compiler that this member function can only read data members, but it cannot write them. Creating a const member function is actually a contract which the compiler enforces. If you declare a member function like this: zclass X { int i; public: int f() const; };then f( ) can be called for any const object, and the compiler knows that it's a safe thing to do because you've said the function is const
23 23 volatile objects and member function zA volatile object is one which may be changed by forces outside the program's control. For example, in a data communication program or alarm system, some piece of hardware may cause a change to a variable, while the program itself may never change the variable. The reason it's important to be able to declare a variable volatile is to prevent the compiler from making assumptions about code associated with that variable. The primary concern here is optimizations. If you read a variable, and (without changing it) read it again sometime later, the optimizer may assume that the variable hasn't changed and delete the second read. If the variable was declared volatile, however, the optimizer won't touch any code associated with the variable. zThe syntax for const and volatile member functions is identical. Only volatile member functions may be called for volatile objects. In addition, objects and member functions can be both const and volatile, as shown here:
24 24 Static Members
25 25 Continue
26 26 Nested classes zA nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables. zMember functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class
27 27 Local classes zA local class is declared within a function definition. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions.
28 28 C++ and C zC is a subset of C++. Advantages: Existing C libraries can be used, efficient code can be generated. But: C++ has the same caveats and problems as C (e.g. pointer arithmetic,…). zC++ can be used both as a low level and as a high level language. We focus on the high level aspects.
29 29 C++ and Java zJava is a full object oriented language, all code has to go into classes. zC++ - in contrast - is a hybrid language, capable both of functional and object oriented programming. So, C++ is more powerful but also more difficult to handle than Java.
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.