Classes - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Classes

Description:

COMP103 - Classes. 1 ... COMP103 - Classes. 2. Motivation. Types such as int, ... Classes allow you to build 'smart' objects that can answer many questions (and ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 36
Provided by: ValuedGate953
Category:
Tags: classes

less

Transcript and Presenter's Notes

Title: Classes


1
  • Classes
  • (Chapter 10)

Classes are derived types. They are
combination of data and functions defined
together to form a type .
2
Motivation
  • Types such as int, double, and char are simple
    objects.
  • They can only answer one question What value do
    you contain?

3!
3
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
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 ltlt degree ltlt scale

5
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
Object-Oriented Solution
  • 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

7
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
Access Specifiers
Figure 10-2
direct access
external
9
Temperature Conversion Program
  • include ltiostreamgt
  • // definition of Temperature class goes here
  • void main()
  • Temperature temp
  • // Temperature is a class type
  • cout ltlt "Enter temperature (e.g., 98.6 F) "
  • temp.read() cout ltlt "--gt"
  • temp.Fahrenheit()
  • temp.print() cout ltlt " "
  • temp.Celsius()
  • temp.print() cout ltlt " "
  • temp.Kelvin()
  • temp.print()

10
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
Temperature Class Declaration
  • class Temperature
  • public
  • void read()
  • void print()
  • void Fahrenheit()
  • void Celsius()
  • void Kelvin()
  • private
  • double degree
  • char scale

12
Class Instantiation
  • void main()
  • Temperature temp1
  • Temperature temp2
  • . . .
  • temp1 temp2
  • degree degree
  • scale scale

13
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
Associating Functions with Classes
  • scope resolution operator
  • Syntax
  • ltclass_namegt ltmember_namegt
  • Allows same member_name under different
    class_name
  • Example
  • void Temperatureprint() (Example01.cpp)
  • void Fractionprint() (p.493 of text)

15
Function Printing Temperature
  • void Temperatureprint()
  • cout ltlt degree ltlt " " ltlt scale
  • Remark
  • Member functions of a class can access the
    private data members of their class, but normal
    functions cannot.

16
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
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
  • classNameclassName(parameter list) //
    Definition
  • . . .
  • // className construction function

18
Default Constructor
  • Example
  • A constructor function initializes the data
    members when a Temperature object is declared.
  • TemperatureTemperature()
  • degree 0.0
  • scale 'C'
  • void main()
  • Temperature temp1
  • temp1.print()

19
Temperature Class
  • class Temperature
  • public
  • Temperature() // default constructor
  • void read()
  • void print()
  • void Fahrenheit()
  • void Celsius()
  • void Kelvin()
  • private
  • double degree
  • char scale

20
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
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
  • TemperatureTemperature(double d, char s)
  • degree d
  • scale s
  • if(scale!'C' scale!'F' scale!'K')
  • cout ltlt "Bad Temperature scale " ltlt scale ltlt
    endl

22
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
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
Reading Temperature
  • Using the read() member function
  • Temperature temp1
  • cout ltlt "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 Temperatureread()
  • cin gtgt degree gtgt scale
  • if(scale!'C' scale!'F' scale!'K')
  • cout ltlt "Bad Temperature scale " ltlt scale ltlt
    endl

25
Conversion Functions
  • The member function Fahrenheit() changes the
    degree and scale of the member data to
    Fahrenheit.
  • void TemperatureFahrenheit()
  • if(scale 'C')
  • degree degree1.832.0
  • else if(scale 'K')
  • degree (degree-273.15)1.8 32.0
  • scale 'F'

26
Conversion Functions
  • The functions Celsius() and Kelvin() are similar.
  • void TemperatureCelsius()
  • if(scale 'F')
  • degree (degree-32.0)/1.8
  • else if(scale 'K')
  • degree degree - 273.15
  • scale 'C'
  • void TemperatureKelvin()
  • if(scale 'F')
  • degree (degree-32.0)/1.8 273.15
  • else if(scale 'C')
  • degree degree 273.15
  • scale 'K'

27
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
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 ltlt "Enter temperature (e.g., 98.6 F) "
  • temp.read()
  • temp.printAll() // prints temperature in F, C,
    K

29
Calling Member Functions from Member Functions
  • This member function calls the other member
    functions
  • void TemperatureprintAll()
  • // prints temperature in all scales
  • cout ltlt "--gt"
  • // calls member function Fahrenheit()
  • Fahrenheit()
  • print() cout ltlt " "
  • Celsius()
  • print() cout ltlt " "
  • Kelvin()
  • print()

30
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
Copy Constructor
  • Syntax
  • Declaration
  • className (const className variable)
  • Definition
  • classNameclassName(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
Bitwise Logical Copy
Figure 10-9
33
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
Example of destructor
  • class MyArray // class definition
  • private
  • int size
  • int a
  • public
  • MyArray(int aSize)
  • MyArray()
  • MyArrayMyArray(int aSize)
  • // default constructor
  • a new intaSize
  • MyArrayMyArray()
  • // destructor
  • delete a

35
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
Write a Comment
User Comments (0)
About PowerShow.com