1 Object-Oriented Programming OOP
2 Object-oriented programming is a new paradigm for program development in which the focus is on the data instead of the functions and control flow.
3 All programming can be considered as the process of modeling objects in the real world (or now, in virtual worlds).
4 The word “objects” is being used in a very broad sense here. Cars, elevators, and dice can be objects, but so also can dates, times and addresses.
5 If we seek to model a car, we try to capture 2 aspects of “carness” – attributes and capabilities.
6 Attributes are characteristics of a car that we want to deal with while ignoring other characteristics that do not matter to us in our modeling. -- Abstraction – e.g. year, make, model, color, mileage, doors, horsepower,…
7 Capabilities are actions performed by, for or on an object. They often have the effect of changing one or more attributes of an object. For a car, this might be – drive, stop, go, fill up, change gears, lock, paint, wreck, …
8 In OOP, rather than define a particular car like my Canery, we define a class called Car. That class is a template that we can use to construct my Camery and your Jeep …
9 When we define a class we are really defining a new data type like int, char, float, … It’s just one that YOU create from scratch and decide how it works and what it will do. Actually, the strings we have been using are a class that a programmer developed.
10 Abstract Data Type (ADT) l An abstract data type is a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
11 When we designate an attribute we have to specify a domain of values that that attribute can have. For Color, the domain might be {Red, Blue, Green, Yellow, Orange} For Mileage the domain would be float.
Several Possible Representations of ADT Time 3 int variables 3 strings 3-element int array Choice of representation depends on time, space, and algorithms needed to implement operations “10” “45” “27”
13 OK, now for some examples.
14 class cars { private: double gas_mileage; //gas mileage in mpg double mileage; //odometer reading double gas_volume; //gas volume in gallons public: cars(double, double, double); //constructor func. public: ~cars(void); //destructor function void drive(double); //prototype for drive function }; Data members Function members
15 The word public means that this information is exposed to other programs can refer to it. Private means that this information is not available to other programs. This is a lot like local scope. Generally, data members are private and function member prototypes are public. However, the actual definitions of the function members is not available to any program outside the class definition. This means that the implementation of these member functions can be changed without having any possible effect on any program that makes use of the class, except for speed of execution (assuming a correct program). Keeping this information hidden is sometimes called encapsulation.
16 cars::cars(double new_gas_mileage, double new_mileage, double new_gas_volume) { gas_mileage = new_gas_mileage; mileage = new_mileage; gas_volume = new_gas_volume; }
17 void cars::drive(double distance) //this function results in the car being driven //distance miles. It must modify the internal variables //accordingly { //amount of gas cosumed double g_consum = distance/gas_mileage; if (g_consum < gas_volume) { mileage += distance; //increase mileage on odometer gas_volume -= g_consum; } else { mileage += gas_volume*gas_mileage; gas_volume = 0; }
18 void main() { cars civic(39, 0, 10); //construct a Honda Civic object civic.drive(300); } Here we use the cars constructor to create an instance (also called class instance, or object instance or object) called civic. civic is an object variable. You can create as many object instances of a class as you want.
19 One last characteristic of OOP that I will mention is called Inheritance. Inheritance allows a programmer to extend the definition of a class to extend or specialize it, JUST by adding the new data members and function members that are needed. So, from the Car class, we could derive a new class Hybrid, by adding just the new stuff and inheriting from the old class Car.
20 C++ classType l Facilitates re-use of C++ code for an ADT l Software that uses the class is called a client l Client code uses class’s public member functions to manipulate class objects