0% found this document useful (0 votes)
65 views71 pages

CSE 1062 Fundamentals of Programming Lecture #13: Spring 2017

The document discusses structures, arrays of structures, structures as function arguments, linked lists, and classes in C++. It provides examples of defining and populating structures, passing structures by value and reference, returning structures from functions, and using linked lists to connect structures. The key concepts covered are abstract data types, declaring and implementing classes, and how classes allow for more complex user-defined data types compared to built-in types in C++.

Uploaded by

Alem Mezgebo
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)
65 views71 pages

CSE 1062 Fundamentals of Programming Lecture #13: Spring 2017

The document discusses structures, arrays of structures, structures as function arguments, linked lists, and classes in C++. It provides examples of defining and populating structures, passing structures by value and reference, returning structures from functions, and using linked lists to connect structures. The key concepts covered are abstract data types, declaring and implementing classes, and how classes allow for more complex user-defined data types compared to built-in types in C++.

Uploaded by

Alem Mezgebo
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/ 71

ASTU

CSE 1062 Fundamentals of Programming

Lecture #13

Spring 2017

Computer Science & Engineering Program


The School of EE & Computing
Adama Science & Technology University
Outline ASTU

Structure
• Single structures
• Arrays of structures
• Structures as function arguments
• Linked lists
Object Oriented Programming I
• Classes
• Basic class functions
• Adding class functions
Case Study: Constructing Date Object
• Reading Assignments
• Chapter 11 of the text book
• Chapter 13 of the text book
• Dynamic Structure Allocation
• Complex Numbers

2
Single Structures ASTU

• Creating and using a structure involves two


steps
– Declare record structure
– Assign specific values to structure elements
• Declaring a structure requires listing data
types, data names, and arrangement of
data items
• Data items, or fields, are called members of
a structure
• Assigning data values to members is called
populating the structure

3
Example 1: Define and Populate a Structure ASTU

4
Example 2: Global Declaration ASTU

5
Arrays of Structures ASTU

• The real power of structures is realized


when the same structure is used for lists
of data ID Name Groups

101 Anteneh Alemu [G1-G2]

201 Desalegn Abebaw [G3-G6]

303 Tinsae Gizachew [G9-G10]

405 Kifle Derese [G13-G18]

590 Desta Zerihun [G19-G24]

699 Nemera Jira [G25-G30]

715 Natnael Alemayehu [G31-G36]

888 Getinet Yilma [G39-G42]

987 Fanos Jemal [G43-G46]

1001 Endris Mohammed [G7-G8], G49-G50]

1002 Prof. Yun Koo Chung [G51-G52]


6
1055 Bushra Ali [11-12],[37-38],[G47-G48]
Arrays of Structures ASTU

ID Name Groups

1st Structure 101 Anteneh Alemu [G1-G2]

2nd Structure 201 Desalegn Abebaw [G3-G6]

3rd Structure 303 Tinsae Gizachew [G9-G10]

4th Structure 405 Kifle Derese [G13-G18]

5th Structure 590 Desta Zerihun [G19-G24]

6th Structure 699 Nemera Jira [G25-G30]

7th Structure 715 Natnael Alemayehu [G31-G36]

8th Structure 888 Getinet Yilma [G39-G42]

987 Fanos Jemal [G43-G46]


9th Structure

1001 Endris Mohammed [G7-G8], [G49-G50]


10th Structure
1002 Prof. Yun Koo Chung [G51-G52]
11th Structure

12th Structure 1055 Bushra Ali [11-12],[37-38],[G47-G48]

7
Example 3: Array of Structures ASTU

8
Structures as Function Arguments ASTU

• Structure members can be passed to a


function just like any scalar variable
• Given the structure emp definition:

• Pass a copy of emp.idNum to


display() function:
display(emp.idNum);

9
Structures as Function Arguments ASTU

• Copies of all structure members can be


passed to a function by including the
name of the structure as an argument to
a called function
– Example: calcNet(emp)

10
Example 4: Passing by Value ASTU

11
Example 4: Passing by Value ASTU

12
Example 5: Passing by Reference ASTU

• Change line 10

• Change line 30 (header of fun. definition)

13
Passing a Pointer ASTU

• Instead of passing references, pointers can


be used
• Function call must take the address of the
structure
– Example: calcNet(&emp);
• Function declaration must indicate a pointer
argument
– Example: calcNet(Employee *pt)
• Inside function, pt argument is used to
reference members directly
– Example: (*pt).idNum

14
Example 6: Passing Pointer ASTU

• Change line 10

• Change line 15

• Change Function Definition

15
Passing a Pointer ASTU

16
Passing a Pointer ASTU

