Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 6 1.

Slides:



Advertisements
Similar presentations
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Advertisements

Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction 17.2 const (Constant) Objects and const Member.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
1 Lecture Note 7- Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
Classes and Objects. const (Constant) Objects and const Member Functions Principle of least privilege –Only give objects permissions they need, no more.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
1 Object-Oriented Programming Using C++ CLASS 5. 2 Object Composition Object composition occurs when a class contains an instance of another class. Creates.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10 1.
Inheritance One of the most powerful features of C++
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Chapter 11 More about classes and OOP. Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance.
11 Introduction to Object Oriented Programming (Continued) Cats.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
 2002 Prentice Hall. All rights reserved. 1 Chapter 8 – Object-Based Programming Outline 8.1 Introduction 8.2 Implementing a Time Abstract Data Type with.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
Chapter 17 - C++ Classes: Part II Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3: Classes May 24,
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
EEL 3801 Part VI Fundamentals of C and C++ Programming Classes and Objects.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 7.1Introduction 7.2const (Constant) Objects and const Member Functions 7.3Composition: Objects.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
1 Compiler directive: #define, usage 1 #include using namespace std; #define TAX //double TAX=0.08; #define LAST_NAME "Li" #define FIRST_NAME "Dennis"
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
CompSci 230 S Programming Techniques
#define #include<iostream> using namespace std; #define GO
Chapter 17 - C++ Classes: Part II
Chapter 17 - C++ Classes: Part II
Chapter 7: Classes Part II
Chapter 7: Classes Part II
Lecture 4: Classes (continued) June 14, 2004
Andy Wang Object Oriented Programming in C++ COP 3330
Lecture 5: Classes September 14, 2004
Chapter 7: Classes Part II
Constant Member Functions
Capitolo 7: Classes Part II
Chapter 19 - Inheritance Outline 19.1 Introduction
CS148 Introduction to Programming II
Chapter 19 - Inheritance Outline 19.1 Introduction
Class Diagram.
Chapter 7: Classes Part II
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 7: Classes (Part II)
Chapter 7: Classes Part II
Presentation transcript:

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6 1

Quick Look 2 Object Composition

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

Object Composition 4 Object composition occurs when a class contains an instance of another class Creates a “has a” relationship between classes

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

Date Class 6 #ifndefDATE1_H #defineDATE1_H class Date { public: Date(int = 1, int = 1, int = 1900); void print ( ) const; ~Date( ); private: int month;// 1-12 int day;// 1-31 based on month int year;// any year int checkDay(int); }; #endif

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; }

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; }

Date Class 9 //Print Date object in form month/day/year void Date::print() const { cout << month << ‘/’ << day << ‘/’ << year; } Date::~Date() { cout << “Date object destructor for date ”; print(); cout << endl; }

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

Employee Class 11 // Member function definitions for Employee class. #include using namespace std; #include #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

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; }

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; }

Sample Driver 14 #include 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 birthDatefor hireDate

has - a 15 Aggregation/Composition one object contains another object car has a motor human has a brain Robot has an arm

aggregation 16 class car { public://stuff private: Motor myMotor; }; class human { public://stuff private: Brain myBrain; };

is – a 17 is a - relationship that represents inheritance/generalization (class derivation) For example: Helicopter is a vehicle Train is a vehicle Truck is a vehicle Plane is a vehicle Motorcycle is a vehicle

Inheritance Diagram 18 CARHELICOPTER Vehicle TRAIN

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

Identify Relationships 20 Sun, Planet__________ Elevator, Rider__________ Date, Person__________ Person, Employee__________ Circle, Point__________ Manager, Employee__________ Triangle, Rectangle__________ Computer, Keyboard__________ Computer, Person__________ Computer, Laptop__________

Two Types of Inheritance 21 BASE1 BASE2BASE3 DerivedA DerivedC DerivedB DerivedD Single Multiple

Draw an Inheritance Diagram 22 Person Student Name Address Professor