Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.

Similar presentations

Presentation on theme: "Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in."— Presentation transcript:

1 Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in terms of objects in the domain of the problem you are trying to solve, so part of the development process is designing a set of types to suit the context. For a program to analyze bandy scores, you may have types such as Player and Team. The variables of the basic types don’t allow you to model real-world objects adequately.

2 Classes and Object- Oriented... tMyn2 A structure as we have defined it up to now is a big step forward, but there is more to it than that. As well as the notion of user-defined types, object- oriented programming implicitly incorporates a number of additional important ideas, namely encapsulation, data hiding, inheritance and polymorphism.

3 Classes and Object- Oriented... tMyn3 Encapsulation: An object will contain a specific set of data values that describe the object in sufficient detail for your needs. An object will also contain a set of functions that operate on it – functions that will use or change the set of data values. They define the set of operations that can be applied to the object – what you can do with it, or to it. Every object of a given type will incorporate the same combination of these things: the set of data values as data members, and the set of operations as member functions.

4 Classes and Object- Oriented... tMyn4 This packaging of data values and functions within an object is referred to as encapsulation.

5 Classes and Object- Oriented... tMyn5 Wikipedia: The term encapsulation is often used interchangeably with information hiding, while some make distinctions between the two. It seems that people, however, fail to agree on the distinctions between information hiding and encapsulation though one can think of information hiding as being the principle and encapsulation being the technique. A software module hides information by encapsulating the information into a module or other construct which presents an interface.

6 Classes and Object- Oriented... tMyn6 An object contains everything necessary to define its properties and define its operations. The data members define the properties that distinguish the object. The member functions of the object define what you can do with it. A loanAccount object balance: 16000€ interestRate: 22% calcInterest()

7 Classes and Object- Oriented... tMyn7 Data hiding: Ideally, the data members of a loanAccount object would be protected from direct outside interference, and would only be modifiable in a controlled way. The ability to make the data values for an object generally inaccessible is called data hiding.

8 Classes and Object- Oriented... tMyn8 Wikipedia: In computer science, the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. Protecting a design decision involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change). In modern programming languages, the principle of information hiding manifests itself in a number of ways, including encapsulation (given the separation of concerns) and polymorphism.

9 Classes and Object- Oriented... tMyn9 Generally, the data members should not be accessible from outside. The member functions can provide the means to alter data members when necessary. The data members of an object should generally be hidden. The member functions of the object provide the tools to alter the data members in a controlled way. A loanAccount object balance: 16000€ interestRate: 22% calcInterest()

10 Classes and Object- Oriented... tMyn10 The member functions of the object can provide a mechanism that ensures any changes to the data members follow a particular policy, and that the values set are appropriate. Hiding the data belonging to an object prevents it from being accessed directly, but you can provide access through functions that are members of the object, either to alter a data value in a controlled way, or simply to obtain its value. Hiding the data within an object is not mandatory, but it’s generally a good idea for at least a couple of reasons:

11 Classes and Object- Oriented... tMyn11 Maintaining the integrity of an object requires that you control how changes are made. Direct access to the values that define an object undermines the whole idea of object-oriented programming. Object-oriented programming is supposed to be programming in terms of objects, not in terms of the bits that go to make up an object. You can think of the data members as representing the state of the object, and the member functions that manipulate them as representing the object’s interface to the outside word.

12 Classes and Object- Oriented... tMyn12 A program using the class interface is only dependent on the function names, parameter types and return types specified for the interface. The internal mechanics of these functions do not affect the program creating and using objects of the class.

13 Classes and Object- Oriented... tMyn13 Inheritance: Inheritance is the ability to define one type in terms of another. So inheritance is a relationship called generalization which is a specialization/generalization relationship in which objects of the specialized element (the child) are substitutable for objects of the generalized element (the parent). It is the mechanism by which more-specific elements incorporate the structure and behaviour of more- general elements.

14 Classes and Object- Oriented... tMyn14 Graphically, generalization is rendered as a solid directed line with a large open arrowhead, pointing to the parent. A class that has exactly one parent is said to use single inheritance; a class with more than one parent is said to use multiple inheritance.

15 Classes and Object- Oriented... tMyn15 move(d: direction; s: speed) stop() resetCounter() Motor steeringMotormainMotor

16 Classes and Object- Oriented... tMyn16 Wikipedia: Inheritance is a way to form new classes using classes that have already been defined. The new classes, known as derived classes, take over (or inherit) attributes and behaviour of the pre-existing classes, which are referred to as base classes. It is intended to help reuse of existing code with little or no modification.

17 Classes and Object- Oriented... tMyn17 Inheritance is also sometimes called generalization, because the is-a relationships represent a hierarchy between classes of objects. For instance, a "fruit" is a generalization of "apple", "orange", "mango" and many others. One can consider fruit to be an abstraction of apple, orange, etc. Conversely, since apples are fruit (i.e. an apple is-a fruit), apples may naturally inherit all the properties common to all fruit, such as being a fleshy container for the seed of a plant.

18 Classes and Object- Oriented... tMyn18 One common reason to use inheritance is to create specializations of existing classes. In specialization, the new class has data or behavior aspects that are not part of the inherited class. For example, a "Bank Account" class might have data for an "account number", "owner", and "balance". An "Interest Bearing Account" class might inherit "Bank Account" and then add data for "interest rate" and "interest accrued" along with behavior for calculating interest earned.

19 Classes and Object- Oriented... tMyn19 Polymorphism: The word polymorphism means the ability to assume different forms at different times. It means that, in a hierarchy of classes, you can specify operations with the same signature at different points in the hierarchy. Ones in the child classes override the behaviour of ones in the parent classes. When a message is dispatched at run time, the operation in the hierarchy that is invoked is chosen polymorphically – that is, a match is determined at run time according to the type of the object.

20 Classes and Object- Oriented... tMyn20 Collectively, the name of an operation plus its parameters (including its return type, if any) is called the operation’s signature. Polymorphism in C++ always involves calling a member function of an object, using either a pointer or a reference. Such a function call can have different effects at different times. Polymorphism means that objects belonging to a family of inheritance-related classes can be passed around and operated on using base class pointers and references.

21 Classes and Object- Oriented... tMyn21 The particular function called through the pointer is not decided when your program is compiled, but when your program executes. Thus, the same function call can do different things depending on what kind of object the pointer points to.

22 Classes and Object- Oriented... tMyn22 Wikipedia: Polymorphism is the ability of objects belonging to different types (classes) to respond to method calls (member function calls) of methods of the same name, each one according to an appropriate type- specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding).

23 Classes and Object- Oriented... tMyn23 In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently. This means that the same member function can result in different actions as appropriate for a specific class.

24 Classes and Object- Oriented... tMyn24 Summary: A class is a user-defined data type. It is a description of a set of objects that share the same attributes, operations, relationships and semantics. The variables and functions declared within a class are called members of the class. The variables are called data members and the functions are called member functions. The member functions of a class are sometimes referred to as methods. More generically, a method is the implementation of an operation.

25 Classes and Object- Oriented... tMyn25 Having defined a class, we can declare variables of the class type (also called instances of the class). Each instance will be an object of the class. An object is a concrete manifestation of an abstraction; an entity with a well-defined boundary and identity that encapsulates state and behaviour; an instance of a class. OOP is a programming style based on the idea of defining your own data types as classes. It involves the ideas of encapsulation of data, class inheritance and polymorphism.

Download ppt "Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in."

Similar presentations

Ads by Google