COM262 Object Development - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

COM262 Object Development

Description:

int bmonth, int bday, int byear, int hmonth, int hday, int hyear ) : birthDate( bmonth, bday, byear ), hireDate( hmonth, hday, hyear ) firstName = fname; ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 30
Provided by: osirisSun
Category:

less

Transcript and Presenter's Notes

Title: COM262 Object Development


1
COM262 Object Development
Driving (arrays of) classes with main()and menus
Additional O-O features const initialisation
list for const friend
2
Milestone testPart 1 Problem specification ?
code
Class declaration
Class definition function bodies
Main() program
Part 2 Chop-up code ? class.h, class.cpp,
main.cpp
3
  • FileNew 6other ConsoleWizard
  • VCL, Multithreaded
  • Get rid of pragma etc.
  • Get rid of main( arguments )
  • Save as class.cpp
  • Save Project as classProject.bpr
  • Write a class
  • Add-in your string and IO header files
    (cutpaste)
  • FileNew other hpp file
  • FileNew other cpp file
  • (add to Project)

4
HEADER FILEStudent.hifndefdefineendif
  • include ltiostream.hgt
  • include ltstringgt // included for string classes
  • using stdstring
  • class Student
  • private
  • string name
  • int mark
  • public
  • // default constructor function
  • Student( void )
  • Student( string hs, int m )
  • void AddMark( int extra )
  • int GetMark( void )
  • string GetName( void )
  • void PrintDetails( int index )

PRIVATE DATA
PUBLIC FUNCTIONS
5
CONSTRUCTORS FUNCTIONSStudent.cpp
  • include Student.h
  • include ltiostream.hgt
  • include ltstringgt // included for string classes
  • using stdstring
  • StudentStudent( void )
  • cout ltlt "Enter new student's name "
  • cin gtgt name
  • mark 0
  • StudentStudent( string hs, int m )
  • name hs
  • mark m

