Você está na página 1de 45

EC-211 Object Oriented Programming and Data Structures using C++

Lecture 1

Resources
Object Oriented Programming in C++, Robert Lafore C++ How to Program, Deitel and Deitel www.cplusplus.com Thousands of other resources available online

Lecture Details
1.
i. ii.

Programming Basics
Procedural Programming Object Oriented Programming

2.
i. ii.

The Object Oriented Approach


Classes Objects

3.
i. ii.

The Object Oriented Advantage


Encapsulation Inheritance

Reading
Ch 1 of Lafores book

Programming Basics
There are 2 ways to write a program The Procedural approach The Object Oriented approach

Procedural Programming
What you have been doing until now. Instructions are given in a listed format and executed in a top-down format.

Example Procedural

Procedural Programming
Click to edit Master text styles Second level Third level Fourth level Fifth level

Procedural Programming
Problems with Procedural Programming Cannot cope with very large project sizes Expensive software errors (e.g. air traffic control) Goal of OOP Clearer, more reliable, more easily maintained programs More effective way of coping with

Procedural Programming
Causes of Problems with Procedural Programming Unrestricted Access to Data

Global data is allowed Access to data by multiple functions leads to inconsistencies, and side-effects Programs become difficult to modify and maintain

Example procedural 2

Procedural Programming
Poor Modeling of Real World Things

Real world things are integral collections of data and functions e.g. a car: has data (make, model etc.) and functions (acceleration) Procedural languages do not tie up data with functions

Difficulties in Creating New Data Types

e.g. complex nos., date, rational nos. etc. Language Extensibility

Write a program to bake cookies.

Ref: http://www.youtube.com/watch?v=c5kfCH50wl0

Procedural Programming

Very task related.

PSEUDOCODE:

Gather ingredients (flour, butter, sugar, eggs, etc.) Preheat oven Beat eggs and butter Add flour and sugar Mix Bake for 10 mins.

Object Oriented Programming

Model Related

Organizes properties and functions (or methods) into one unit called class For baking cookies we can design a model (or class) called Baker (or x )

Object Oriented Programming


PSEUDOCODE: Baker:

Properties (ingredients)

Flour Butter Sugar Eggs bakeCookies

Functions (actions)

Gather ingredients (flour, butter, sugar, eggs, etc.)

Object Oriented Programming (OOP)


Object oriented programming models the Real World Why modelling is important? In the physical world we deal with objects such as people and cars. Such objects arent like data and they arent like functions. Complex real-world objects have both attributes and behavior.

Object Oriented Programming


The fundamental idea is to combine into a single unit both data and functions that operate on the data. Such a unit is called an Object. The definition of this unit is called a Class. An objects functions are called member functions in C++. And its data are called members.

Procedural vs Object Oriented


Click to edit Master text styles
Second level

Click to edit Master text styles


Second level

Third level

Fourth level Fifth level

Third level
Fourth level Fifth level

Objects and Classes


Objects belong to classes All objects with the same characteristics (data and functions) constitute one class. A class serves as a plan, or a template, or sketch. It merely specifies what data and what functions will be included in the objects of that class. A class consists of data items and

Objects and Classes


Declaring a class doesnt create any objects, just as mere existence of data type int doesnt create any variables. Remember A class is thus a description of a no. of Structures?? similar objects. For instance, HUMAN is a class, and JOHN is its instance (object)
Example OOP

Objects and Classes


Declaring a class doesnt create any objects, just as mere existence of data type int doesnt create any variables. A class is thus a description of a no. of similar objects. For instance, HUMAN is a class, and JOHN is its instance (object)
Example OOP

Encapsulation
Member functions in C++, typically provide the only way to access the data items in a class. To read a data item in an object, its member function in the object is called. It accesses the data and returns the value. The data cannot be accessed directly. Thus data and its function are said to be Encapsulated into a single entity.

Inheritance
Derive other classes from an existing class Each class shares common characteristics with the class from which it was derived, and can also add its own modifications, additions. For instance, VEHICLE is a class from which CAR, TRUCK, BUS, MOTORCYCLE classes can be derived.

Inheritance
The original class is called the BASE CLASS; the others are DERIVED CLASSES Re-usability Language extensibility (create new data types)

Reusability
Once a class has been written, created, and debugged, it can be distributed to other programmers for use in their own programs. This is called reusability.

Polymorphism and Overloading


Using operators or functions in different ways depending on what they are operating on is called polymorphism Overloading is a special case of polymorphism, e.g. +, -, /, << etc.

Overloaded Functions
An overloaded function performs different activities depending on the kind of data sent to it Different no. of arguments Different types of arguments Or both

Classes
User-Defined data type Model real world Data and Functions are encapsulated into one entity An instance of a class is known as an object

class smallobj {private: int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } };

class smallobj {private: keyword int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } };

class smallobj

Class name

{private: keyword int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } };

class smallobj

Class name

{private: keyword int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } };

semicolon

class smallobj {private: int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } }; Access Specifiers

class smallobj {private: int somedata; public: void setdata(int s) {somedata = s; } void showdata()

visible to class members and member functions only!

{cout << "Data is " << somedata << endl; } };

class smallobj {private: int somedata; public: visible to the entire program!

void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } };

class smallobj {private: int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } }; class data

class smallobj {private: int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } }; member function to set data

class smallobj {private: int somedata; public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; } }; member function to show data

class smallobj

Class name

Hidden {private: data keyword int somedata; Public functions public: void setdata(int s) {somedata = s; } void showdata() {cout << "Data is " << somedata << endl; semicolon } };

class smallobj

Class name

Hidden {private: data keyword int somedata; Public functions public: void setdata(int s) {somedata = s; } void showdata()

Set hidden variable somedata equal to external variable s

{cout << "Data is " << somedata << endl; semicolon } };

class smallobj

Class name

Hidden {private: data keyword int somedata; Public functions public: void setdata(int s) {somedata = s; } void showdata()

Set hidden variable somedata equal to external variable s

Display value of hidden variable {cout << "Data is " << somedata << endl; somedata semicolon

} };

Data Hiding
An important characteristic of OOP Keeps data hidden inside the class to keep it safe from functions outside the class Only member functions can access it Access Specifier Private

Can only be accessed from within the class

Public

Objects
To use a class in a c++ program, its objects must be instantiated Similar to declaring a variable

void main() {int s=10; smallobj small;

small.setdata(5); small.showdata(); }

void main() {int s=10;

name of the object An object of the class smallobj

smallobj small; name of the class small.setdata(5); small.showdata(); }

Topics covered
Analysis of Procedural vs. Object Oriented approach Characteristics of Object Oriented programming Objects and classes: fundamentals of an OOP program

Você também pode gostar