ECE 538 Object Oriented Concepts Session 4 - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

ECE 538 Object Oriented Concepts Session 4

Description:

Class definition contains declarations or definitions of all members. Members must be declared in ... Can't define same class more than once in a .cpp file ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 34
Provided by: johngwe
Category:

less

Transcript and Presenter's Notes

Title: ECE 538 Object Oriented Concepts Session 4


1
ECE 538 Object Oriented ConceptsSession 4
  • Dr. John G. Weber
  • John.Weber_at_notes.udayton.edu
  • http//academic.udayton.edu/JohnWeber

2
Multifile programs
  • C allows separate compilation of program
    modules
  • Class Libraries
  • C organized around use of class libraries
  • Two components
  • Interface
  • Implementation
  • Organization and Conceptualization
  • Programming ease
  • Separate programmers
  • Functionality

3
Creating Multifile Programs
  • Consider accessing a library (not a system lib)
  • Library is compiled separately and you have
    access to object code
  • Interface is specified in libname.h file
  • libname.h contains public data and function
    definitions
  • Mechanics
  • To use library, reference in your source code
    file
  • include library.h
  • Put header file and obj file in same directory
    with your source
  • Alternately, tell compiler where they are located
  • Visual C select toolsoptionsdirectories and
    browse for directory

4
Inter-file Communication
  • Variables
  • Global variable may only be defined once in
    program
  • To access in different file, first declare with
    extern key word
  • e.g. //File A
  • int globalvar1 //definition in file
    A
  • //File B
  • extern int globalvar1 //declare in File B
  • globalvar1 5 //use in File B
  • Note Cannot initialize a variable in an extern
    declaration
  • Can define global variables of the same name in
    different files by using static keyword
  • Makes definition visible within file only

5
Inter-file Communication (cont)
  • Inter-file Functions
  • Function declaration specifies name of function,
    return type, and type of arguments
  • Function body defines function
  • To use a function with its definition in another
    filedeclare where you wish to use it
    (prototype)
  • Multiple declarations OK if consistent
  • Function can be made invisible to other files
    with static declaration
  • e.g. static int somefunction (int a, int b)
  • Above function only visible in file declared

6
Inter-file Communication (cont)
  • Inter-file Classes
  • Recall
  • class definition does not reserve memory
  • Informs compiler what members constitute class
  • Class definition contains declarations or
    definitions of all members
  • Members must be declared in class definition
  • Members may be defined in class definition or in
    another file
  • Class must be defined not simply declared in
    every file where it is used
  • Cant define same class more than once in a .cpp
    file
  • Every .cpp file using the class must have class
    definition
  • Use header files to do this

7
Header Files
  • Supply two or more source files with same info
  • e.g. //file.h
  • extern int globalvar //var declaration
  • int globalfunction(int) //function declaration
  • //fileA.cpp
  • include file.h
  • int globalvar //define global variable
  • int globalfunction(int n) //function definition
  • return n
  • //fileB.cpp
  • include file.h
  • .
  • .
  • globalvar 5 //work with global variable
  • int globalvarBglobalfunction(globalvar)
    //work with global function

8
Typical Structure
  • Header File
  • Class definition
  • Member function prototypes
  • Class.cpp file
  • Function definitions
  • Application.cpp file
  • include Class.h file