• Using pointers to functions is very


common
• Special notation exists for locating a
member of a structure from a structure
pointer
– Notation is pointer->member
• Equivalent to (*pointer).member
– Examples:

17
Changing Pointer Address ASTU

18
Returning Structures ASTU

• In practice, most structure-handling


functions get direct access to a structure
by receiving a structure reference or
address
– Changes to a structure can be made directly
from a function
• To have a function return a separate
structure, follow same procedure as for
returning scalar values

19
Example 7: Returning Structures ASTU

20
Linked Lists ASTU

• A classic data handling problem is


making additions or deletions to existing
structures that are maintained in a
specific order
• Linked lists provide method for
maintaining a constantly changing list
• Linked list: Set of structures in which
each structure contains at least one
member whose value is the address of
next logically ordered structure in list
21
Linked Lists ASTU

• Instead of requiring each record to be


physically stored in correct order, each
new structure is physically added
wherever computer has free storage
space
• Records are “linked” by including address
of next record in the record immediately
preceding it
• Current record contains address of next
record no matter where next record is
stored
22
Linked Lists ASTU

23
Linked Lists ASTU

• Adjusting addresses to point to the


correct structures

24
Linked Lists ASTU

• Using initial & final pointer variables

25
Linked Lists ASTU

• Storing an address in a structure


member

26
Example 8: Linked List ASTU

27
The Relationship between Structures ASTU

28
Example 10: Cycling through the List ASTU

29
Example 10: Cycling through the List ASTU

30
Classes ASTU

• A procedural program consists of one or


more algorithms that have been written
in computer-readable language
– Input and display of program output take a
back seat to processing
• Clear emphasis on formulas and calculations
• An object-oriented approach fits
graphically windowed environments
• Abstract data types: Central to creation
of objects; a user defined rather than
built-in data type
31
Abstract Data Types ASTU

• Data type: Combination of data and


associated operations
• A data type defines both the types of
data and the types of operations that
can be performed on the data
– Data type = Allowable Data Values + Operational Capabilities

• Operations in C++ are an inherent part


of each data type

32
Abstract Data Types ASTU

• C++ Built-in Data Type Capabilities

33
Abstract Data Types ASTU

• Abstract data type (ADT): User defined


type that specifies both a type of data
and the operations that can be
performed on it
– User defined types are required when you
want to create objects that are more
complex than simple integers and characters
• Data structure: How data is stored
• Class: C++ name for an abstract data
type
34
Class Construction ASTU

• A class is usually constructed in two


parts:
– Declaration section
• Declares both the data types and functions for
the class
– Implementation section
• Defines the functions whose prototypes have
been declared in the declaration section

35
Class Construction ASTU

• Class members: Both the variables and


the functions listed in the declaration
section
• Data members or instance variables:
Variables listed in the declaration section
• Member functions: Functions listed in
the declaration section
• When a function is part of a class it is
referred to as a method to denote class
membership

36
Example 1: Class Construction ASTU

37
Class Construction ASTU

• Initial uppercase letter in class name Date


not required but followed by convention
• Keywords public and private are
access specifiers that define access rights
– private: Indicates that class member can only
be accessed by class functions
• Restricting user from access to data storage
implementation details is called data hiding
• After a class category like private is
designated, it remains in force until a new
category is specified

38
Class Construction ASTU

• public functions can be called from


outside the class
• In general, all class functions should be
public so that they provide capabilities
to manipulate class variables from
outside the class
• The function with same name as class is
the class’s constructor function
– Used to initialize class data members with
values
39
Class Construction ASTU

• Implementation section: member


functions declared in the declaration
section are written
• General form for functions written in the
implementation section is the same as all
C++ functions with the addition of the
class name and the scope resolution
operator ::

40
Example 1: Complete Program ASTU

• Modify Example 1
– Insert the following

– Add the main function

41
Class Construction ASTU

• Variables of a user-declared class must:


– Be defined before use in a program
– Are referred to as objects
• An object name’s attribute is referenced
with the dot operator
– objectName.attributeName
objectName is the name of a specific object
attributeName is the name of a data member
defined for the object’s class

42
Class Construction ASTU

• The syntax for referring to an object’s


method is:
– objectName.methodName(parameters)
• objectName is the name of the specific object
• methodName is name of a function defined for
the object’s class

43
Terminology ASTU

• Class: Programmer defined data type from


which objects can be created
• Objects: Created from classes
– Referred to as instances of a class
• Process of creating a new object is called
instantiation of the object
• Each time a new object is instantiated a
new set of data members belonging to the
object is created
– Values contained in these data members
determine the object’s state

