Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Advertisements

Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
1 CMSC 202 More C++ Classes. 2 Announcements Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March.
 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)
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
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ü.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
Classes and Objects. const (Constant) Objects and const Member Functions Principle of least privilege –Only give objects permissions they need, no more.
Introduction to Classes and Data Abstraction
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 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.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4 - Classes Jan 27,
1 Classes and Data Abstraction Part II Initializing Class Objects: Constructors Constructors  Initialize data members  Same name as class 
1 Chapter 4 CLASSES AND OBJECTS. 2 Outlines Procedural vs. Object ‑ Oriented Programming C++ Structures versus C Structures Classes –Accessing Class Members.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
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.
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.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2000 Prentice Hall, Inc. All rights reserved const (Constant) Objects and const Member Functions Principle of least privilege –Only give objects.
 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,
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
1 Lecture 6 Classes and Data Abstraction: Part II.
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 7.1Introduction 7.2const (Constant) Objects and const Member Functions 7.3Composition: Objects.
 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 Prentice Hall, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf Introduction Object-oriented programming (OOP) –Objects Encapsulates.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
 2000 Prentice Hall, Inc. All rights reserved. 1 Outline 7.1Introduction 7.2 const (Constant) Objects and const Member Functions 7.3Composition: Objects.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Jan 20, 2004.
Chapter 16: Classes and Data Abstraction
Lecture 5: Classes (continued) Feb 1, 2005
Chapter 16: Classes and Data Abstraction
Chapter 17 - C++ Classes: Part II
Chapter 7: Classes Part II
Chapter 7: Classes Part II
Lecture 4: Classes (continued) June 14, 2004
Classes: A Deeper Look Outline
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Chapter 7: Classes Part II
Capitolo 7: Classes Part II
7.4 friend Functions and friend Classes
Object-oriented programming (OOP)
Chapter 6: Classes and Data Abstraction
#include <iostream.h>
CISC181 Introduction to Computer Science Dr
Chapter 7: Classes Part II
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
Chapter 7: Classes (Part II)
Chapter 7: Classes Part II
Presentation transcript:

Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public: SalesPerson(); // constructor void getSalesFromUser(); // get sales figures from keyboard void setSales( int, double ); // User supplies one month's // sales figures. void printAnnualSales(); private: double totalAnnualSales(); // utility function double sales[ 12 ]; // 12 monthly sales figures }; #endif

Classi - Esempi2 // Member functions for class SalesPerson #include using std::cout; using std::cin; using std::endl; #include using std::setprecision; using std::setiosflags; using std::ios; #include "salesp.h" // Constructor function initializes array SalesPerson::SalesPerson() { for ( int i = 0; i < 12; i++ ) sales[ i ] = 0.0; }

Classi - Esempi3 // Function to get 12 sales figures from the user // at the keyboard void SalesPerson::getSalesFromUser() { double salesFigure; for ( int i = 1; i <= 12; i++ ) { cout << "Enter sales amount for month " << i << ": "; cin >> salesFigure; setSales( i, salesFigure ); } // Function to set one of the 12 monthly sales figures. // Note that the month value must be from 0 to 11. void SalesPerson::setSales( int month, double amount ) { if ( month >= 1 && month 0 ) sales[ month - 1 ] = amount; // adjust for subscripts 0-11 else cout << "Invalid month or sales figure" << endl; }

Classi - Esempi4 // Print the total annual sales void SalesPerson::printAnnualSales() { cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << "\nThe total annual sales are: $" << totalAnnualSales() << endl; } // Private utility function to total annual sales double SalesPerson::totalAnnualSales() { double total = 0.0; for ( int i = 0; i < 12; i++ ) total += sales[ i ]; return total; }

Classi - Esempi5 // Demonstrating a utility function // Compile with salesp.cpp #include "salesp.h" int main() { SalesPerson s; // create SalesPerson object s s.getSalesFromUser(); // note simple sequential code s.printAnnualSales(); // no control structures in main return 0; }

