1 What is Object Oriented Programming? An object is like a black box. The internal details are hidden. z Identifying objects and assigning responsibilities.

Презентация:



Advertisements
Похожие презентации
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Advertisements

Unit II Constructor Cont… Destructor Default constructor.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
1/27 Chapter 9: Template Functions And Template Classes.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
1 © Luxoft Training 2012 Inner and anonymous classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Учимся писать Эссе. Opinion essays § 1- introduce the subject and state your opinion § 2-4 – or more paragraphs - first viewpoint supported by reasons/
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Customer-to-Provider Connectivity with BGP Connecting a Multihomed Customer to Multiple Service.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Applying Route-Maps as BGP Filters.
DRAFTING TECHNIQUES I 136. Here is a basic shape. From here, we will do some advanced drafting once we put this shape on a sheet as a drawing. Select.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Ideal Family Were prepared by Iryna Molokova and Ilona Synytsia.
Транксрипт:

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

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 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 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 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 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 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 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 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 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 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 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 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 Function and data member

18 continue

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 Friend Function

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 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 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 Static Members

25 Continue

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