Sei sulla pagina 1di 3

Lecture 1

Introduction to Object Oriented Programming


The real world consists of objects. Thats how humans see and understand the world! o All objects have two things: attributes (e.g., size, shape, color and weight) and behavior (e.g., a ball rolls, bounces, inflates and deflates; a baby cries, sleeps, crawls, walks and blinks; a car accelerates, brakes and turns; a towel absorbs water). Object oriented programming (OOP) is a programming paradigm/style/technique that uses the humans way of thinking in term of objects and considers everything an object. o OOP allows programmers to package everything into nice neat objects/packages. o Each object in OOP can have attributes and behaviors. o All objects are self-sufficient/complete/independent of each other. o The objects can interact/communicate with each other. The programmer can create a collection of attributes and behaviors once and can create multiple copies/instances of that collection. In technical OOP terminology o The collection is known as a class. o The attributes that we package are known as data members and o The behaviors are known as member functions (also called methods). o Each instance/copy of the collection is known as an object. Example: car o Class name: car (note: car would be the collection that well package attributes and behaviors into) o Data members (attributes): Type Color Top speed Engine capacity Maximum loading capacity Number of doors o Member functions (behaviors): Start Stop Accelerate Brake Turn o Object: Honda Civic o Saying Honda Civic is an object of class car is similar to saying Honda Civic is an instance of a car. Classes are also known as Abstract Data Types.

Data hiding
A cars internal working is hidden under it body. The user doesnt know and doesnt want to know what happens inside the car. Theres a very clean and clear exposed set of equipment and procedures using which the user can drive the car without knowing what happens in there. The manufacturer of the car might want to hide the internal working of the car to o Not overwhelm the user with unwanted details because thats not the users concern. OOP states that the internal working of a class and the data it holds must be hidden. o Reason is not that we are working on some top secret project. o The only reason of employing data hiding is that the internal working and data of a class must not be accessible by the parts of a program that dont need to access it. o But there must also be a clean and clear set of tools and procedures that let the user use a class. This is known as data hiding.

Encapsulation
The internal working of the car and the set of clean and clear equipment and procedures that allow a user to use the car are all combined into one unit: the car. OOP says that the data of the class, its internal working and the tools and procedures that allow its usage should all be combined into the class itself, so that every object is self-sufficient and independent of others. o Every object is one complete unit having all what it needs. Add a function to the class and explain interface of the class: why functions need to be public and data private. Functions are also called behavior of the object.

Example
class Distance { private: int feet; int inches; public: void display() { cout<<feet<<'-'<<inches<<'\"'; } };

You can declare objects of this class like o Distance d1; Its exactly similar to declaring variables int a

would declare a variable a of type int Distance d1 would declare an object d1 of type Distance

Potrebbero piacerti anche