1 / 29

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. What is Object Oriented Programming?.

urvi
Download Presentation

What is Object Oriented Programming?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 What is Object Oriented Programming?

  2. 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, …

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

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

  5. #include<string> #include<iostream> class Person{ char name[20]; int yearOfBirth; public: void displayDetails() { cout << name << " born in " << yearOfBirth << endl; } //... }; Example: The Person class 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 • Abstractionis 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.

  8. Basic Terminology:Inheritance • 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

  9. 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. 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 • 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). A car has wheels.

  11. Basic 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).

  12. 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 from • Inheritance “fills in the blanks” by adding the missing methods • The result is a concrete class, i.e., a class that can be instantiated since all its methods are defined • NilClass and ConsClass are concrete classes • This 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 • Making Classes: Creating, extending or reusing abstract data types. • Making 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 • 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 of program text that the member can be accessed by name) • Programming 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!

  16. 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 classes • In C++ and Java, a private member is 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

  17. Function and data member

  18. continue

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

  20. Friend Function

  21. Const and volatile function • 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. • const objects • 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. • 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.

  22. const member functions • 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. • 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: • 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

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

  24. Static Members

  25. Continue

  26. Nested classes • 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. • 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

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

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

More Related