44
Basic Class Functions ASTU

• Constructor: A function used to initialize


an object’s data members when the
object is created
• Accessor: A function that reports
information about an object’s state
• Mutator: A function that modifies the
values stored in an object’s data
members

45
Constructor Functions ASTU

• A constructor function is any function with


the same name as its class
• Multiple constructors can be defined for
each class as long as they can be
distinguished by number and types of their
parameters
• A constructor’s intended purpose is to
initialize a new object’s data members
• If no constructor function is written, the
compiler supplies a default constructor
• In addition to initialization, a constructor
can perform other tasks when it is called

46
Constructor Functions ASTU

• General format of a constructor includes:


– The same name as the class to which it
belongs
– No return type (not even void)
– A constructor that does not require
arguments is called the default constructor

47
Calling Constructors ASTU

• Constructors are called when an object is created


• Declaration can be made in a variety of ways
Date c(4,1,2013);
Date c = Date(4,1,2013);
Date c = 8; // similar to above with
// defaults for day and
// year
• An object should never be declared with empty
parentheses
Date a();
• Not the same as the declaration Date a;
• Does not result in an object being created

48
Example 2: Constructors ASTU

49
Accessor Functions ASTU

• An accessor function provides a means


for reporting on an object’s state
• Conventionally called get() functions
• Each class should provide a complete set
of accessor functions
• Accessor functions are extremely
important because they provide a means
of retrieving and displaying an object’s
private data values

50
Mutator Function ASTU

• A mutator function provides a means


for changing an object’s data member
• Conventionally called set() functions
• A class can contain multiple mutators, as
long as each one has a unique name or
parameter list

51
Example 3: Accessors and Mutators ASTU

52
Example 3: Accessors and Mutators ASTU

53
Adding Class Functions ASTU

• Most classes typically require additional


functions
• In C++, there are two basic means of
supplying these additional capabilities:
– Construct class functions in a similar manner as
mutator and accessor functions
– Construct class functions that use conventional
operator symbols, such as =, *, ==, >=, which
are known as operator functions (not discussed here)
• Both approaches can be implemented as
member or friend functions
54
Member Functions ASTU

• Member functions can be added to a


class by including their prototypes in the
declaration section and providing code
for the function in the implementation
section or as an inline function
• The general syntax for each function
header is:

55
Example 4: Member Functions ASTU

56
Example 4: Member Functions ASTU

57
Friend Functions ASTU

• Private variables can be accessed and


manipulated through a class’s member
functions

58
Friend Functions ASTU

• External access to private functions can


be granted through the friends list
mechanism
• The friends list members are granted the
same privileges as a class’s member
functions
• Nonmember functions in the list are
called friend functions
• Friends list: Series of function prototype
declarations preceded with the friend
keyword
59
Friend Functions ASTU

60
Example 5: Friend Functions ASTU

61
Example 5: Friend Functions ASTU

62
Case Study: Constructing Date Object ASTU

• Step 1: Analyze the problem: define


operations
• Step 2: Develop a solution: define classes
and data members
• Step 3: Code the solution
• Step 4: Test and correct the program:
testing the Date class entails testing and
verifying each class function and
operator function

63
Case Study: Constructing Date Object ASTU

• Data Structure
– In this application, you have one type of
object, a date, which is specified by a
month, day, and year.

64
Case Study: Constructing Date Object ASTU

• Required Operations
– A complete description of a Date class must
include how a date is to be represented and
the operations the class must provide.

65
Case Study: Constructing Date Object ASTU

• Data Members

• Algorithm to Determine Leap Year

66
Code the Solution ASTU

67
Code the Solution ASTU

• Testing is left as an exercise


68
Structures Versus Classes ASTU

• Structures are normally used with all


member variables public and with no
member functions.
• However, in C++ a structure can have private
member variables and both public and private
member functions
• Aside from some notational differences, a C++
structure can do anything a class can do.
• Having said this and satisfied the “truth
in advertising” requirement, we advocate
that you forget this technical detail
about structures.
69
Structures Versus Classes ASTU

• If you take this technical detail seriously


and use structures in the same way that
you use classes,
– then you will have two names (with different
syntax rules) for the same concept.
• On the other hand, if you use structures
as we described them, then you will have
a meaningful difference between
structures (as you use them) and classes;
and your usage will be the same as that
of most other programmers.

70
Structures Versus Classes ASTU

• One difference between a structure and


a class is that
– they differ in how they treat an initial group
of members that has neither a public nor a
private access specifier.
– If the first group of members in a definition
is not labeled with either public :or private:
,then a structure assumes the group is
public, whereas a class would assume the
group is private.

71

You might also like