9
ExampleComplex Class Header File
//complex.h //header file for complex class
definition include ltiostreamgt using namespace
std class Complex private double real,
imaj public Complex() //no argument
constructor Complex(double re) //one arg
constructor Complex(double re, double im)
void showcomplex() //complex
addition Complex Complexoperator (Complex
b) //scaler complex Complex
Complexoperator (double a)
10
ExampleComplex Class Function Definition File
//complex.cpp //function definitions for complex
class include ltiostreamgt include
"complex.h" using namespace std ComplexComple
x() real(0), imaj(0) //no
argument constructor ComplexComplex(double
re) real(re), imaj(0) //one arg
constructor ComplexComplex(double re, double
im) real(re), imaj(im) //
2-arg constructor void Complexshowcomplex()
//display complex in (re,im)
form cout ltlt "( " ltlt real ltlt "," ltlt imaj ltlt "
)" ltltendl //complex addition Complex
Complexoperator (Complex b) double re,
im re real b.real im imaj
b.imaj return Complex(re,im) //scaler
complex Complex Complexoperator (double
a) Complex r a, b (real,imaj) return r
b
11
ExampleComplex Class Test File
//main.cpp //file to test complex class include
"complex.h" int main() int testint
5 Complex testcomplex (2,3), test2 (1,1),
test3,result cout ltlt"testint "ltlttestintltlt
endl testcomplex.showcomplex() result test2
testcomplex cout ltlt" complex complex result
" result.showcomplex() test3
testint cout ltlt"convert from real to complex
" test3.showcomplex() cout ltlt"add int to
complex " result testcomplex
testint result.showcomplex() return 0
12
MultipleIncludes Hazard
  • Cant define function or variable in header
    shared by multiple source files
  • Causes multiple definition errors
  • Including same header multiple times creates same
    problem
  • e.g. Suppose head1.h and head2.h are header
    files and head2.h includes head1.h
  • Suppose now that app.cpp includes head1.h and
    head2.h
  • This can generate multiple definition error
    problem
  • Use the following construct to prevent
  • if !defined (HEAD1)
  • define HEAD1
  • include head1.h //put potential multiple
    definition stuff here
  • endif

13
Namespaces
  • Defining a Namespace
  • Namespace is section of code that is given a name
  • e.g. typical namespace definition
  • namespace my_namespace
  • const double PI 3.1415927
  • double circumf (double radius)
  • return 2 PI radius
  • //end my_namespace
  • To access circumf outside of my_namespace, use
  • My_namespacecircumf (5.13)
  • May have multiple instances of same namespace
    definition
  • May place declarations outside namespace that
    behave as if they were inside
  • e.g.
  • double my_namespacearea (double radius)
  • return PIradiusradius

14
typedef
  • Create new name for data types
  • e.g. typedef unsigned long unlong
  • Makes unlong a synonym for unsigned long
  • typedef int ptr_to_int
  • Makes ptr_to_int synonym for int
  • ptr_to_int p1, p2, p3 declares p1, p2, and p3 as
    pointers to integers
  • Can use to rename classes
  • e.g. class My_Class_With_A_Long_Name member
  • Can become
  • typedef My_Class_With_A_Long_Name mcwaln
  • mcwaln some_object will instantiate an object of
    the above type

15
Class Diagrams
  • Full and Abbreviated Forms
  • Full form contains three divisions
  • Class Name
  • Attributes
  • Operations
  • Abbreviated Form has only the class name

16
Diagram Discussion
class employee //employee
class private char nameLEN
//employee name unsigned long
number //employee number public
void getdata() cout ltlt "\n Enter last
name " cin gtgt name cout ltlt "
Enter number " cin gtgt number void
putdata() const cout ltlt "\n Name "
ltlt name cout ltlt "\n Number " ltlt
number
  • - before attribute or operation indicates
    private
  • indicates public (sometimes omitted)
  • indicates protected
  • Attribute_Nametype
  • Operation_Name(arguments)return_type

17
Inheritance
  • Open arrow head points toward the base class
  • Operations shown in each diagram have
    implementations in each class
  • Inherited operations not enumerated
  • e.g. Each of these classes has two native
    operations (of the same name)
  • Class Manager has four operations
  • employeegetdata()
  • employeeputdata()
  • getdata()
  • getdata()

18
Class Relationships
  • Some Important Class Relationships
  • Inheritance
  • Association
  • Aggregation
  • Composition

19
Inheritance
  • Discusses previously

20
Association
  • Concept
  • Real-World entities represented by classes in
    program have some kind of obvious relationship
  • For Example, drivers are related to cars, books
    are related to libraries, race horses are related
    to race tracks
  • If such entities were classes in a program, the
    relationship would be called association
  • Class association actually implies that the
    objects of the classes have some kind of
    relationship rather than the classes themselves
  • Two classes are associated if an object of one
    class calls a member function of an object of
    another class.
  • Two classes are associated if an attribute of one
    class is an object of the other class