void StudentAddMark(int extra) mark
extra int StudentGetMark(void) return
mark
6
  • include Student.h
  • include ltconio.hgt
  • include ltiostream.hgt
  • include ltstringgt
  • using stdstring
  • void main()
  • int av_mark
  • Student a_Student(Hector", 28)
  • Student b_Student( Morag", 45)
  • a_Student.AddMark( 33 )
  • b_Student.AddMark( 45 )
  • // calculate average mark
  • av_mark ( a_Student.GetMark()
    b_Student.GetMark() ) / 2
  • // display the information
  • cout ltlt "The average mark was " ltlt av_mark ltlt
    endl
  • getch()

MAIN() PROGRAM StudentApp.cpp
7
main() function and menu
  • switch ( choice )
  • case 1 // get extra and add marks
  • cin gtgt extra_marks
  • a_student.AddMark(extra_marks)
  • break
  • case 2
  • a_student.PrintDetails()
  • break
  • case 3
  • mark a_student.GetMark()
  • cout ltlt "Current mark is " ltlt mark
  • break
  • case 4 // exit
  • return 0
  • int main()
  • int mark, extra_marks, choice
  • Student a_student(Anakin", 12)
  • while ( 1 ) // forever loop until exit option
  • cout ltlt "Select your option"
  • cout ltlt "1. Add some marks"
  • cout ltlt "2. Display student details"
  • cout ltlt "3. Display just the mark"
  • cout ltlt "4. Exit program."
  • // get user's menu selection into a variable
  • cin gtgt choice

8
  • const int NO_STUDENTS3
  • displayMenu()
  • main()
  • int choice, i, extra_marks
  • String name
  • Student studentsNO_STUDENTS
  • while ( 1 ) // forever loop until exit
  • displayMenu()
  • // get user's menu selection
  • cin gtgt choice

switch ( choice ) case 1 break case 2
break case 9 break default //
end switch // end while // end main
main() function, menu and array of students
9
case 2 // get student name print mark
cin gtgt name for ( i0 i lt NO_STUDENTS i )
if ( name
studentsi.GetName() )
break // exit for loop
if ( i NO_STUDENTS ) // exit
switch cout ltlt "No student
found" break // found student
details ! studentsi.PrintDetails( )
break // exit switch
case 1 // get student index, add marks cin gtgt
i gtgt extra_marks studentsi.AddMark(
extra_marks ) break
10
Additional O-O features
const initialisation list for const friend

11
const (Constant) Objects and const Member
Functions
  • Principle of least privilege
  • Only give objects permissions they need, no more
  • Keyword const
  • Specify that an object is not modifiable
  • Any attempt to modify the object is a syntax
    error
  • Example
  • const Time noon( 12, 0, 0 )
  • Declares a const object noon of class Time and
    initializes it to 12

12
  • const objects require const functions
  • Member functions declared const cannot modify
    their object
  • const must be specified in function prototype and
    definition
  • Prototype
  • ReturnType FunctionName(param1,param2) const
  • Definition
  • ReturnType FunctionName(param1,) const
  • Example
  • int AgetValue() const return
    privateDataMember
  • Returns the value of a data member but doesnt
    modify anything so is declared const
  • Constructors / Destructors cannot be const
  • They need to initialize variables, therefore
    modifying them

13
class Time public // default
constructor Time( int 0, int 0, int 0 )
void setTime( int, int, int ) // set
time // get functions (normally declared
const) int getHour() const // return
hour int getMinute() const // return
minute int getSecond() const // return
second // print functions (normally declared
const) void printConst() const // print
time void print() // print
time private int hour // 0
23 int minute // 0 59 int second
// 0 59
14
int main() Time wakeUp( 6, 45, 0 ) //
non-constant object const Time noon( 12, 0, 0 )
// constant object //
COMPILER ERRORS MEMBER FUNCTION
OBJECT wakeUp.setHour( 18 ) // non-const
non-const noon.setHour( 12 ) // non-const
const wakeUp.getHour() // const
non-const noon.getMinute() // const
const noon.printStandard() // non-const
const
15
const (Constant) Objects and const Member
Functions
  • Member initializer syntax
  • Data member age_EFFECT in class Person
  • constructor for Increment is modified as follows
  • Person Person ( int c, int i ) age_EFFECT
    ( i ) count c
  • age_EFFECT ( i ) initializes age_EFFECT to i
  • All data members can be initialized using member
    initializer syntax
  • consts and references must be initialized using
    member initializer syntax
  • Multiple member initializers
  • Use comma-separated list after the colon

16
class Person public Person( int c 0, int
i 1 ) void getting_older() wisdom
age_effect void print() const private i
nt wisdom const int age_effect // const data
member
17
// Constructor for class Increment Person
Person ( int c, int i ) // initializer for const
member age_effect( i ) wisdom c
// Print the data void Person print()
const cout ltlt jedi mastery " ltlt wisdom
18
Composition Objects as Members of Classes
  • Composition, AGGREGATION
  • Class has objects of other classes as members
  • Construction of objects
  • Member objects constructed in order declared
  • Not in order of constructors member initializer
    list
  • Constructed before their enclosing class objects
    (host objects)

19
class Date public // default
constructor Date( int 1, int 1, int 1900
) // print date in month/day/year format void
print() const // provided to confirm
destruction order Date() private int
month // 1-12 int day // 1-31 based on
month int year // any year // utility
function to test proper day // for month and
year int checkDay( int )
20
// Constructor Confirm proper value for
month // call utility function checkDay // to
confirm proper value for day. DateDate( int
mn, int dy, int yr ) // validate the month if
( mn gt 0 mn lt 12 ) month mn else
month 1 cout ltlt "Month " ltlt mn ltlt "
invalid. \n" year yr // should
validate yr day checkDay( dy )// validate the
day
21
class Employee public Employee( string,
string, int, int, int, int, int, int
) void print() const Employee()
private string firstName string
lastName const Date birthDate const Date
hireDate
22
EmployeeEmployee( string fname, string
lname, int bmonth, int bday, int byear, int
hmonth, int hday, int hyear ) birthDate(
bmonth, bday, byear ), hireDate( hmonth, hday,
hyear ) firstName fname lastName
lname main() Employee e( "Bob", "Jones",
7, 24, 1949, 3, 12, 1988 )
23
friend Functions and friend Classes
  • friend function and friend classes
  • Can access private and protected members of
    another class
  • friend functions are not member functions of
    class
  • Defined outside of class scope
  • Properties of friendship
  • Friendship is granted, not taken
  • Not symmetric (if B a friend of A, A not
    necessarily a friend of B)
  • Not transitive (if A a friend of B, B a friend of
    C, A not necessarily a friend of C)

24
friend Functions and friend Classes
  • friend declarations
  • To declare a friend function
  • Type friend before the function prototype in the
    class that is giving friendship
  • friend int myFunction( int x )
  • should appear in the class giving friendship
  • To declare a friend class
  • Type friend class Classname in the class that is
    giving friendship
  • if ClassOne is granting friendship to ClassTwo,
  • friend class ClassTwo
  • should appear in ClassOne's definition

25
  • 1. Class definition
  • 1.1 Declare function a friend
  • 1.2 Function definition
  • 1.3 Initialize Count object

26
counter.x after instantiation 0 counter.x after
call to setX friend function 8
27
  • (Previous program without friend declared)

28
  Compiling... Fig07_06.cpp D\books\2000\cpphtp3\
examples\Ch07\Fig07_06\Fig07_06.cpp(22)
error C2248 'x' cannot access private member
declared in class 'Count'
D\books\2000\cpphtp3\examples\Ch07\Fig07_06\
Fig07_06.cpp(15) see declaration of
'x' Error executing cl.exe.   test.exe - 1
error(s), 0 warning(s)
29
TUTORIAL
  • Milestone Test 2
Write a Comment
User Comments (0)
About PowerShow.com