Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

2000 Prentice Hall, Inc. All rights reserved Oggetti const e funzioni membro const 2. Composizione: oggetti come membri di classi 3. Funzioni friend.
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
1 Lecture Note 7- Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
Definition Class In C++, the class is the construct primarily used to create objects.
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
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 6 1.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
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.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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?
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
11 Introduction to Object Oriented Programming (Continued) Cats.
 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.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
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"
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Pointer to an Object Can define a pointer to an object:
A CLASS CONSISTS OF VARIABLES,
CS505 Data Structures and Algorithms
Access Functions and Friend Functions
Submission Example May 14, 2018
#define #include<iostream> using namespace std; #define GO
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?
Chapter 9 Type Conversions
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?
14.4 Copy Constructors.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
group work #hifiTeam
C++ Interlude 1 C++ Classes
Classes.
Today’s Topic Const Ref:
Chapter 7: Classes Part II
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CMSC 341 C++ and OOP.
CS 144 Advanced C++ Programming February 21 Class Meeting
CS31 Discussion 1D Winter18: week 6
Class: Special Topics Overloading (methods) Copy Constructors
CMSC 341 C++ and OOP.
Chapter 11 Classes.
Data Structures & Programming
Presentation transcript:

Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp #ifndef DATE1_H #define DATE1_H class Date { public: Date( int = 1, int = 1, int = 1900 ); // default constructor void print() const; // print date in month/day/year format ~Date(); // provided to confirm destruction order 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 ); }; #endif

Approfondimento Classi - Esempi2 // Member function definitions for Date class. #include using std::cout; using std::endl; #include "date1.h" // Constructor: Confirm proper value for month; // call utility function checkDay to confirm proper // value for day. Date::Date( int mn, int dy, int yr ) { if ( mn > 0 && mn <= 12 ) // validate the month month = mn; else { month = 1; cout << "Month " << mn << " invalid. Set to month 1.\n"; }

Approfondimento Classi - Esempi3 year = yr; // should validate yr day = checkDay( dy ); // validate the day cout << "Date object constructor for date "; print(); // interesting: a print with no arguments cout << endl; } // Print Date object in form month/day/year void Date::print() const { cout << month << '/' << day << '/' << year; } // Destructor: provided to confirm destruction order Date::~Date() { cout << "Date object destructor for date "; print(); cout << endl; }

Approfondimento Classi - Esempi4 // Utility function to confirm proper day value // based on month and year. // Is the year 2000 a leap year? int Date::checkDay( int testDay ) { static const 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 && // February: Check for leap year testDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) ) return testDay; cout << "Day " << testDay << " invalid. Set to day 1.\n"; return 1; // leave object in consistent state if bad value }

Approfondimento Classi - Esempi5 // Declaration of the Employee class. // Member functions defined in emply1.cpp #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(); // provided to confirm destruction order private: char firstName[ 25 ]; char lastName[ 25 ]; const Date birthDate; const Date hireDate; }; #endif

Approfondimento Classi - Esempi6 // Member function definitions for Employee class. #include using std::cout; using std::endl; #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 ) { // copy fname into firstName and be sure that it fits int length = strlen( fname ); length = ( length < 25 ? length : 24 ); strncpy( firstName, fname, length ); firstName[ length ] = '\0';

Approfondimento Classi - Esempi7 // copy lname into lastName and be sure that it fits length = strlen( lname ); length = ( length < 25 ? length : 24 ); strncpy( lastName, lname, length ); lastName[ length ] = '\0'; cout << "Employee object constructor: " << firstName << ' ' << lastName << endl; } void Employee::print() const { cout << lastName << ", " << firstName << "\nHired: "; hireDate.print(); cout << " Birth date: "; birthDate.print(); cout << endl; }

Approfondimento Classi - Esempi8 // Destructor: provided to confirm destruction order Employee::~Employee() { cout << "Employee object destructor: " << lastName << ", " << firstName << endl; }

