Presentation is loading. Please wait.

Presentation is loading. Please wait.

This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.

Similar presentations


Presentation on theme: "This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson."— Presentation transcript:

1 This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, 2000. COMP103 - Classes1 Classes (Chapter 10) Classes are “derived” types. They are combination of data and functions defined together to form a type.

2 COMP103 - Classes2 Motivation Types such as int, double, and char are “simple” objects. They can only answer one question: “What value do you contain?” 3!

3 COMP103 - Classes3 Motivation Classes allow you to build “smart” objects that can answer many questions (and perform various actions). For example: “What is your temperature?” “What is your temperature in Fahrenheit?” “Print your temperature in Kelvin.”

4 COMP103 - Classes4 “Temperature” Example Write a program that, given a temperature in Fahrenheit, Celsius, or Kelvin, 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;

5 COMP103 - Classes5 “Temperature” Example Is there a way to build a user-defined data type that combines degree and scale into one object? Can this object automatically convert between different scales, and know how to print itself out? (Can we construct a “smart” object?) Answer: Yes, by using the class construct.

6 COMP103 - Classes6 Design and build a class to represent the temperature object Identify: 1) data required (data members), and 2) operations that this object can perform (member functions) class Temperature { public: // member functions private: double degree; // data members char scale; }; Object-Oriented Solution

7 COMP103 - Classes7 Access Rights Control the level of information hiding Accessible to member functions within the class only (private) Accessible to any functions (public) protected: we will explain it in next chapter

8 COMP103 - Classes8 Figure 10-2 Access Specifiers external direct access

9 COMP103 - Classes9 #include // definition of Temperature class goes here void main() { Temperature temp; // Temperature is a class type cout << "Enter temperature (e.g., 98.6 F): "; temp.read(); cout "; temp.Fahrenheit(); temp.print();cout << " = "; temp.Celsius(); temp.print();cout << " = "; temp.Kelvin(); temp.print(); } “Temperature” Conversion Program

10 COMP103 - Classes10 Smart “Temperature” Object A smart object should carry within itself the ability to perform its operations Operations of Temperature object : initialize degree and scale with default values read a temperature from the user and store it compute the corresponding Fahrenheit temperature compute the corresponding Celsius temperature compute the corresponding Kelvin temperature display the degrees and scale to the user

11 COMP103 - Classes11 “Temperature” Class Declaration class Temperature { public: void read(); void print(); void Fahrenheit(); void Celsius(); void Kelvin(); private: double degree; char scale; };

12 COMP103 - Classes12 Class Instantiation void main() { Temperature temp1; Temperature temp2;... } temp1:temp2:degreescale

13 COMP103 - Classes13 Function: Printing “Temperature” A programmer can write: temp1.print(); temp2.print(); Smart object interpretation: temp1: Receives print() message and displays values stored in degree and scale temp2: Receives print() message and displays values stored in degree and scale

14 COMP103 - Classes14 Associating Functions with Classes :: scope resolution operator Syntax :: Allows same member_name under different class_name Example: void Temperature::print() (Example01.cpp) void Fraction::print() (p.493 of text)

15 COMP103 - Classes15 Function Printing “Temperature” void Temperature::print() { cout << degree << " " << scale; } Remark: Member functions of a class can access the private data members of their class, but normal functions cannot.

16 COMP103 - Classes16 Manager Functions (Section 10-3 of text) Special Functions: Constructors – they are called when an instance of a class is created. Copy constructors – they are called when a copy of an existing instance of a class needs to be created. Destructors – they are the opposite of constructors, i.e. when a class (object) dies.

17 COMP103 - Classes17 Constructors Constructors are member functions of a class A constructor of an object is invoked (called) once upon the creation of the object Constructor name and class name are the same Syntax: className(parameter list); // Declaration className::className(parameter list) // Definition {... } // className construction function

18 COMP103 - Classes18 Default Constructor Example: A constructor function initializes the data members when a Temperature object is declared. Temperature::Temperature() { degree = 0.0; scale = 'C'; } void main() { Temperature temp1; temp1.print(); }

19 COMP103 - Classes19 “Temperature” Class class Temperature { public: Temperature(); // default constructor void read(); void print(); void Fahrenheit(); void Celsius(); void Kelvin(); private: double degree; char scale; };

20 COMP103 - Classes20 Default Constructor Remarks: Constructor functions have no return type (not even void!) Example: Temperature(); The constructor function is automatically called whenever a Temperature class object is declared. Example: Temperature temp1;

21 COMP103 - Classes21 Explicit-Value Constructor An explicit-value constructor initializes the data members when a Temperature object is declared with parameters: Temperature temp3(98.6, 'F'); Explicit Value Constructor definition: Temperature::Temperature(double d, char s) { degree = d; scale = s; if(scale!='C' && scale!='F' && scale!='K') { cout << "Bad Temperature scale: " << scale << endl; }

22 COMP103 - Classes22 Temperature Class class Temperature { public: Temperature(); // default constructor // explicit value constructor Temperature(double idegree, char iscale); void read(); void print(); void Fahrenheit(); void Celsius(); void Kelvin(); private: double degree; char scale; };

23 COMP103 - Classes23 Weird C++ Syntax The default constructor is a function with no parameters so you might think that it should actually be called using Temperature temp1(); the same way as any other function without parameters. This is not correct. A default constructor should be called as Temperature temp1; without using the ().

24 COMP103 - Classes24 Reading Temperature Using the read() member function: Temperature temp1; cout << "Enter temperature (e.g., 98.6 F): "; temp1.read(); // process the temperature in temp1 When temp1 receives the read() message, it gets values from cin into degree and scale. void Temperature::read() { cin >> degree >> scale; if(scale!='C' && scale!='F' && scale!='K') cout << "Bad Temperature scale: " << scale << endl; }

25 COMP103 - Classes25 Conversion Functions The member function Fahrenheit() changes the degree and scale of the member data to Fahrenheit. void Temperature::Fahrenheit() { if(scale == 'C') degree = degree*1.8+32.0; else if(scale == 'K') degree = (degree-273.15)*1.8 + 32.0; scale = 'F'; }

26 COMP103 - Classes26 Conversion Functions The functions Celsius() and Kelvin() are similar. void Temperature::Celsius() { if(scale == 'F') degree = (degree-32.0)/1.8; else if(scale == 'K') degree = degree - 273.15; scale = 'C'; } void Temperature::Kelvin() { if(scale == 'F') degree = (degree-32.0)/1.8 + 273.15; else if(scale == 'C') degree = degree + 273.15; scale = 'K'; }

27 COMP103 - Classes27 Conversion Functions Calling the Fahrenheit() member function: Temperature temp1;// default value: 0 C temp1.Fahrenheit(); temp1.print();// prints: 32 F When temp1 receives the Fahrenheit() message, it converts to the Fahrenheit temperature 32 F.

28 COMP103 - Classes28 Calling Member Functions from Member Functions We can simplify the main() program by building a member function printAll() to print the temperature in all scales: void main() { Temperature temp; cout << "Enter temperature (e.g., 98.6 F): "; temp.read(); temp.printAll(); // prints temperature in F, C, K }

29 COMP103 - Classes29 Calling Member Functions from Member Functions This member function calls the other member functions: void Temperature::printAll() { // prints temperature in all scales cout "; // calls member function Fahrenheit() Fahrenheit(); print();cout << " = "; Celsius(); print();cout << " = "; Kelvin(); print(); }

30 COMP103 - Classes30 “Temperature” Class class Temperature { public: Temperature(); Temperature(double idegree, char iscale); void read(); void print(); void printAll(); void Fahrenheit(); void Celsius(); void Kelvin(); private: double degree; char scale; };

31 COMP103 - Classes31 Copy Constructor Syntax Declaration: className (const className &variable); Definition: className::className(const className &variable) {... } // className copy constructor Example: void main() { // explicit value constructor Temperature t1(100, ‘F’); Temperature t2(t1); // t2 gets the value of t1... }

32 COMP103 - Classes32 Figure 10-9 Bitwise & Logical Copy

33 COMP103 - Classes33 Destructor The destructor of an object is called upon for its deletion. It is usually used to release memory allocated to that object. Syntax: Declaration - ~className( ); Definition – className:: ~className() {... }

34 COMP103 - Classes34 Example of destructor class MyArray // class definition { private: int size; int *a; public: MyArray(int aSize); ~MyArray(); }; MyArray::MyArray(int aSize) { // default constructor a = new int[aSize]; } MyArray::~MyArray() { // destructor delete [] a; }

35 COMP103 - Classes35 Summary Classes/Objects Allow you to combine data and functions together (to create smart objects) Member data, member functions of an object Scope operator :: Used to denote a member function is part of a classes Special Manager functions Constructors Called when object is created Copy Constructors Called when a copy of an existing instance needs to be created Destructor Called when object is “destroyed”


Download ppt "This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson."

Similar presentations


Ads by Google