ΗΥ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