A.A. 2006/2007 Laurea di Ingegneria Informatica Fondamenti di C++ Horstmann Capitolo 3: Oggetti Revisione Prof. M. Angelaccio
Obbiettivi Acquisire familiarità con la nozione di oggetto Apprendere le proprietà delle classi guida usate nel libro/corso Definire oggetti con valori iniziali Concetto di funzione elemento e notazione dot Modificare e interrogare lo stato di un oggetto Semplici esempi applicativi
Creazione di oggetti Un oggetto è un valore che può essere creato, manipolato e memorizzato Ogni oggetto di C++ appartiene ad una classe Una classe è un tipo di dato definito dal programmatore e non primitivo in C++ (come ad esempio int o double)
Sintassi 3.1 Object Construction Class_name(construction parameters); Example: Time(19, 0, 0); Purpose: Construct a new object for use in an expression.
Sintassi 3.2 Object Variable Definition Class_name variable_name(construction parameters); Example: Time homework_due(19, 0 0); Purpose: Define a new object variable and supply parameter values for initialization.
Oggetti vs Tipi di dato primitivi Oggetti Tipo Astratto Definizione con sintassi (..) simile a funzione Uso con notazione dot Tipi di dato primitivi Tipo primitivo Definizione con dichiarazione Uso con operatori standard
Constructing Objects (Time class) To use the Time class we must include the file that contains its definition: #include "ccc_time.cpp" We use " " instead of < > because the file is not a standard header. The name stands for Computing Concepts with C++ Essentials.
Constructing Objects Creating any object is called construction. We can construct an object just like we create any variable. Time sometime; We initialize an object by passing construction parameters when the object is created. The Time class uses military time. Time day_end(23, 59, 59); /* the last second of the day */
Constructing Objects (cont.) Creating objects without parameters is called default construction. No parameters means not parenthesis! The Time class creates an object with the current time. Time now; /* the time this object is created */ Time later(); /* NO! */ An object can be created anytime by supplying the class name with (or without) parameters. sometime = Time(12, 5, 18); /* we already declared sometime */
Using Objects A function applied to an object with the dot notation is called a member function. The Time class has several member functions used to find the state of the object: now.get_seconds(); /* returns the seconds value of now */ now.get_minutes(); /* returns the minutes value of now */ now.get_hours(); /* returns the hours value of now */
Esempi con Oggetti Time Esempio 1 (add seconds) Visualizzare l ora corrispondente alla formula 9:00:00 + 1000 sec. (il risultato atteso è 9:16:40) Esempio 2 (seconds left) Calcolare il numero dei secondi rimanenti fino alla mezzanotte
Using Objects (time1.cpp)
Using Objects The Time class does not have any member functions used to set the state of the object to avoid abuse: now.set_hours(2); /* No! Not a supported member function */ now.set_hours(9999); /* Doesn't make sense */ The Time class does provide other member functions to facilitate other actions: now.add_seconds(1000); /* Changes now to move by 1000 seconds */ now.seconds_from(day_end); /* Computes number of seconds between now and day_end */
Using Objects (time2.cpp)
Real-Life Objects Object-oriented programming allows us to easily model entities from real life. As an example, the text provides a simple Employee class:
Esempio di Classe Employee All employees have names and salaries which are set at construction. Employee harry("hacker, Harry", 45000.00); Both attributes can be queried. cout << "Name: " << harry.get_name() << "\n"; cout << "Salary: " << harry.get_salary() << "\n"; Onlysalarycan beset. harry.set_salary(new_salary);
Class Diagram of an Employee Costruttore Employee - name : string - salary : double + Employee() + get_name() : string + get_salary() : double + set_salary() : void Costanti Metodi
Using Objects (employee.cpp)