0% found this document useful (0 votes)
9 views29 pages

CSCE 1001 Chapter 10

CS

Uploaded by

abdelrahmansendo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views29 pages

CSCE 1001 Chapter 10

CS

Uploaded by

abdelrahmansendo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 10

Defining Classes

Copyright © 2014 Pearson Addison-Wesley. All rights reserved.


Overview

10.1 Structures

10.2 Classes

10.3 Abstract Data Types

10.4 Introduction to Inheritance

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 2


10.1
Structures

Copyright © 2014 Pearson Addison-Wesley. All rights reserved.


Structures
 A structure can be viewed as an object
 Contains multiple values of possibly different types

 The multiple values are logically related as a single item


 Example: A bank Certificate of Deposit (CD)
has the following values:
a balance
an interest rate
a term (months to maturity)

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 4


The CD Definition
 The Certificate of Deposit structure can be
defined as
struct CDAccount
{
double balance;
double interest_rate;
int term; //months to maturity
}; Remember this semicolon!
 Keyword struct begins a structure definition
 CDAccount is the structure tag or the structure’s type name
 Member names are identifiers declared in the braces

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 5


Using the Structure
 Structure definition is generally placed outside
any function definition
 This makes the structure type available to all code

that follows the structure definition

 To declare two variables of type CDAccount:

CDAccount my_account, your_account;

 My_account and your_account contain distinct


member variables balance, interest_rate, and term

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 6


The Structure Value

 The Structure Value


 Consists of the values of the member variables

 The value of an object of type CDAccount


 Consists of the values of the member variables

balance
interest_rate
term

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 7


Specifying Member Variables
 Member variables are specific to the
structure variable in which they are declared

 Syntax to specify a member variable:


Structure_Variable_Name . Member_Variable_Name

 Given the declaration:


CDAccount my_account, your_account;

 Use the dot operator to specify a member variable


my_account.balance
my_account.interest_rate
my_account.term

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 8


Using Member Variables
 Member variables can be used just as any other
variable of the same type
 my_account.balance = 1000;

your_account.balance = 2500;
 Notice that my_account.balance and your_account.balance
are different variables!
 my_account.balance = my_account.balance + interest;

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 9


Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 1- 10
Duplicate Names
 Member variable names duplicated between
structure types are not a problem.
struct FertilizerStock struct CropYield
{ {
double quantity; int quantity;
double nitrogen_content; double size;
}; };

FertilizerStock super_grow; CropYield apples;


 super_grow.quantity and apples.quantity are
different variables stored in different locations

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 11


Structures as Arguments

 Structures can be arguments in function calls


 The formal parameter can be call-by-value

 The formal parameter can be call-by-reference

 Example:
void get_data(CDAccount& the_account);
 Uses the structure type CDAccount we saw

earlier as the type for a call-by-reference


parameter

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 12


Structures as Return Types
 Structures can be the type of a value returned by
a function
 Example:
CDAccount shrink_wrap(double the_balance,
double the_rate,
int the_term)
{
CDAccount temp;
temp.balance = the_balance;
temp.interest_rate = the_rate;
temp.term = the_term;
return temp;
}

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 13


Hierarchical Structures
 Structures can contain member variables that are
also structures
struct PersonInfo
struct Date {
{ double height;
int month; int weight;
int day; Date birthday;
int year; };
};
 struct PersonInfo contains a Date structure

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 14


Using PersonInfo
 A variable of type PersonInfo is declared by

PersonInfo person1;

 To display the birth year of person1, first access the


birthday member of person1

cout << person1.birthday…

 But we want the year, so we now specify the


year member of the birthday member

cout << person1.birthday.year;


Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 15
Initializing Classes
 A structure can be initialized when declared
 Example:
struct Date
{
int month;
int day;
int year;
};
 Can be initialized in this way
Date due_date = {12, 31, 2004};

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 16


Section 10.1 Conclusion

 Can you

 Write a definition for a structure type for


records consisting of a person’s wage rate,
accrued vacation (in whole days), and status
(hourly or salaried). Represent the status as
one of the two character values ‘H’ and ‘S’.
Call the type EmployeeRecord.

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 17


10.2
Classes

Copyright © 2014 Pearson Addison-Wesley. All rights reserved.


What Is a Class?

 A class is a data type whose variables are objects


 Some pre-defined data types you have used are
 int

 char

 You can define your own classes as well

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 19


Class Definitions

 A class definition includes


 A description of the kinds of values the variable

can hold
 A description of the member functions

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 20


A Class Example
 To create a new type named DayOfYear as
a class definition
 Decide on the values to represent

 This example’s values are dates such as July 4

using an integer for the number of the month


 Member variable month is an int (Jan = 1, Feb = 2, etc.)
 Member variable day is an int
 Decide on the member functions needed
 We use just one member function named output

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 21


Class DayOfYear Definition

 class DayOfYear
{
public:
void output( );
int month;
int day;
};
Member Function Declaration

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 22


Defining a Member Function
 Member functions are declared in the class
declaration
 Member function definitions identify the class
in which the function is a member
 void DayOfYear::output()
{
cout << “month = “ << month
<< “, day = “ << day
<< endl;
}

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 23


Member Function Definition
 Member function definition syntax:
Returned_Type
Class_Name::Function_Name(Parameter_List)
{
Function Body Statements
}
 Example: void DayOfYear::output( )

{
cout << “month = “ << month
<< “, day = “ << day << endl;
}

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 24


The ‘::’ Operator
 ‘::’ is the scope resolution operator
 Tells the class a member function is a member
of

 void DayOfYear::output( ) indicates that


function output is a member of the
DayOfYear class

 The class name that precedes ‘::’ is a type


qualifier

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 25


‘::’ and ‘.’
 ‘::’ used with classes to identify a member
void DayOfYear::output( )
{
// function body
}

 ‘.’used with variables to identify a member


DayOfYear birthday;
birthday.output( );

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 26


Calling Member Functions
 Calling the DayOfYear member function output
is done in this way:
DayOfYear today, birthday;
today.output( );
birthday.output( );

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 27


Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 28
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 10- 29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy