OoP/ Slide 11 An application example Temperature annualtemp [12]i double annualAverageCelsius(Temperature arraytemp[]) double av=0.0 for (int 1=0;i<12; i++)av=av+celsius(arraytemp[i]) return av };
OOP / Slide 11 An application example: double annualAverageCelsius(Temperature arraytemp[]) { double av=0.0; for (int i=0;i<12;i++) av=av+celsius(arraytemp[i]); return av; }; Temperature annualtemp[12];
OoP/ Slide 12 Put the variables and functions together Actual problem 1. Member 'variablesare still separated from functions'manipulating these variables. 2. However, functions are intrinsically related to the type The sim plest class or a C-structure) defined this way is a collection of (member) variables( similarto RECORD in Pascal) A more advanced class is a collection of (member) variables and(member) functions The art of programming is the art of organising complexity
OOP / Slide 12 Put the variables and functions together … Actual problem: 1. Member ‘variables’ are still separated from ‘functions’ manipulating these variables. 2. However, ‘functions’ are intrinsically related to the ‘type’. A more advanced class is a collection of (member) variables and (member) functions The simplest class (or a C-structure) defined this way is a collection of (member) variables (similar to RECORD in Pascal) “The art of programming is the art of organising complextity
An improved Temperature class with member functions associated Assembly the data and operations together into a class class Temperature public: void print ()i // member functions double celsius o double fahrenheit double degree; / member variables char scale
OOP / Slide 13 Assembly the data and operations together into a class! class Temperature{ public: void print(); // member functions double celsius(); double fahrenheit(); double degree; // member variables char scale; }; An improved Temperature class with member functions associated
OoP/ Slide 14 Operators for members The dot operator not only for public member variables of an object, but also for public member functions(during usage),e.g Temperature templi templ celsius ()i function→ method templ print ()i Function(procedure)call> message Comments: 1. Temp1 receives print()message and displays values stored in degree and scale, receives celsius( message to give the temperature in celsius 2. It is not the function which is calling the object like print(temp 1)traditionally, temp 1. print(> object oriented 3. The temperature are 'smart objects unlike 'stupid basic type objects
OOP / Slide 14 Operators for members The dot operator not only for public member variables of an object, but also for public member functions (during usage), e.g. Temperature temp1; temp1.celsius(); temp1.print(); 1. Temp1 receives print() message and displays values stored in degree and scale, receives celsius() message to give the temperature in celsius … 3. The temperature are ‘smart objects’ ☺ unlike ‘stupid’ basic type objects Comments: 2. It is not the function which is calling the object like print(temp1) traditionally, temp1.print() → object oriented! function → method Function(procedure) call → message
OoP/ Slide 15 Operators for defining member functions a: for member functions of a class during definition) double celsius(double degree, char scale) From the class, notfrom an object double temperature:: celsius double cel If (scale==R) cel=(degree-320)/1.8; else cel=degree return cel Full name of the function is used with a class name while dot operator is with an object!
OOP / Slide 15 double Temperature::celsius() { double cel; If (scale==‘F’) cel= (degree-32.0)/1.8; else cel=degree; return cel; } :: for member functions of a class (during definition) Operators for defining member functions :: is used with a class name while dot operator is with an object! From the class, not from an object Full name of the function double celsius(double degree, char scale)