CSci 152: Programming II Fall 2004 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CSci 152: Programming II Fall 2004

Description:

The object name is initialized first, then bDay, and finally student. ... name(first,last), bDay(month,day,year) personID = ID; ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 13
Provided by: DerekH71
Category:
Tags: bday | csci | fall | programming

less

Transcript and Presenter's Notes

Title: CSci 152: Programming II Fall 2004


1
CSci 152 Programming IIFall 2004
  • Composition

2
  • COMPOSITION
  • In composition one (or more) member(s) of a class
    is an object of another class type.
  • Composition is a has-a relation.

3
  • class dateType
  • public
  • void setDate(int month, int day, int year)
  • void getDate(int month, int day, int
    year)
  • void printDate() const
  • dateType(int month, int day, int year)
  • dateType()
  • private
  • int dMonth //variable to store the month
  • int dDay //variable to store the day
  • int dYear //variable to store the year

4
(No Transcript)
5
  • void dateTypesetDate(int month, int day, int
    year)
  • dMonth month
  • dDay day
  • dYear year
  • void dateTypegetDate(int month, int day,
  • int year)
  • month dMonth
  • day dDay
  • year dYear
  • void dateTypeprintDate() const
  • coutltltdMonthltlt"-"ltltdDayltlt"-"ltltdYear

6
  • //constructor with parameter
  • dateType dateType(int month, int day, int year)
  • dMonth month
  • dDay day
  • dYear year
  • dateType dateType() //default parameter
  • dMonth 1
  • dDay 1
  • dYear 1900

7
  • class personalInfo
  • public
  • void setpersonalInfo(string first, string
    last,
  • int month, int day, int year, int
    ID)
  • void printpersonalInfo () const
  • personalInfo(string first, string last,
  • int month, int day, int year, int ID)
  • personalInfo()
  • private
  • personType name
  • dateType bDay
  • int personID

8
(No Transcript)
9
  • personalInfo student
  • When the object student enters its scope, the
    objects bDay and name, which are members of
    student, also enter their scopes as a result,
    one of their constructors is executed.
  • The arguments to the constructor of a
    member-object (such as bDay) are specified in the
    heading part of the definition of the constructor
    of the class.
  • Member-objects of a class are constructed in the
    order they are declared (not in the order they
    are listed in the constructors member
    initialization list), and before the enclosing
    class objects are constructed.
  • The object name is initialized first, then bDay,
    and finally student.

10
  • void personalInfosetpersonalInfo(string first,
  • string last, int month, int day, int year,
    int ID)
  • name.setName(first,last)
  • bDay.setDate(month,day,year)
  • personID ID
  • void personalInfoprintpersonalInfo () const
  • name.print()
  • coutltlt"s date of birth is "
  • bDay.printDate()
  • coutltltendl
  • coutltlt"and personal ID is "ltltpersonID

11
  • personalInfopersonalInfo(string first, string
    last,
  • int month, int day, int year, int ID)
  • name(first,last), bDay(month,day,year)
  • personID ID
  • personalInfopersonalInfo() //default
    constructor
  • personID 0

12
  • In the case of inheritance we use the class name
    to invoke the base classs constructor while in
    the case of composition we use the member object
    name to invoke the constructor of the member
    object.
Write a Comment
User Comments (0)
About PowerShow.com