Chapter 8 - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 8

Description:

Chapter 8 Classes and Objects. Advantages of Using Objects ... double Parabola : : calc_area (double x1, double x2) Function call. Function header ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 23
Provided by: RalphFTo5
Category:
Tags: chapter | parabola

less

Transcript and Presenter's Notes

Title: Chapter 8


1
Chapter 8 Classes and Objects
2
Advantages of Using Objects
  • Classes form tightly knit group of functions
  • Distinct program boundaries
  • More reliable large programs
  • Reduces amount of programming done from scratch

Lesson 8.1
3
struct
  • Data type created by grouping other data types
  • Contains data members
  • Defined by programmer derived data type
  • Declares space for all data members

Lesson 8.1
4
Syntax
  • struct fluid
  • public
  • char initial
  • double temp, density, velocity

Lesson 8.1
5
Declaring struct Variables
  • Use struct name followed with names of variables
  • Example
  • Fluid water, oil

Lesson 8.1
6
Assigning Values to Data Members
  • Use dot operator with variable name to store and
    access values
  • Examples
  • water.density 9.81
  • oil.density water.density

Lesson 8.1
7
Class
  • Class is a type
  • Aggregation of both data and functions
  • General form

class Class_name private
private_members public
public_members
Lesson 8.2
8
Class Members
  • Listed as in ordinary declarations
  • Type and identifier in sequence
  • Class definition does NOT reserve memory for
    class members
  • Memory reserved when class declared
  • Cannot use storage class specifiers auto, extern
    or register
  • Can be objects of other classes

Lesson 8.2
9
Access Specifiers
  • Private
  • Restricts access of data to member functions
  • member data cannot be accessed directly from main
  • Public
  • Allows member function to be called from main or
    any other function in program

Lesson 8.2
10
Member Function Definitions
  • Those not included inside class definition
  • Require class name and scope resolution operator
    () to immediately precede function name

type class_name function header
type variable statements return
(value )
Lesson 8.2
11
Objects
  • New memory reserved for each data member of class
    when object declared
  • Encapsulation
  • Link between data and functions of object
  • data hiding data access restricted to member
    functions
  • Classes create programming units with distinct
    boundaries
  • Instantiation
  • Declaration of objects of a particular class
    (instances)

Lesson 8.2
12
Function Calls
  • Each member function called must be associated
    with an object
  • Example object_name . function_name
  • object_name called invoking object

Function call
ob1 . calc_area (left_limit,
right_limit) double Parabola calc_area
(double x1, double x2)
Function header
Lesson 8.2
13
Constructor Function
  • Class member function executed automatically upon
    declaration of object
  • Frequently used to initialize values of data
    members
  • Can be called just Constructor

Lesson 8.3
14
Constructor Characteristics
  • Name identical to class name
  • Does not return a value
  • Has public access
  • May or may not have arguments

Lesson 8.3
15
Constructor Function Form
class Name private
type data_member public
Name ( ) type
function_member ( )
Lesson 8.3
16
Construction Function Definition
Name Name ( ) data_member
value
Lesson 8.3
17
Constructor Functions - Arguments
  • Cannot return a value but can receive value
    through argument list
  • Function can assign value to data member
  • Passing a value to a constructor

Weather_station (int) Weather_station station
(5) Weather_station Weather_station (int
wind_speed_var)
Lesson 8.4
18
Initializing Data Member with Constructor
  • Initialization list which follows single colon
    written after header
  • Data member name and temporary storage variable
    name (enclosed in parentheses)
  • General Form

Class Class (type dummy)
data_member (dummy)
data member set equal to valuepassed to dummy
Lesson 8.4
19
Explicitly Calling a Constructor Function
  • Cannot call like other member functions
  • Can call constructor a second time for that
    object using an assignment statement

station1 Weather_station (10)
Right side creates nameless object that is
assigned to station1
Lesson 8.4
20
Overloading Constructor Function
  • Can have two or more constructor functions in
    class definition
  • Default copy constructor automatically executed
    when one object declared to be identical to
    another
  • Distinguishable in terms of number or type of
    arguments

Lesson 8.5
21
Overload Declarations
  • Same name with different argument lists
  • Example

Microwave_instruction ( ) Microwave_instruction
(int) Microwave_instruction (int, int)
Lesson 8.5
22
Summary
Learned how to
  • Define a class
  • Use an object
  • Select class data members
  • Select class function members
  • Create a constructor function
  • Create overloaded constructor functions
Write a Comment
User Comments (0)
About PowerShow.com