22
Spring 2008 Mark Fontenot [email protected] CSE 2341 - Honors Principles of Computer Science I Note Set 6 1

CSE 2341 - Honors Principles of Computer Science I

Embed Size (px)

DESCRIPTION

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected]. Note Set 6. Quick Look. Object Composition. Relationships. Different kinds of relationships can exist between classes “is a” (inheritance – more on this later) - PowerPoint PPT Presentation

Citation preview

Page 1: CSE 2341 - Honors Principles of Computer Science I

Spring 2008

Mark [email protected]

CSE 2341 - HonorsPrinciples of Computer Science I

Note Set 61

Page 2: CSE 2341 - Honors Principles of Computer Science I

Quick Look

2

Object Composition

Page 3: CSE 2341 - Honors Principles of Computer Science I

Relationships

3

Different kinds of relationships can exist between classes“is a” (inheritance – more on this later)“has a” (composition/aggregation)“uses a” (association)

Page 4: CSE 2341 - Honors Principles of Computer Science I

Object Composition

4

Object composition occurs when a class contains an instance of another class

Creates a “has a” relationship between classes

Page 5: CSE 2341 - Honors Principles of Computer Science I

Object Composition

5

class Date{private: //private //memberspublic: //public //members};

class Employee{private: char lName[25]; char fName[25]; Date birthDate; Date hireDate;public: //public members};

Page 6: CSE 2341 - Honors Principles of Computer Science I

Date Class

6

#ifndef DATE1_H#define DATE1_Hclass Date{public:

Date(int = 1, int = 1, int = 1900); void print ( ) const; ~Date( );

private:int month; // 1-12int day; // 1-31 based on monthint year; // any yearint checkDay(int);

};#endif

Page 7: CSE 2341 - Honors Principles of Computer Science I

Date Class

7

Date::Date(int mn, int dy, int yr) { if (mn > 0 && mn <= 12) month = mn; else { month = 1; cout << “Month 1” << mn << “ invalid.Set to month 1.”

<< endl; } year = yr; // could also check day = checkDay(dy); // validate the day cout << “Date object constructor for date ”; print( ); cout << endl;}

Page 8: CSE 2341 - Honors Principles of Computer Science I

Date Class

8

int Date::checkDay(int testDay){ static int daysPerMonth[13] = {0, 31, 28, 31, 30

31, 30, 31, 31, 30, 31, 30, 31}; if (testDay > 0 && testDay <= daysPerMonth[month]) return testDay;

if (month == 2 && testDay == 29 && (year % 400 == 0||(year % 4 == 0 && year % 100 !=0)))

return testDay;

cout << “Day” << testDay << “ invalid. Set to day 1.”<<endl; return 1; }

Page 9: CSE 2341 - Honors Principles of Computer Science I

Date Class

9

// Print Date object in form month/day/yearvoid Date::print() const{ cout << month << ‘/’ << day << ‘/’ << year;}

Date::~Date(){ cout << “Date object destructor for date ”; print(); cout << endl;}

Page 10: CSE 2341 - Honors Principles of Computer Science I

Employee Class

10

// emply1.h#ifndef EMPLY1_H#define EMPLY1_H#include "date1.h“class Employee {public: Employee(char *, char *, int, int, int, int, int, int ); void print() const; ~Employee(); private: char firstName[ 25 ]; char lastName[ 25 ]; const Date birthDate; const Date hireDate;};#endif

Page 11: CSE 2341 - Honors Principles of Computer Science I

Employee Class

11

// Member function definitions for Employee class.#include <iostream>using namespace std;#include <cstring>#include "emply1.h"#include "date1.h”Employee::Employee( char *fname, char *lname, int bmonth, int bday, int byear, int hmonth, int hday, int hyear ) : birthDate( bmonth, bday, byear ), hireDate( hmonth, hday, hyear ) // constructor of date class called twice: // once for the birthDate object // once for the hireDate object{ // body of constructor

Page 12: CSE 2341 - Honors Principles of Computer Science I

Employee Constructor

12

Employee::Employee( char *fname, char *lname, int bmonth, int bday, int byear, int hmonth, int hday, int hyear ) :birthDate( bmonth, bday, byear ), hireDate( hmonth, hday, hyear ) { int length = strlen( fname ); length = ( length < 25 ? length : 24 ); strncpy( firstName, fname, length ); length = strlen( lname ); length = ( length < 25 ? length : 24 ); strncpy( lastName, lname, length ); cout << "Employee object constructor: “ << firstName << ' ' << lastName << endl;}

Page 13: CSE 2341 - Honors Principles of Computer Science I

Employee Class

13

void Employee::print() const{ cout << lastName << ", " << firstName << "\nHired: "; hireDate.print(); cout << " Birth date: "; birthDate.print(); cout << endl;}Employee::~Employee(){ cout << "Employee object destructor: " << lastName << ", " << firstName << endl;}

Page 14: CSE 2341 - Honors Principles of Computer Science I

Sample Driver

14

#include <iostream>using namespace std;

#include "emply1.h“int main(){ Employee e( "Bob", "Jones", 7, 24, 1949, 3, 12, 1988 ); e.print(); Date d( 14, 35, 1994 ); cout << endl; return 0;}

for birthDate for hireDate

Page 15: CSE 2341 - Honors Principles of Computer Science I

has - a

15

Aggregation/Compositionone object contains another object

car has a motorhuman has a brainRobot has an arm

Page 16: CSE 2341 - Honors Principles of Computer Science I

aggregation

16

class car{ public://stuff private: Motor myMotor;};

class human{ public://stuff private: Brain myBrain;};

Page 17: CSE 2341 - Honors Principles of Computer Science I

is – a

17

is a - relationship that represents inheritance/generalization (class derivation)

For example:Helicopter is a vehicleTrain is a vehicleTruck is a vehiclePlane is a vehicleMotorcycle is a vehicle

Page 18: CSE 2341 - Honors Principles of Computer Science I

Inheritance Diagram

18

CAR HELICOPTER

Vehicle

TRAIN

Page 19: CSE 2341 - Honors Principles of Computer Science I

Uses A Relationship

19

An operation of class A receives or returns an object of class B

In the process of an operation of class A, an object of class B must be inspected or created

Objects of class A contain a reference to objects of class B

Page 20: CSE 2341 - Honors Principles of Computer Science I

Identify Relationships

20

Sun, Planet __________Elevator, Rider __________Date, Person __________Person, Employee __________Circle, Point __________Manager, Employee __________Triangle, Rectangle__________Computer, Keyboard __________Computer, Person __________Computer, Laptop __________

Page 21: CSE 2341 - Honors Principles of Computer Science I

Two Types of Inheritance

21

BASE1 BASE2 BASE3

DerivedA DerivedC

DerivedB

DerivedD

Single Multiple

Page 22: CSE 2341 - Honors Principles of Computer Science I

Draw an Inheritance Diagram

22

PersonStudentNameAddressProfessor