3
3 Objects An object is an encapsulation of both functions and data Objects are an Abstraction – represent real world entities – Classes are data types that define shared common properties or attributes – Objects are instances of a class – All objects have attributes (characteristics), this is sometimes referred to as state. Objects have State – have a value at a particular time Objects have Operations – associated set of operations called methods that describe how to carry out operations (behavior) Objects have Messages – request an object to carry out one of its operations by sending it a message – messages are the means by which we exchange data between objects

4
Classes Class : Whatever we can see in this world all the things are a object. And all the objects are categorized in a special group. That group is termed as a class. Class has many other features like creation and implementation of the object, Inheritance etc.

5
classes Every object belongs to (is an instance of) a class An object may have fields, or variables – The class describes those fields An object may have methods – The class describes those methods An Abstract Data Type (ADT) bundles together: – some data, representing an object or "thing" – the operations on that data The operations defined by the ADT are the only operations permitted on its data Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc.

8
Object-Oriented Concept The rectangle area problem Define a class: Rect – Data: width, length – Functions: compute_area() An object: an instance of the class Rect – To Solve the problem, create an object of Rect, and request this object to return the area of the rectangle

11
Methods & Messages Sending a message is our way of: – interacting with objects. – manipulating an objects state. Tells the object what to do with itself Example: To change the channel on TV – We use the channel selection buttons,This sends a message that we want to select a new channel – The TV responds to the message by selecting and executing a method. – The TV now receives a new signal which is the channel we selected

12
Method – Tells the object how to respond to a message Our TV-01 objects respond to the following messages: – Turn the television set on or off – Change the channel – Change the volume

14
Characteristics of OOPL Encapsulation: C ombining data structure with actions – Data structure: represents the properties, the states, or characteristics of objects – Actions: permissible behaviors that are controlled through the member functions Data hiding: Process of making certain data inaccessible Inheritance: A bility to derive new objects from old ones – permits objects of a more specific class to inherit the properties (data) and behaviors (functions) of a more general/base class – ability to define a hierarchical relationship between objects Polymorphism: A bility for different objects to interpret functions differently

17
Encapsulation an object encapsulates both its attributes & methods implications: – an attribute/ method is attached to an object/ class – when you mention an attribute/ methods, you have to specify which object/ class it comes from why encapsulation? – when you get hold of an object, you also get hold of its data & behaviour components – good for reuse

18
Information Hiding an object can hide its internal details – e.g. you dont know how your mobiles electronics works except punching the buttons can selectively show some details to the outside world – e.g. your mobile only shows the number it dials defines an interface to interact with the outside world – e.g. your mobile interacts with your through the buttons & screen

19
Why Information Hiding? the object can have a complex internal but simple interface – making interaction with the outside world easier you dont need to know about the internal of an object – only the interface is important – i.e. how to interact with it facilitate code reuse – hiding any internal change from the outside world by keeping the interface unchanged

20
Inheritance a class may be similar to another class but being more specialised – e.g. the class student is similar to the class person but student is more specialised a person has attributes like: sex, age, name a student has all these + a student no.

22
Overloading different functions/ procedures/ methods can have the same name – provided that the parameters are of different types giving a unique signature – the system will figure out which one to invoke – e.g. you can have 2 procedures, both named call, taking a dog or person object as parameter respectively. Depending on you give it a dog or person object as the parameter, Java will know which one to use.

23
Why Overloading? you can call the same method (name) but invoke different behaviour – dynamic binding of method which method to invoke is determined at runtime code reuse – in term of the calling code

24
Encapsulation is the mechanism that binds together code and data, and keeps both safe from outside interference or misuse. 1. Both data and member functions treated as single unit 2. Abstract focuses on behavior of object, encapsulation focuses on actual implementation 3. Encapsulation achieved through data hiding 4. For abstractions to work, implementations must be encapsulated.

26
ABSTRACTION AND ENCAPSULATION Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world. Definition: Data Abstraction is the separation between the specification of a data object and its implementation. Definition: A data type is a collection of objects and a set of operations that act on those objects. Definition: An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operation

27
Advantages of Data Abstraction and Data Encapsulation Simplification of software development Testing and Debugging Reusability Modifications to the representation of a data type

