Академический Документы
Профессиональный Документы
Культура Документы
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.
3.
i. ii.
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
Ref: http://www.youtube.com/watch?v=c5kfCH50wl0
Procedural Programming
PSEUDOCODE:
Gather ingredients (flour, butter, sugar, eggs, etc.) Preheat oven Beat eggs and butter Add flour and sugar Mix Bake for 10 mins.
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 )
Properties (ingredients)
Functions (actions)
Third level
Third level
Fourth level Fifth level
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.
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()
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()
class smallobj
Class name
Hidden {private: data keyword int somedata; Public functions public: void setdata(int s) {somedata = s; } void showdata()
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
Public
Objects
To use a class in a c++ program, its objects must be instantiated Similar to declaring a variable
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