Object Oriented Programming and the C Programming Language

Object Oriented Programming and the C++ Programming Language

Object Oriented Programming is an attempt to model the world in a programmatic language * It does so by classifying all objects according to their attributes and behaviour. * At the base of Object Oriented Programming is the classification, or Class.

Why classify our world? * It is a question of organization. * We need to break our programs down into manageable chunks. * Computer programming is growing in complexity and size at an alarming rate.

The Evolution of Object Oriented Programming Non-structured Programming • Historically the earliest form of programming. • Producing hardly-readable ("spaghetti") code and is considered a bad approach for creating major projects, • Had been praised for the freedom it offers to programmers. Structured Programming • Structured programming takes on the top-to-bottom approach. • Makes extensive use of subroutines, block structures, for and while loops -in contrast to using simple tests and jumps such as the goto statement. • Structured programming is based around data structures and subroutines. Object Oriented Programming • Evolved from Structured programming. • It splits the program into objects that can be reused in other programs. • Each object or module has the data and the instruction of what to do with the data within it. This can be reused in other software.

Object Oriented Programming is based on Classes and Objects. What are these and what are the differences between the two? A class is a general blueprint or template or set of instructions to build a specific type of object. A class contains data and functions that act on that data. Some of the data and functions are visible to the outside world, some are not. Every object is built from a class. An instance is a specific object built from a specific class. An object is not a copy of a class, it is an instance of a class. Each class should be designed to accomplish one, and only one, thing. Because each class is designed to have only a single responsibility, many classes are used to build an entire application.

Examples of Classes and Objects Which is a class (classification) and which are objects (a concrete implementation): Toyota Corolla LE, Honda Civic DX, Automobile, Ford Fusion Hybrid S Apple i. Phone 5 s, Smartphone, Samsung Galaxy S, Blackberry 10 Passport Desktop_PC, HP Pavilion 500, Dell Inspiron Micro, Lenovo Think. Center M 900 Oranges, Lemons, Grapefruit, Limes, Citrus_Fruit

The Automobile Class Adjectives that describe an automobile (variables): Actions that an automobile performs (functions):

The Automobile Class Variables: string model; string make; string colour; int year; //four digits of the form yyyy double fuel. Efficiency; //measured in liters/100 km double fuel. In. Tank; //measured in liters. Functions: <<constructor>>Automobile(string _make, string _model, string _colour, int _year); void set. Fuel. Efficiency(double _efficiency); void add. Fuel(double _liters); void drive(double _distance); //in kilometers void display. Report();

class Automobile { private: string make; string model; string colour; int year; double fuel. Efficiency; double fuel. In. Tank; public: Automobile(string _make, string _model, string _colour, int _year){ make = _make; model = _model; colour = _colour; year = _year; fuel. In. Tank = 0; }; void set. Fuel. Efficiency(double _efficiency) { fuel. Efficiency = _efficiency; }; void add. Fuel(double _liters) { fuel. In. Tank += _liters; //ignore tank capacity }; void drive(double _distance) { double fuel. Consumed = fuel. Efficiency / 100 * _distance; fuel. In. Tank -= fuel. Consumed; }; void display. Report() { cout << "The " << colour << " " << year << " " << make << " " << model << " has " << fuel. In. Tank << " left in the tank" << endl; }; };

int main() { Automobile* car 1 = new Automobile("Toyota", "Corolla", "grey", 2013); Automobile* car 2 = new Automobile("Honda", "Civic", "red", 2012); car 1 ->add. Fuel(50. 0); car 2 ->add. Fuel(50. 0); //Set fuel efficiency for city driving then drive 200 km car 1 ->set. Fuel. Efficiency(8. 2); car 1 ->drive(200. 0); car 2 ->set. Fuel. Efficiency(7. 8); car 2 ->drive(200. 0); //Set fuel efficiency for highway driving then drive 300 km car 1 ->set. Fuel. Efficiency(6. 2); car 1 ->drive(300. 0); car 2 ->set. Fuel. Efficiency(5. 8); car 2 ->drive(300. 0); //display the results car 1 ->display. Report(); car 2 ->display. Report(); delete(car 1); delete(car 2); return 0; } ====== Results ====== The grey 2013 Toyota Corolla has 15. 00 liters left in the tank The red 2012 Honda Civic has 17. 00 liters left in the tank

The Automobile Class - Considerations • The main() function does not know the internal workings of the Automobile class. • The main() function uses what the Automobile class exposes: – Constructor for the Automobile class – set. Fuel. Efficiency() – add. Fuel() – drive() – display. Report() • The main() function does not know how the Automobile class measures fuel consumption. • The Automobile class could be hundreds of lines of code, but this is irrelevant to the developer since (s)he only needs to concentrate on writing code for main().

Packages • Many classes can be grouped together to form a “package” or a “library”. • These “packages/libraries” can be imported into any program and used as a blackbox. • This allows the development of thousands perhaps millions of lines of code where the developer only needs to concentrate on a particular part of the code at any one time.

Other Object Oriented Languages C++ is not the only one. Many languages, from compiled to interpreted have now adopted the object oriented approach. Here a few: Ada 95, C#, Java, Object Pascal, Perl since v 5, PHP 5, Python, Ruby, Swift, Visual Basic, etc…
- Slides: 13