Objects Classes
Properties Methods
Compare Classes with Structures Describe Private and Public sections of Classes Define Member functions
Personnel
Accounts
Sales
Adding new data items means modifying all the tasks or functions that access the data. Next to impossible to separate parts of a program from revisions made in another part.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 7 of 47
Traditional Programming focussed on implementation details. In Traditional Programming code and data were stored separately.
Introduction to OOP
OOP allows for analysis and design of an application in terms of entities or objects. Code and data are merged into a single indivisible thing -- an object.
Functions:
Calculate salary Pay salary Pay bills Tally accounts Transact with banks
Objects
A concept or thing with defined boundaries that is relevant to the problem we are dealing with.
Help to understand the real world Provide a practical basis for a computer application
Each object has its own properties or characteristics that describe what it is or does.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 11 of 47
Different Objects
Name: Jack Age: 28 Weight: 65 kgs Actions: Walk Talk Sleep Model: Ferrari Colour: Red Year: 1995
Classes
Group of objects that have the same properties, common behaviour and common relationships.
Polygon objects
Property/Attribute
A characteristic required of an object or entity when represented in a class Each instance of the class or object has its own value for each of its properties but it shares the property names or operations with other instances of the class.
Method
Sales
What is the salary of Jack
The black box actually contains code The communication between objects is done using messages. These messages can be translated to function calls in a program.
Accounts
Abstraction
Process of examining certain aspects of a problem.
Data Abstraction
Used to identify properties and methods of each object as relevant to the application at hand. By grouping objects into classes, we are doing data abstraction of a problem. Common definitions are stored once per class rather than once per instance of the class. Methods can be written once for a class, so that all the objects in a class benefit from code reuse.
Inheritance (1)
The property that allows the reuse of an existing class to build a new class. Superclass - The class from which another class inherits its behaviour. Subclass - The class that inherits the properties and methods of another class.
Inheritance (2)
Each subclass shares common properties with the class from which it is derived. Subclass may have its own particular characteristics.
Insects
Mammals
Reptiles
Amphibians
Encapsulation (1)
Providing access to an object only through its messages, while keeping the details private is called information hiding. An equivalent buzzword is encapsulation.
A class has many properties and methods. It is not necessary for a user to have access to all of them. All communication to an object is done via messages. Messages define the interface to the object.
Encapsulation (2)
Encapsulation:
Builds an impenetrable wall to protect the code from accidental corruption due to the silly little errors that we are all prone to make.
Isolates errors to small sections of code to make them easier to find and fix.
Reusability
Object-oriented development allows:
Information to be shared within an application Reuse of designs and code for future projects
Programs are broken down into reusable objects. Inheritance also promotes reuse.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 24 of 47
Cats
Lions
Polymorphism (1)
Polymorphism enables the same function to behave differently on different classes. The existing object stays the same, and any changes made are only additions to it.
Polymorphism (2)
Subclasses Class: Shape Methods: Draw Move Initialise
Class Construct
The general syntax of the class construct is:
class user_defined name{ private: data_type members; implementation operations; public: data_type members; implementation operations; };
Private data of other classes is hidden and not available within the functions of this class.
Member functions are usually put in the public part of the class because they have to be called outside the class either in a program or in a function.
Declaration of a member function must define its return value as well as the list of its arguments.
For example, the member function void setdate(int, int, int) has no return value or has a void return value, and has three integer arguments.
Defining Objects
exampleclass object1,object2; defines two objects, object1 and object2, of class exampleclass. When we define an object, space is set aside for it in memory.
A member function is always called to act on a specific object, not on the class. Associated with a specific object with the dot operator ( the period).
object_data 350
object2
actual argument When a member function is called, it is given access to the object of which the function is a member.
Returning Objects
A return statement in a function is considered to initialise a variable of the returned type.
exampleclass object3 = object1.function1(object2);
Passing and returning of objects is not very efficient since it involves passing and returning a copy of the data members.
Object-Oriented Languages
C++ , Smalltalk , Eiffel, CLOS, Java Vary in their support of object-oriented concepts. No single language that matches all styles and meets all needs.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 43 of 47
Smalltalk (1)
Pure object-oriented language. While C++ makes some practical compromises to ensure fast execution and small code size, Smalltalk makes none. Uses run-time binding, which means that nothing about the type of an object need be known before a Smalltalk program is run. Significantly faster to develop than C++ programs.
Smalltalk (2)
Has a rich class library that can be easily reused via inheritance Has a dynamic development environment. It is not explicitly compiled, like C++. Smalltalk generally takes longer to master than C++. It is syntactically very simple.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 47 of 47