28
Abstract Classes May Contain abstract methods Some methods and data may be defined abstract class ColouredShape { private Colour c; // storage allocated for each public abstract void draw(); public Colour getColour() { return c; } public abstract void erase(); } No instances allowed Subclasses must implement all abstract methods or they are also abstract

29
Florida Community College at Jacksonville Object-oriented Principle - Inheritance Inheritance is the process by which one object acquires the properties of another object. By use of inheritance, an object need only define all of its characteristics that make it unique within its class, it can inherit its general attributes from its parent. 29 of 10 slides Account CheckingMortgageLoan COP 2551 Object-Oriented Programming OO Concepts Overview

30
Florida Community College at Jacksonville Object-oriented Principle – Encapsulation Encapsulation is the mechanism that binds together the code and the data it manipulates, and keeps both safe from outside interference and misuse. 30 of 10 slides A Class Private variables and methods Public variables and methods Public variables is not recommended COP 2551 Object-Oriented Programming OO Concepts Overview

31
Encapsulation Hides the implementation details of a class Forces the user to use an interface to access data Makes the code more maintainable API doc as an example

36
A Graphical Representation of Classes colour name where attributes (data component) methods (procedural component) interface to the outside world walk swim bark the Dog class the Person class name own walk instruct write_

38
Access specifiers Specify whether the data defined will be available to the users of the objects of the class. Public Private Protected Public: the data defined under public is available to objects. Private: data defined under private is not available to objects.

40
Function and data member Functions can be defined either inside or outside the class. Defining function members outside class 1.very simple to define functions outside class 2.If the functions are bigdefine outside Static data members of class 1.Static members are stored at a location where they are retained throughout the execution of the program and are not stored with class objects. 2.Stored only as a single copy– similar to member functions. 3.All the static data members are initialized to zero at the time of declaration.

44
Default Arguments A default argument is a value given in the function declaration that the compiler automatically inserts if the caller does not provide a value for that argument in the function call. Syntax: return_type f(…, type x = default_value,…);

46
Default Arguments (Rules) Once an argument has a default value, all the arguments after it must have default values. Once an argument is defaulted in a function call, all the remaining arguments must be defaulted. int f(int x, int y=0, int n) // illegal int f(int x, int y=0, int n=1) // legal

47
Function overloading Function redefined with different set of arguments. EX: add(float, float) Add(int, int) Add (int, int, int) Function overloading is useful when similar function is required to be called with either variable number of arguments or arguments of different type or both.

49
What is a Friend Function? A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class. How to define and use Friend Function in C++: The friend function is written as any other normal function, except the function declaration of these functions is preceded with the keyword friend. The friend function must have the class to which it is declared as friend passed to it in argument. Some important points The keyword friend is placed only in the function declaration of the friend function and not in the function definition. It is possible to declare a function as friend in any number of classes. When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend. It is possible to declare the friend function as either private or public. The function can be invoked without the use of an object.

54
const member functions A function, which guarantees not to modify the invoking object. If the body of the const function contains a statement that modifies the invoking object, the program does not compile. One exception here is the mutable member. A mutable data member can be modified by const function.

56
Volatile functions A member function invoked by a volatile object. A volatile object s value can be changed by external parameters which are not under the control of the program.

57
volatile member functions. Declare a member function with the volatile specifier to ensure that it can be called safely for a volatile object: class B { int x; public: void f() volatile; // volatile member function }; int main() { volatile B b; // b is a volatile object b.f(); // call a volatile member function safely } The object b is declared volatile. Calling a non-volatile member function from this object is unsafe, because b's state might have been changed by a different thread in the meantime. To ensure that f() can be called safely for a volatile object, it's declared volatile too.

64
A pointer can point to an object created by a class. Object pointers are useful in creating objects at run time. student s1; student *ptr = &s1; s1. getdata(); s1.show(); equivalent to ptr->getdata(); ptr-> show(); or (*ptr).show(); we can also create the objects using pointers and new operator student *ptr = new student; This allocates enough memory for the data members in the object structure and assigns the address of the memory space to ptr. We can also create an array of objects using pointers. student *ptr = new student[5];

65
constant objects const student s1(x,y); // object s1 is constant Any attempt to modify the values of x and y will generate compile time error. A constant object can call only constant member functions. void PrintDetails()const { cout <

66
nested classes It is an another way of inheriting properties of one class into another. From this we can understand that an object can be collection of many other objects, that is a class can contain objects of other classes as its members. class alpha(…); class beta(…); class gamma { alpha a1; beta b1; }; All objects of gamma class will contain the objects a1 and b1. This kind of relationship is called containership or nesting.

67
local classes Classes can be defined and used inside a function or a block. Such classes are called local classes. void test(int a) //function { ….. class student //local class {.. ….. //class definition … }; … student s1(a); //create student object …. … } Local classes can use global variables and static variables declared inside the function. Enclosing function cannot access the private member of a local class.

69
C++ - Advance Extension C++ allow function overloading – In C++, functions can use the same names, within the same scope, if each can be distinguished by its name and signature – The signature specifies the number, type, and order of the parameters – The name plus signature, then, uniquely identifies a function

70
Take Home Message There are many different kinds of programming paradigms, OOP is one among them. In OOP, programmers see the execution of the program as a collection of dialoging objects. The main characteristics of OOPL include encapsulation, inheritance, and polymorphism. Not only OOPL can do OOP, but also others.