21
Types of Association
  • Unary (Unidirectional)
  • Object of one class calls function from object of
    another class
  • Binary (bidirectional)
  • Objects of each class call functions from objects
    of the other class

22
Example
/times2.cpp //converts from time24 to time12
using constructor in time12 include
ltiostreamgt include ltstringgt using namespace
std class time24 private int
hours //0 to 23 int
minutes //0 to 59 int
seconds //0 to 59 public
//no-arg constructor
time24() hours(0), minutes(0), seconds(0)
time24(int h, int m, int s) hours(h),
minutes(m), seconds(s) //3-arg constructor
void display() const //format
231501 if(hours lt 10)
cout ltlt '0' cout ltlt hours ltlt ''
if(minutes lt 10) cout ltlt '0' cout
ltlt minutes ltlt '' if(seconds lt 10)
cout ltlt '0' cout ltlt seconds
int getHrs() const return hours
int getMins() const return minutes
int getSecs() const return seconds
23
Example (Cont)
class time12 private bool pm
//true pm, false am
int hrs //1 to 12
int mins //0 to 59
public //no-arg
constructor time12() pm(true), hrs(0),
mins(0) time12(time24)
//1-arg constructor
//3-arg constructor
time12(bool ap, int h, int m) pm(ap), hrs(h),
mins(m) void display() const
cout ltlt hrs ltlt ''
if(mins lt 10) cout ltlt '0' //extra zero for
"01" cout ltlt mins ltlt ' '
string am_pm pm ? "p.m." "a.m."
cout ltlt am_pm
24
Example (Cont)
time12time12( time24 t24 ) //1-arg
constructor
//converts time24 to time12 int hrs24
t24.getHrs() //get hours
//find am/pm pm
t24.getHrs() lt 12 ? false true
mins
(t24.getSecs() lt 30) ? //round secs
t24.getMins() t24.getMins()1
if(mins 60) //carry mins?
mins0 hrs24
if(hrs24 12 hrs24 24) //carry hrs?
pm (pmtrue) ? false true //toggle
am/pm hrs (hrs24 lt 13) ? hrs24
hrs24-12 //convert hrs if(hrs0)
//00 is 12 a.m. hrs12
pmfalse
25
Example (Cont)
int main() int h, m, s while(true)
//get 24-hour
time from user cout ltlt "Enter 24-hour time
\n" cout ltlt " Hours (0 to 23) " cin gtgt
h if(h gt 23) //quit if
hours gt 23 return(1) cout ltlt "
Minutes " cin gtgt m cout ltlt " Seconds
" cin gtgt s time24 t24(h, m, s)
//make a time24 cout ltlt "You entered "
//display the time24 t24.display()
time12 t12 t24 //convert
time24 to time12 cout ltlt "\n12-hour time
" //display equivalent time12
t12.display() cout ltlt
"\n\n" return 0
26
Navigability
  • Direction of association
  • e.g. if you know object of class one, you can
    quickly find obj of class 2
  • time12 class calls time24 functions to support
    time conversion
  • i.e. since we know time24 obj, we can easily
    convert to time12 obj with the classes as defined

27
Aggregation Classes within Classes
  • If class B is derived from Class A, then B is a
    kind of A
  • B has all characteristics of A plus some of its
    own
  • Aggregation is called a has a relationship
  • A library has a book
  • A car has a door
  • Aggregation may occur when one object is an
    attribute of another

Class A Class B A objA // define obj
A as an object of class A
28
UML for Aggregation
29
Aggregation Example
  • Recall the Multiple Inheritance Example
  • Restructure to use aggregation instead

30
Mini-program Example
class student class employee class
manager student stu //stu is an object of
class student employee emp //emp is an object
of class employee class scientist student
stu //stu is an object of class
student employee emp //emp is an object of
class employee class laborer employee
emp //emp is an object of class employee
31
Composition
  • Stronger form of aggregation
  • All the characteristics of aggregation plus two
    more
  • Part may belong to only one whole
  • Lifetime of part is same as lifetime of whole
  • Composition is a consists of relationship

32
Multiplicity Symbols
33
Assignment
  • Due June 17
  • Rework the multiple inheritance example to use
    aggregation
Write a Comment
User Comments (0)
About PowerShow.com