Title: Chapter 8
1Chapter 8 Classes and Objects
2Advantages 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
3struct
- 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
4Syntax
- struct fluid
-
- public
- char initial
- double temp, density, velocity
Lesson 8.1
5Declaring struct Variables
- Use struct name followed with names of variables
- Example
- Fluid water, oil
Lesson 8.1
6Assigning 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
7Class
- Class is a type
- Aggregation of both data and functions
- General form
class Class_name private
private_members public
public_members
Lesson 8.2
8Class 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
9Access 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
10Member 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
11Objects
- 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
12Function 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
13Constructor 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
14Constructor Characteristics
- Name identical to class name
- Does not return a value
- Has public access
- May or may not have arguments
Lesson 8.3
15Constructor Function Form
class Name private
type data_member public
Name ( ) type
function_member ( )
Lesson 8.3
16Construction Function Definition
Name Name ( ) data_member
value
Lesson 8.3
17Constructor 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
18Initializing 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
19Explicitly 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
20Overloading 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
21Overload Declarations
- Same name with different argument lists
- Example
Microwave_instruction ( ) Microwave_instruction
(int) Microwave_instruction (int, int)
Lesson 8.5
22Summary
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