Download presentation
Presentation is loading. Please wait.
Published byNatalie Richards Modified over 9 years ago
1
Classes Representing Non-Trivial Objects
2
Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature in both scales.
3
Preliminary Analysis An object must be directly representable using a single type. A temperature has two attributes: –its magnitude (a double), and –its scale (a character). When an object cannot be directly represented by any of the available types, build a class!
4
Building Classes Begin by defining variables to store the attributes of the object being represented (a temperature) in a class header file (e.g., Temperature.h): double myMagnitude; char myScale; Pretending that we are the temperature object, we begin the name of each attribute with the prefix, to reinforce this internal perspective. Pretending that we are the temperature object, we begin the name of each attribute with the prefix my, to reinforce this internal perspective.
5
Building Classes We then wrap these variables in a class declaration: class Temperature { public: private: double myMagnitude; char myScale; }; When declared within a class declaration, such variables are called class data members.
6
Information Hiding Classes have a public section and a private section. Items declared in the public section are accessible to users of the class; items declared in the private section are inaccessible to users of the class. Data members should go in the private section, to prevent programmers from writing programs that access data members directly.
7
Why? Software must often be updated. A program that uses a class should still work after the class has been updated. Updating a class may require that its data members be replaced by others. If a program accesses class data members directly, then that program “breaks” if they are replaced. This increases software maintenance costs.
8
A New Type A programmer can now write: #include “Temperature.h” // class Temperature... Temperature aTemp; and object can be visualized as follows: and object aTemp can be visualized as follows: myMagnitude myScale aTemp The data members and within are uninitialized. The data members myMagnitude and myScale within aTemp are uninitialized.
9
Operations and Messages Operations on a class object are usually implemented as class function members. A call to a function member: Object.Function() can be thought of as the caller sending the object a message named. can be thought of as the caller sending the object Object a message named Function. The definition of a function member details what does in response to the message. The definition of a function member details what Object does in response to the message.
10
Example: Output Suppose we want to be able to display the value of a object by sending it the message: Suppose we want to be able to display the value of a Temperature object by sending it the Print() message: aTemp.Print(cout); Using the internal perspective (where I am the object receiving the message): Using the internal perspective (where I am the Temperature object receiving the message):Specification: Receive: out, an ostream. Output: myMagnitude and myScale, via out. Passback: out, containing the new values.
11
Function Member Definitions void Temperature::Print(ostream & out) const { out << myMagnitude << myScale; } The function returns nothing, so its return-type is. The function returns nothing, so its return-type is void. The full name tells the compiler this is a function member. The full name Temperature::Print() tells the compiler this is a Temperature function member. The caller passes and changes an argument, so we need a reference parameter to store this argument. The caller passes and changes an ostream argument, so we need a reference parameter to store this argument. Function members that change no data members should be defined as function members. Function members that change no data members should be defined as const function members. In Temperature.cpp:
12
Function Member Prototypes class Temperature { public: void Print(ostream & out) const; private: double myMagnitude; char myScale; }; By declaring the prototype within the class, we tell the compiler that class has a function member named, and that objects should “understand” the message. By declaring the prototype within the class, we tell the compiler that class Temperature has a function member named Print(), and that Temperature objects should “understand” the Print() message. Most function prototypes go in the class public section.
13
Problem At present, a declaration: At present, a Temperature declaration: Temperature aTemp; leaves ’s data members of uninitialized. leaves aTemp ’s data members of uninitialized. To auto-initialize them to a default value (e.g., ), we can use a special function called a constructor. To auto-initialize them to a default value (e.g., 0C ), we can use a special function called a constructor.
14
Constructors A constructor function is a class function member whose task is to initialize the class data members. Because they just initialize data members, they don’t return anything, and so their specification is often given as a postcondition (a boolean expression that is true when the function terminates). Default Constructor Specification: Postcondition: myMagnitude == 0.0 && myScale == ‘C’.
15
Constructor Definition Since it returns nothing, a constructor has no return type (not even ). Since it returns nothing, a constructor has no return type (not even void ). The name of a constructor is always the name of the class (in this case ). The name of a constructor is always the name of the class (in this case Temperature() ). As a function member of class, its full name is. As a function member of class Temperature, its full name is Temperature::Temperature(). Temperature::Temperature() { myMagnitude = 0.0; myScale = ‘C’; }
16
Constructor Prototype class Temperature { public: Temperature(); void Print(ostream & out) const; private: double myMagnitude; char myScale; }; Since they specify the first thing a user of the class needs to know (i.e., how to define class objects), constructor prototypes are usually the first function members listed in the public section of the class.
17
Object Definitions A programmer can now write: Temperature aTemp; and object can be visualized as follows: and object aTemp can be visualized as follows: The class constructor is automatically called whenever a class object is defined. C myMagnitude myScale aTemp 0.0
18
Testing To test this much, we can write: //... documentation //... other #includes #include “Temperature.h” int main() { Temperature aTemp; aTemp.Print(cout); // displays 0C }
19
Problem 2 At present, we can only initialize a Temperature to a default value: Temperature aTemp; We have no means of initializing a to any other value. We have no means of initializing a Temperature to any other value. To initialize a to a particular value (e.g., ), we can overload the constructor with a second definition. To initialize a Temperature to a particular value (e.g., 98.6F ), we can overload the constructor with a second definition.
20
Constructor 2 To overload the constructor, we just provide a second definition (and prototype) that differs from all other constructors in at least one parameter. To initialize the data members of our class, this second constructor must receive the initial values via its parameters. Constructor 2 Specification: Receive: magnitude, a double; scale, a char. Precondition: scale == ‘F’ || scale == ‘C’. Postcondition: myMagnitude == magnitude && myScale == scale. myScale == scale.
21
Constructor 2 Definition As before, we place this (second) constructor definition in the implementation file -- Temperature.cpp. Temperature::Temperature(double magnitude, char scale) { assert(scale == ‘F’ || scale == ‘C’); myMagnitude = magnitude; myScale = scale; }
22
Constructor 2 Prototype class Temperature { public: Temperature(); Temperature(double magnitude, char scale); void Print(ostream & out) const; private: double myMagnitude; char myScale; }; The same name can be used to define different functions, provided the signature (the list of the parameter types) of each function is different.
23
Object Definitions A programmer can now write: Temperature temp1, temp2(98.6, ‘F’); and and are defined as follows: and temp1 and temp2 are defined as follows: The compiler uses the number of arguments in a declaration to decide which constructor to use in initializing an object. C myMagnitude myScale temp1 0.0 F myMagnitude myScale temp2 98.6
24
Testing To test this much, we can write: //... documentation //... other #includes #include “Temperature.h” int main() { Temperature temp1, temp2(98.6, ‘F’); temp1.Print(cout); // displays 0C cout << endl; temp2.Print(cout); // displays 98.6F }
25
Summary When no existing type is appropriate to represent an object, the C++ class allows a type to be created. Classes have data members (private), and function members (public). In its definition, a function member’s name must be preceded by the name of the class and the scope operator (::). Class constructor functions allow class objects to be initialized to default, or to specified values.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.