OoP/ Slide 6 Temperature example Write a program that, given a temperature in Fahrenheit or Celsius, will display the equivalent temperature in each of the scales double degree=0.0; / needs 2 items char scale F To apply a function f() to a temperature, we must specify both degree and scale f(degree, scale)i Also to display a temperature 197 8202(Putonghua) cout < degree < scale 1878066( English)
OOP / Slide 6 Temperature example Write a program that, given a temperature in Fahrenheit or Celsius, will display the equivalent temperature in each of the scales. double degree = 0.0; // needs 2 items! char scale = 'F'; To apply a function f() to a temperature, we must specify both degree and scale: f(degree, scale); Also to display a temperature: cout << degree << scale;
OoP/ Slide 7 Put related variables together (remember that an array is a collection of variables of same type) The simpliest Class (or a C-structure) can be thought of being a collection of variables of different types
OOP / Slide 7 (remember that an Array is a collection of variables of same type) The simpliest Class (or a C-structure) can be thought of being a collection of variables of different types Put related variables together …
OoP/ Slide 8 a first simple class or object-oriented' solution class Temperature i public double degree Two mem ber variables degree and scale char scale;← a new(user-defined)type, a composite ty pe: Temperature! Remark: structure Temperature i In old C, this can be done using 'structure double degree similar to 'recordin Pascal char scale
OOP / Slide 8 A first simple ‘class’ or ‘object-oriented’ solution class Temperature { public: double degree; char scale; }; a new (user-defined) type, a composite type: Temperature! Two member variables: degree and scale In old C, this can be done using ‘structure’: structure Temperature { double degree; char scale; }; similar to ‘record’ in Pascal Remark:
OoP/ Slide 9 The dot operator for (public) members The modifier public' means that the mem ber variables can be accessed from the objects, e. g Temperature temp 1, temp2i templ degree=54.0; temp1.sca1e=NF′; temp2 degree=104.5 temp2. scale=C′ A C++ struct is a class in which all mem bers are by default public
OOP / Slide 9 temp1.degree=54.0; temp1.scale=‘F’; temp2.degree=104.5; temp2.scale=‘C’; The dot operator for (public) members Temperature temp1, temp2; The modifier ‘public’ means that the member variables can be accessed from the objects, e.g. A C++ struct is a class in which all members are by default public
OoP/ Slide 10 Manipulation of the new type Some basic operations: void print (Temperature temp out <<The temperature is degree < temp degree < with the scale M<< temp. scale < endli double celsius (Temperature temp) double cel if (temp. scale=='Fl) cel=(temp. degree-320)/1.8; else cel=temp. degree return cel double fahrenheit(Temperature temp) double fa f(temp.scale==C)fa= temp. degree *1.8+320i else fa=temp. degree return fa
OOP / Slide 10 void print(Temperature temp) { cout << “The temperature is degree “ << temp.degree << “with the scale “ << temp.scale << endl; } double celsius(Temperature temp) { double cel; if (temp.scale==‘F’) cel=(temp.degree-32.0)/1.8; else cel=temp.degree; return cel; } double fahrenheit(Temperature temp) { double fa; if(temp.scale==‘C’) fa= temp.degree *1.8+32.0; else fa=temp.degree; return fa; } Some basic operations: Manipulation of the new type: