What is Object Oriented Programming z Identifying objects

  • Slides: 29
Download presentation
What is Object Oriented Programming? z Identifying objects and assigning responsibilities to these objects.

What is Object Oriented Programming? z Identifying objects and assigning responsibilities to these objects. z Objects communicate to An object is like a other objects by sending black box. messages. The internal z Messages are received by details are the methods of an object hidden. 1

What is an object? z. Tangible Things z. Roles z. Incidents z. Interactions z.

What is an object? z. Tangible Things z. Roles z. Incidents z. Interactions z. Specifications as a car, printer, . . . as employee, boss, . . . as flight, overflow, . . . as contract, sale, . . . as colour, shape, … 2

So, what are objects? zan object represents an individual, identifiable item, unit, or entity,

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 z. An "object" is anything to which a concept applies. Etc. 3

Why do we care about objects? z. Modularity - large software projects can be

Why do we care about objects? z. Modularity - large software projects can be split up in smaller pieces. z. Reuseability - Programs can be assembled from pre-written software components. z. Extensibility - New software components can be written or developed from existing ones. 4

Example: The Person class #include<string> #include<iostream> class Person{ private char name[20]; data int year.

Example: The Person class #include<string> #include<iostream> class Person{ private char name[20]; data int year. Of. Birth; public: void display. Details() { cout << name << " born in " << year. Of. Birth << endl; public } processes //. . . }; 5

The two parts of an object Object = Data + Methods or to say

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 z. Abstraction is the representation of the essential features of an object.

Basic Terminology z. Abstraction is the representation of the essential features of an object. These are ‘encapsulated’ into an abstract data type. z. 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 z. Inheritance means that one class inherits the characteristics of another

Basic Terminology: Inheritance z. Inheritance means that one class inherits the characteristics of another class. This is also called a “is a” relationship: A car is a vehicle A dog is an animal A teacher is a person 8

Basic Terminology: Polymorphism z. Polymorphism means “having many forms”. It allows different objects to

Basic Terminology: Polymorphism z. 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. E. g. the message display. Details() of the Person class should give different results when send to a Student object (e. g. the enrolment number). 9

Basic Terminology: Aggregation z. Aggregation describes a “has a” relationship. One object is a

Basic Terminology: Aggregation z. Aggregation describes a “has a” relationship. One object is a part of another object. A car has wheels. z. We distinguish between composite aggregation (the composite “owns” the part) and shared aggregation (the part is shared by more then one composite). 10

Basic Terminology: Behaviour and Messages z. The most important aspect of an object is

Basic Terminology: Behaviour and Messages z. 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 z List. Class is an abstract class, i. e. , a class

Abstract classes z List. Class is an abstract class, i. e. , a class in which some methods are left undefined (such as init and append) z Abstract classes are not intended to be instantiated, but inherited from z Inheritance “fills in the blanks” by adding the missing methods z The result is a concrete class, i. e. , a class that can be instantiated since all its methods are defined z Nil. Class and Cons. Class are concrete classes z This technique is an example of higher-order programming (namely genericity: passing a procedure value, i. e. , a method) 12

The two steps of Object Oriented Programming z. Making Classes: Creating, extending or reusing

The two steps of Object Oriented Programming z. Making Classes: Creating, extending or reusing abstract data types. z. 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

Classes (syntax simplified) A class is also a value that can be in an expression position class $ attr Attr. Name 1 : Attr. Namen meth Pattern Statement end : meth Pattern Statement end 14

Controlling visibility z Visibility is the control given to the user to limit access

Controlling visibility z Visibility is the control given to the user to limit access to members of a class (attributes and methods) z Each member (attribute or method) is defined with a scope (part of program text that the member can be accessed by name) z Programming languages use words like public, private, and protected to define visibility z Unfortunately, different languages use these keywords to define different scopes y. Source of enormous confusion! Be careful! 15

Public and private scopes z In Smalltalk and Oz, a private member is one

Public and private scopes z In Smalltalk and Oz, a private member is one which is only visible in the object instance y. The object instance can see all the private members in its class and its super classes z In C++ and Java, a private member is visible among all instances of a given class, but not to subclasses z A public member is visible anywhere in the program z By default, attributes are private and methods are public 16

Function and data member 17

Function and data member 17

continue 18

continue 18

Function overloading z. When a function is refined with different set of arguments, then

Function overloading z. When a function is refined with different set of arguments, then it is known as overloaded function. This process is known as function overloading. z. Example: int add(); int add(int, int); int add(float, float); 19

Friend Function 20

Friend Function 20

Const and volatile function z C++ allows you to restrict the use of a

Const and volatile function z C++ 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. z const objects z const 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. z You 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. 21

const member functions z The compiler can tell when you're trying to change a

const member functions z The 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. z The 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: z class 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 22

volatile objects and member function z A volatile object is one which may be

volatile objects and member function z A 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. z The 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: 23

Static Members 24

Static Members 24

Continue 25

Continue 25

Nested classes z A nested class is declared within the scope of another class.

Nested classes z A 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. z Member 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 26

Local classes z. A local class is declared within a function definition. Declarations in

Local classes z. A 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. 27

C++ and C z. C is a subset of C++. Advantages: Existing C libraries

C++ and C z. C 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, …). z. C++ can be used both as a low level and as a high level language. e h t n o s u. c s o t f c e e p W s a l e v e l 28 high

C++ and Java z. Java is a full object oriented language, all code has

C++ and Java z. Java is a full object oriented language, all code has to go into classes. z. C++ - 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. 29