Approfondimento Classi - Esempi9 // Demonstrating composition: an object with member objects. #include using std::cout; using std::endl; #include "emply1.h" int main() { Employee e( "Bob", "Jones", 7, 24, 1949, 3, 12, 1988 ); cout << '\n'; e.print(); cout << "\nTest Date constructor with invalid values:\n"; Date d( 14, 35, 1994 ); // invalid Date values cout << endl; return 0; }

Approfondimento Classi - Esempi10 Date object constructor for date 7/24/1949 Date object constructor for date 3/12/1988 Employee object constructor: Bob Jones Jones, Bob Hired: 3/12/1988 Birth date: 7/24/1949 Test Date constructor with invalid values: Month 14 invalid. Set to month 1. Day 35 invalid. Set to day 1. Date object constructor for date 1/1/1994 Date object destructor for date 1/1/1994 Employee object destructor: Jones, Bob Date object destructor for date 3/12/1988 Date object destructor for date 7/24/1949

Approfondimento Classi - Esempi11 // Argomento: membri static di una classe #ifndef EMPLOY1_H #define EMPLOY1_H class Employee { public: Employee( const char*, const char* ); // constructor ~Employee(); // destructor const char *getFirstName() const; // return first name const char *getLastName() const; // return last name // static member function static int getCount(); // return # objects instantiated private: char *firstName; char *lastName; // static data member static int count; // number of objects instantiated }; #endif

Approfondimento Classi - Esempi12 // Member function definitions for class Employee #include using std::cout; using std::endl; #include #include "employ1.h" // Initialize the static data member int Employee::count = 0; // Define the static member function that // returns the number of employee objects instantiated. int Employee::getCount() { return count; }

Approfondimento Classi - Esempi13 // Constructor dynamically allocates space for the // first and last name and uses strcpy to copy // the first and last names into the object Employee::Employee( const char *first, const char *last ) { firstName = new char[ strlen( first ) + 1 ]; assert( firstName != 0 ); // ensure memory allocated strcpy( firstName, first ); lastName = new char[ strlen( last ) + 1 ]; assert( lastName != 0 ); // ensure memory allocated strcpy( lastName, last ); ++count; // increment static count of employees cout << "Employee constructor for " << firstName << ' ' << lastName << " called." << endl; }

Approfondimento Classi - Esempi14 // Destructor deallocates dynamically allocated memory Employee::~Employee() { cout << "~Employee() called for " << firstName << ' ' << lastName << endl; delete [] firstName; // recapture memory delete [] lastName; // recapture memory --count; // decrement static count of employees } // Return first name of employee const char *Employee::getFirstName() const { // Const before return type prevents client from modifying // private data. Client should copy returned string before // destructor deletes storage to prevent undefined pointer. return firstName; }

Approfondimento Classi - Esempi15 // Return last name of employee const char *Employee::getLastName() const { // Const before return type prevents client from modifying // private data. Client should copy returned string before // destructor deletes storage to prevent undefined pointer. return lastName; }

Approfondimento Classi - Esempi16 // Driver to test the employee class #include using std::cout; using std::endl; #include "employ1.h" int main() { cout << "Number of employees before instantiation is " << Employee::getCount() << endl; // use class name Employee *e1Ptr = new Employee( "Susan", "Baker" ); Employee *e2Ptr = new Employee( "Robert", "Jones" ); cout << "Number of employees after instantiation is " getCount();

Approfondimento Classi - Esempi17 cout << "\n\nEmployee 1: " getFirstName() getLastName() << "\nEmployee 2: " getFirstName() getLastName() << "\n\n"; delete e1Ptr; // recapture memory e1Ptr = 0; delete e2Ptr; // recapture memory e2Ptr = 0; cout << "Number of employees after deletion is " << Employee::getCount() << endl; return 0; }

Approfondimento Classi - Esempi18 Number of employees before instantiation is 0 Employee constructor for Susan Baker called. Employee constructor for Robert Jones called. Number of employees after instantiation is 2 Employee 1: Susan Baker Employee 2: Robert Jones ~Employee( ) called for Susan Baker ~Employee( ) called for Robert Jones Number of employees after deletion is 0