Classi - Esempi6 Enter sales amount for month 1: Enter sales amount for month 2: Enter sales amount for month 3: Enter sales amount for month 4: Enter sales amount for month 5: Enter sales amount for month 6: Enter sales amount for month 7: Enter sales amount for month 8: Enter sales amount for month 9: Enter sales amount for month 10: Enter sales amount for month 11: Enter sales amount for month 12: The total annual sales are: $

Classi - Esempi7 / Declaration of the Time class. // Member functions defined in time3.cpp // preprocessor directives that // prevent multiple inclusions of header file #ifndef TIME3_H #define TIME3_H class Time { public: Time( int = 0, int = 0, int = 0 ); // constructor // set functions void setTime( int, int, int ); // set hour, minute, second void setHour( int ); // set hour void setMinute( int ); // set minute void setSecond( int ); // set second

Classi - Esempi8 // get functions int getHour(); // return hour int getMinute(); // return minute int getSecond(); // return second void printMilitary(); // output military time void printStandard(); // output standard time private: int hour; // int minute; // int second; // }; #endif

Classi - Esempi9 // Member function definitions for Time class. #include using std::cout; #include "time3.h" // Constructor function to initialize private data. // Calls member function setTime to set variables. // Default values are 0 (see class definition). Time::Time( int hr, int min, int sec ) { setTime( hr, min, sec ); } // Set the values of hour, minute, and second. void Time::setTime( int h, int m, int s ) { setHour( h ); setMinute( m ); setSecond( s ); }

Classi - Esempi10 // Set the hour value void Time::setHour( int h ) { hour = ( h >= 0 && h < 24 ) ? h : 0; } // Set the minute value void Time::setMinute( int m ) { minute = ( m >= 0 && m < 60 ) ? m : 0; } // Set the second value void Time::setSecond( int s ) { second = ( s >= 0 && s < 60 ) ? s : 0; } // Get the hour value int Time::getHour() { return hour; } // Get the minute value int Time::getMinute() { return minute; }

Classi - Esempi11 // Get the second value int Time::getSecond() { return second; } // Print time is military format void Time::printMilitary() { cout << ( hour < 10 ? "0" : "" ) << hour << ":" << ( minute < 10 ? "0" : "" ) << minute; } // Print time in standard format void Time::printStandard() { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << ( minute < 10 ? "0" : "" ) << minute << ":" << ( second < 10 ? "0" : "" ) << second << ( hour < 12 ? " AM" : " PM" ); }

Classi - Esempi12 // Demonstrating the Time class set and get functions #include using std::cout; using std::endl; #include "time3.h" void incrementMinutes( Time &, const int ); int main() { Time t; t.setHour( 17 ); t.setMinute( 34 ); t.setSecond( 25 ); cout << "Result of setting all valid values:\n" << " Hour: " << t.getHour() << " Minute: " << t.getMinute() << " Second: " << t.getSecond();

Classi - Esempi13 t.setHour( 234 ); // invalid hour set to 0 t.setMinute( 43 ); t.setSecond( 6373 ); // invalid second set to 0 cout << "\n\nResult of attempting to set invalid hour and" << " second:\n Hour: " << t.getHour() << " Minute: " << t.getMinute() << " Second: " << t.getSecond() << "\n\n"; t.setTime( 11, 58, 0 ); incrementMinutes( t, 3 ); return 0; } void incrementMinutes(Time &tt, const int count) { cout << "Incrementing minute " << count << " times:\nStart time: "; tt.printStandard();

Classi - Esempi14 for ( int i = 0; i < count; i++ ) { tt.setMinute( ( tt.getMinute() + 1 ) % 60); if ( tt.getMinute() == 0 ) tt.setHour( ( tt.getHour() + 1 ) % 24); cout << "\nminute + 1: "; tt.printStandard(); } cout << endl; }

Classi - Esempi15 Result of setting all valid values: Hour: 17 Minute: 34 Second: 25 Result to attempting to set invalid hour and second: Hour: 0 Minute: 43 Second: 0 Incrementing minute 3 times: Start time: 11:58:00 AM minute + 1: 11:59:00 AM minute + 1: 12:00:00 PM minute + 1: 12:01:00 PM