ΗΥ150a Φροντιστήριο 4 08/12/2017

Σχετικά έγγραφα
Struct : public by default but Class: private by default. ClassPoint { Struct Point { Public: Double x; Double x; Double y; Double y;

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 5: Component Adaptation Environment (COPE)

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

The Simply Typed Lambda Calculus

Δυναμική μνήμη με πίνακες και λίστες

derivation of the Laplacian from rectangular to spherical coordinates

Abstract classes, Interfaces ΦΡΟΝΤΙΣΤΗΡΙΟ JAVA

Αρχές Τεχνολογίας Λογισμικού Εργαστήριο

Instruction Execution Times

(C) 2010 Pearson Education, Inc. All rights reserved.

ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

Εργαστήριο Java. Διδάσκουσα: Εργαστηριακοί Συνεργάτες:

Αντισταθμιστική ανάλυση

(Διαφάνειες Νίκου Βιδάκη)

Assalamu `alaikum wr. wb.

2 Composition. Invertible Mappings

EE512: Error Control Coding

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

Finite Field Problems: Solutions

Approximation of distance between locations on earth given by latitude and longitude

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

Main source: "Discrete-time systems and computer control" by Α. ΣΚΟΔΡΑΣ ΨΗΦΙΑΚΟΣ ΕΛΕΓΧΟΣ ΔΙΑΛΕΞΗ 4 ΔΙΑΦΑΝΕΙΑ 1

Πώς μπορεί κανείς να έχει έναν διερμηνέα κατά την επίσκεψή του στον Οικογενειακό του Γιατρό στο Ίσλινγκτον Getting an interpreter when you visit your

the total number of electrons passing through the lamp.

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

Section 8.3 Trigonometric Equations

Galatia SIL Keyboard Information

Η λέξη κλειδί this. Γαβαλάς Δαμιανός

Χρειάζεται να φέρω μαζί μου τα πρωτότυπα έγγραφα ή τα αντίγραφα; Asking if you need to provide the original documents or copies Ποια είναι τα κριτήρια

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

TMA4115 Matematikk 3

Potential Dividers. 46 minutes. 46 marks. Page 1 of 11

ΕΠΙΧΕΙΡΗΣΙΑΚΗ ΑΛΛΗΛΟΓΡΑΦΙΑ ΚΑΙ ΕΠΙΚΟΙΝΩΝΙΑ ΣΤΗΝ ΑΓΓΛΙΚΗ ΓΛΩΣΣΑ

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

Writing for A class. Describe yourself Topic 1: Write your name, your nationality, your hobby, your pet. Write where you live.

Στο εστιατόριο «ToDokimasesPrinToBgaleisStonKosmo?» έξω από τους δακτυλίους του Κρόνου, οι παραγγελίες γίνονται ηλεκτρονικά.

Partial Differential Equations in Biology The boundary element method. March 26, 2013

CYTA Cloud Server Set Up Instructions

Fractional Colorings and Zykov Products of graphs

HY150a Φροντιστήριο 3 24/11/2017

Block Ciphers Modes. Ramki Thurimella

Case 1: Original version of a bill available in only one language.

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

[1] P Q. Fig. 3.1

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

Αντικειμενοστρεφής Προγραμματισμός

ΑΓΓΛΙΚΑ Ι. Ενότητα 7α: Impact of the Internet on Economic Education. Ζωή Κανταρίδου Τμήμα Εφαρμοσμένης Πληροφορικής

Areas and Lengths in Polar Coordinates

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

CHAPTER 12: PERIMETER, AREA, CIRCUMFERENCE, AND 12.1 INTRODUCTION TO GEOMETRIC 12.2 PERIMETER: SQUARES, RECTANGLES,

Right Rear Door. Let's now finish the door hinge saga with the right rear door

Living and Nonliving Created by: Maria Okraska

Concrete Mathematics Exercises from 30 September 2016

SOAP API. Table of Contents

ΕΙΣΑΓΩΓΗ ΣΤΗ ΣΤΑΤΙΣΤΙΚΗ ΑΝΑΛΥΣΗ

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

Areas and Lengths in Polar Coordinates

Εισαγωγή στον Αντικειμενοστρέφή Προγραμματισμό Διάλεξη #13

Section 9.2 Polar Equations and Graphs

Advanced Subsidiary Unit 1: Understanding and Written Response

Other Test Constructions: Likelihood Ratio & Bayes Tests

Example Sheet 3 Solutions

Προγραμματισμός Ι. Κλάσεις και Αντικείμενα. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

ΑΝΙΧΝΕΥΣΗ ΓΕΓΟΝΟΤΩΝ ΒΗΜΑΤΙΣΜΟΥ ΜΕ ΧΡΗΣΗ ΕΠΙΤΑΧΥΝΣΙΟΜΕΤΡΩΝ ΔΙΠΛΩΜΑΤΙΚΗ ΕΡΓΑΣΙΑ

CS 150 Assignment 2. Assignment 2 Overview Opening Files Arrays ( and a little bit of pointers ) Strings and Comparison Q/A

Strain gauge and rosettes

Goals of this Lecture. Generics(Γενικότητα) Generic Data Structures Example. Generic Data Structures Example

Μηχανική Μάθηση Hypothesis Testing

Partial Trace and Partial Transpose

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

Στεγαστική δήλωση: Σχετικά με τις στεγαστικές υπηρεσίες που λαμβάνετε (Residential statement: About the residential services you get)

Volume of a Cuboid. Volume = length x breadth x height. V = l x b x h. The formula for the volume of a cuboid is

Matrices and Determinants

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΣΧΟΛΗ ΘΕΤΙΚΩΝ ΕΠΙΣΤΗΜΩΝ ΤΜΗΜΑ ΜΑΘΗΜΑΤΙΚΩΝ ΙΠΛΩΜΑΤΙΚΗ ΕΡΓΑΣΙΑ

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Τ.Ε.Ι. ΔΥΤΙΚΗΣ ΜΑΚΕΔΟΝΙΑΣ ΠΑΡΑΡΤΗΜΑ ΚΑΣΤΟΡΙΑΣ ΤΜΗΜΑ ΔΗΜΟΣΙΩΝ ΣΧΕΣΕΩΝ & ΕΠΙΚΟΙΝΩΝΙΑΣ

Mean bond enthalpy Standard enthalpy of formation Bond N H N N N N H O O O

ΗΥ-150 Programming. Assignment 3. HY150 Programming, University of Crete

Every set of first-order formulas is equivalent to an independent set

The challenges of non-stable predicates

Homework 8 Model Solution Section

Paper Reference. Paper Reference(s) 1776/04 Edexcel GCSE Modern Greek Paper 4 Writing. Thursday 21 May 2009 Afternoon Time: 1 hour 15 minutes

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

Congruence Classes of Invertible Matrices of Order 3 over F 2

Μορφοποίηση υπό όρους : Μορφή > Μορφοποίηση υπό όρους/γραμμές δεδομένων/μορφοποίηση μόο των κελιών που περιέχουν/

VBA ΣΤΟ WORD. 1. Συχνά, όταν ήθελα να δώσω ένα φυλλάδιο εργασίας με ασκήσεις στους μαθητές έκανα το εξής: Version ΗΜΙΤΕΛΗΣ!!!!

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 11/3/2006

Solutions to the Schrodinger equation atomic orbitals. Ψ 1 s Ψ 2 s Ψ 2 px Ψ 2 py Ψ 2 pz

department listing department name αχχουντσ ϕανε βαλικτ δδσϕηασδδη σδηφγ ασκϕηλκ τεχηνιχαλ αλαν ϕουν διξ τεχηνιχαλ ϕοην µαριανι

Wrapper Classes, Abstract Classes and Interfaces

Aντικειμενοστραφής. Προγραμματισμός. Κληρονομικότητα

Section 7.6 Double and Half Angle Formulas

Transcript:

ΗΥ150a Φροντιστήριο 4 08/12/2017

Outline Classes Member Functions Constructor Overloading Access specifiers Encapsulation Inheritance Polymorphism Vectors 1

Classes Classes in C++ are like structs, except that classes provide more power and flexibility. The following class and struct are effectively identical. struct Rectangle { class Rectangle { float length; public: float width; float length; }; float width; }; Structs are public by default; Classes are private by default. A class is a structure that binds the data and its logically related functions together. 2

class Rectangle { Classes - Example - private: float length; Member variables float width; public: Rectangle(); Constructors Rectangle(float x, float y); void set_length(float x); void set_width(float x); Member functions float get_area(void); }; Rectangle::Rectangle(void){ cout << "Object is being created" << endl; } void Rectangle::set_length(float x){length = x;} float Rectangle::get_area(void){return length * width;} 3

Member Functions A member function can be private, protected or public. It can be defined inside or outside the class. Can access private, protected and public data of its class. Can not access private data members of another class. 4

Constructor A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type, not even void. Constructors can be very useful for setting initial values for certain member variables. Can have multiple constructors (with different arguments), but only one can be executed for each new object. 5

class Rectangle { }; float length; float width; public: Rectangle(); Rectangle(float x, float y) Constructor - Example - Rectangle::Rectangle(void){ cout << "Object is being created" << endl; } Rectangle::Rectangle(float x, float y){ length = x; width = y; } int main(void) { Rectangle R1 = Rectangle(); Rectangle R2 = Rectangle(10.0, 15.0); Rectangle R3(10.0, 10.0); } 6

Overloading We can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. We can not overload function declarations that differ only by the return type. Overloading can be also used for Constructors (as seen in the previous slide). 7

class printdata { public: Overloading - Example - void print(int i){ cout << "Printing int:" << i << endl; } void print(double f){ cout << "Printing float:" << f << endl; } }; void print(char* c){ cout << "Printing characters:" << c << endl; } int main(void) { printdata pd; pd.print(5); pd.print(500.263); pd.print("hello C++"); return 0; } 8

Access Specifiers The access restriction to the class members is specified by the labeled public, private and protected sections within the body of the class. class Example { public: // Public class members (data and functions) private: // Private members protected: // Protected members }; 9

Access Specifiers class Example { int num_a // Private by default int GetA() {return num_a;} private: int num_b int GetB() {return num_b;} int main() { Example obj; obj.num_d = 5; //correct protected: std::cout << obj.getd(); //correct int num_c int GetC() {return num_c;} obj.num_a = 2; //wrong public: std::cout << obj.getb(); //wrong int num_d } int GetD() {return num_d;} }; 10

Access Specifiers Access Public Protected Private Same Class Yes Yes Yes Derived Class Yes Yes No Outside Class Yes No No 11

Object Oriented Programming Encapsulation: grouping related data and functions together as objects, and defining and interface to those objects. Inheritance: allowing code to be reused between related types. Polymorphism: allowing a value to be one of several types, and determining at runtime which functions to call based on its type. 12

Object Oriented Programming - Encapsulation - Grouping related data and functions together as objects. If someone gives us a class, we do not need to know how this class works in order to use it; we just need to know its public data/methods -- its interface. This is often compared to operating a car: when you drive, you don t care how the steering wheel makes the wheels turn; you just care that the interface provided by the car (steering wheel) allows you to accomplish your goal. 13

Object Oriented Programming - Encapsulation - Interfaces abstract away the details of how all the operations are actually performed, allowing the programmer to focus on how objects will use each other s interfaces how they interact. This is why C++ requires specifying public access specifiers. By default it assumes that the things you define in a class are internal details, and that someone using your code should not have to worry about. 14

Object Oriented Programming - Inheritance - When creating a class, instead of writing completely new data members and member functions, the programmer can allow the new class to inherit the members of an existing class. The existing class is called the base class, and the new one is referred to as the derived class. The idea of inheritance implements a relationship between classes e.g., dog IS-A animal, undergraduate IS-A student. A class can inherit from multiple classes. 15

Object Oriented Programming - Inheritance - class Shape { // Base class protected: int width, height; public: void set_width(int x) {width=x;} void set_height(int y) {height =y;} }; class Rectangle: public Shape { // Derived class public: int get_area() {return (height * width);} }; int main(void) { Rectangle Rect; Rect.set_width(5); Rect.set_height(4); cout << Total area:" << Rect.get_area() << endl; return 0; } 16

Object Oriented Programming - Polymorphism - Polymorphism refers to the ability of one object to have many types. If we have a function that expects a Shape object, we can pass it a Rectangle object, because every Rectangle is also a Shape. A member function of the base class that is later redefined in each of the derived classes has to be declared virtual. Non-virtual members can also be redefined in derived classes, but non-virtual members of derived classes cannot be accessed through a reference of the base class. Essentially, what the virtual keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer (pointer to the base class that is pointing to an object of the derived class) 17

Object Oriented Programming - Polymorphism - class Shape { protected: float width, height; public: Shape(float x, float y) {width = x; height = y;} virtual float area() {cout << "Parent class" << endl; return 0;} }; class Rectangle: public Shape { public: Rectangle(float x, float y):shape(x, y) { } float area() {return (width * height);} }; class Triangle: public Shape { public: Triangle(float x, float y):shape(x, y) { } float area () {return (width * height / 2); } }; 18 int main() { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); } shape = &rec; shape->area(); shape = &tri; shape->area(); return 0;

Object Oriented Programming - Abstract classes - class Shape { protected: float width, height; public: Shape(float x, float y) { width = x; height = y; } // Pure virtual function virtual float area() = 0; }; A class with at least one pure virtual function is called abstract Cannot create objects of the abstract class; only of the derived classes. The pure virtual function of the base class has to be implemented in the derived classes. 19

Vectors Vectors are sequence containers representing arrays that can change in size. Like arrays, vectors use contiguous storage locations for their elements. Their elements can be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. Unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. Compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically. 20

Vectors - Example - class Person { public: Person(); void setname(string x){ name = x; } void setage(int y){ age = y; } void printname(); }; private: string name; int age; Person::Person(){age = 0;} int main(){ vector<person> vec; // Vector of person objects string Name; Person *p1; for (int n=0; n<5; n++){ getline(cin, Name); // Get name from input } p1 = new Person; // Create new object p1->setname(name); vec.push_back(*p1); // Add in the vector // Iterate through all items in the vector vector<person>::iterator iter; for (iter = vec.begin(); iter!= vec.end(); ++iter ){ iter->printname(); } return 0;} 21

Assignment 4

Ζητείται ένα πρόγραμμα που υλοποιεί τις βασικές λειτουργίες της γραμματείας : (α) εισαγωγή μαθημάτων και φοιτητών (β) εγγραφή σε μαθήματα (γ) εισαγωγή βαθμού - υπολογισμός μέσου όρου Το πρόγραμμα ζητείται να εμφανίζει μια γραμμή εντολών (command prompt) με την οποία ο χρήστης θα μπορεί να δίνει εντολές. insert_course insert_course_from_file [filename] insert_student insert_student_from_file [filename] enroll [course_code] [student_id] disenroll [course_code] [student_id] enrolled_in_course [course_code] add_grade print_student [student_id] course_stats [course_code] 22

Θα πρέπει να υλοποιηθεί η κλάση course, η οποία θα περιέχει τον τίτλο και κωδικό του μαθήματος (private, χρησιμοποιείστε public μεθόδους). Τα προσφερόμενα μαθήματα θα πρέπει να εισάγονται στο σύστημα από την κύρια είσοδο με την εντολή insert_course, και από csv αρχείο (comma-separated) με την εντολή insert_courses_from_file [filename]. Κάθε γραμμή του αρχείου θα περιέχει το όνομα μαθήματος, τον κωδικό μαθήματος, και την τιμή undergraduate ή graduate, πχ. Programming, hy150, undergraduate class course { Operating Systems, hy345, undergraduate private:. public:. }; 23

Θα πρέπει να υλοποιηθεί η κλάση student, την οποία θα κληρονομούν οι κλάσεις undergraduate_student και graduate_student, για τους προπτυχιακούς και μεταπτυχιακούς φοιτητές αντίστοιχα. Με παρόμοιο τρόπο, οι φοιτητές θα εισάγονται στο σύστημα με τις εντολές insert_course και insert_courses_from_file [filename]. Κάθε γραμμή του αρχείου φοιτητών θα περιέχει το ονοματεπώνυμο, του φοιτητή, τον κωδικό (student_id), και την τιμή undergraduate ή graduate. - Ποιες οι διαφορές μεταξύ προπτυχιακών και μεταπτυχιακών? 24

enroll [course_code] [student_id] disenroll [course_code] [student_id] Οι προπτυχιακοί φοιτητές μπορούν να εγγραφούν σε όσα μαθήματα επιθυμούν. Οι μεταπτυχιακοί φοιτητές μπορούν να εγγράφουν σε όσα μεταπτυχιακά μαθήματα επιθυμούν και το πολύ σε 2 προπτυχιακά μαθήματα (έλεγχος). Οι μεταπτυχιακοί φοιτητές που έχουν ήδη εγγραφεί σε 2 προπτυχιακά μαθήματα έχουν την δυνατότητα να διαγραφούν από κάποιο από αυτά και να εγγραφούν σε κάποιο άλλο. Η εντολή enrolled_in_course [course_code] θα πρέπει να τυπώνει τίτλο και κωδικό μαθήματος, τον αριθμό των φοιτητών που είναι εγγεγραμμένοι στο μάθημα, και το student_id αυτών των φοιτητών. 25

Το σύστημα θα επιτρέπει την καταχώρηση βαθμού για τα μαθήματα που ο φοιτητής έχει παρακαθίσει σε εξετάσεις. Με την εντολή add_grade θα εισάγεται ο βαθμός του φοιτητή student_id, για το μάθημα course_id. Επιτρέπεται καταχώρηση βαθμού μόνο σε μαθήματα στα οποία ο φοιτητής είναι εγγεγραμμένος. Ο βαθμός μπορεί να πάρει τιμές από 0 έως 10. Για τους προπτυχιακούς ο βαθμός επιτυχής παρακολούθησης είναι >=5, για τους μεταπτυχιακούς >=6. Αν ο φοιτητής περάσει το μάθημα, παύει να θεωρείται εγγεγραμμένος (enrolled) στο μάθημα. Αν ο βαθμός είναι μικρότερος από το όριο, ο φοιτητής παραμένει εγγεγραμμένος στο μάθημα χωρίς να χρειάζεται να κάνει ξανά εγγραφή. 26

Η εντολή print_student [student_id] θα πρέπει να τυπώνει: Όλα τα στοιχεία του φοιτητή. Τα μαθήματα στα οποία ο φοιτητής είναι εγγεγραμμένος (δεν έχει εξεταστεί ακόμα ή ο βαθμός είναι μικρότερος από το όριο). Τα μαθήματα που έχει παρακολουθήσει επιτυχώς, τους βαθμούς σε κάθε ένα από αυτά, και τον μέσο όρο τους. Η εντολή course_stats [course_code] θα πρέπει να τυπώνει: Όλες τις πληροφορίες σχετικά με το μάθημα (τίτλος, κωδικός). Τον αριθμό των φοιτητών που είναι εγγεγραμμένοι στο μάθημα. Το ποσοστό των φοιτητών που πέρασε το μάθημα (από όσους έχουν καταχωρημένο βαθμό). Μέσο όρο για τους επιτυχόντες. Αν το μάθημα παρακολουθείται και από προπτυχιακούς αλλά και μεταπτυχιακούς φοιτητές, τότε να υπολογιστεί ο μέσος όρος κάθε κατηγορίας ξεχωριστά. 27