Struct : public by default but Class: private by default. ClassPoint { Struct Point { Public: Double x; Double x; Double y; Double y;

Σχετικά έγγραφα
ΗΥ150a Φροντιστήριο 4 08/12/2017

The Simply Typed Lambda Calculus

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

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

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

ΗΥ- ΗΥ 150a Programming Recitation 2 Destructor

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

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

2 Composition. Invertible Mappings

derivation of the Laplacian from rectangular to spherical coordinates

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

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

Section 9.2 Polar Equations and Graphs

Instruction Execution Times

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

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

Section 8.3 Trigonometric Equations

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

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

Advanced Subsidiary Unit 1: Understanding and Written Response

Galatia SIL Keyboard Information

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

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

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

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

Test Data Management in Practice

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

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

Other Test Constructions: Likelihood Ratio & Bayes Tests

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

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

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

the total number of electrons passing through the lamp.

Block Ciphers Modes. Ramki Thurimella

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

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

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

Προγραμματισμός Ι. Εισαγωγή στην C++ Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

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

Finite Field Problems: Solutions

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

CYTA Cloud Server Set Up Instructions

Οντοκεντρικός Προγραμματισμός

Fractional Colorings and Zykov Products of graphs

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

Lecture 2. Soundness and completeness of propositional logic

Strain gauge and rosettes

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

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

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

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

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

PARTIAL NOTES for 6.1 Trigonometric Identities

Math221: HW# 1 solutions

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

( ) 2 and compare to M.

Επιβλέπουσα Καθηγήτρια: ΣΟΦΙΑ ΑΡΑΒΟΥ ΠΑΠΑΔΑΤΟΥ

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

TMA4115 Matematikk 3

4.6 Autoregressive Moving Average Model ARMA(1,1)

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

Concrete Mathematics Exercises from 30 September 2016

Homework 3 Solutions

Problem Set 3: Solutions

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

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

ΕΡΓΑΣΤΗΡΙΟ 1 - ΣΗΜΕΙΩΣΕΙΣ

EE512: Error Control Coding

Οδηγίες Αγοράς Ηλεκτρονικού Βιβλίου Instructions for Buying an ebook

Partial Trace and Partial Transpose

Section 7.6 Double and Half Angle Formulas

C.S. 430 Assignment 6, Sample Solutions

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

Δομές Δεδομένων - Εργαστήριο 2. Λίστες

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

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

Areas and Lengths in Polar Coordinates

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

Υλοποίηση Δικτυακών Υποδομών και Υπηρεσιών: OSPF Cost

6.3 Forecasting ARMA processes

Assalamu `alaikum wr. wb.

LESSON 14 (ΜΑΘΗΜΑ ΔΕΚΑΤΕΣΣΕΡΑ) REF : 202/057/34-ADV. 18 February 2014

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

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

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

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

Homework 8 Model Solution Section

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

A ΜΕΡΟΣ. 1 program Puppy_Dog; 2 3 begin 4 end. 5 6 { Result of execution 7 8 (There is no output from this program ) 9 10 }

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

Models for Probabilistic Programs with an Adversary

Κλάσεις στην Python. Δημιουργία κλάσεων

Η ΠΡΟΣΩΠΙΚΗ ΟΡΙΟΘΕΤΗΣΗ ΤΟΥ ΧΩΡΟΥ Η ΠΕΡΙΠΤΩΣΗ ΤΩΝ CHAT ROOMS

1) Formulation of the Problem as a Linear Programming Model

Example Sheet 3 Solutions

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

Εγκατάσταση λογισμικού και αναβάθμιση συσκευής Device software installation and software upgrade

The challenges of non-stable predicates

Διδάσκων: Παναγιώτης Ανδρέου

Transcript:

Classes: In C++, classes are very much like structs, except that classes provide much more power and flexibility. In fact, the following struct and class are effectively identical. Struct : public by default but Class: private by default ClassPoint { Struct Point { Public: Double x; Double x; Double y; Double y; }; };

Inline: Increase the execution time of a program. Compiler replace those function definition wherever those are being called at compile time instead of referring function definition at runtime. NOTE- This is a suggestion to compiler to make the function inline, if function is big, compiler can ignore. inline int square(int x) { return x*x; } class Vector { float x, y, z; public: Vector(float x, float y, float z) : x(x), y(y), z(z) {} float GetX(void) { return x; } // by default treated inline }; int x = square(2);// function code copied here so this is equal to: int x = 2*2; Vector v(0, 1, 2); x = v.getx(); // code from Vector::GetX also is copied here

Overloading: You 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. You can not overload function declarations that differ only by return type.

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

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 at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

Constructor <<Get-ers>> for be able to call protected, private members But without changing var. values

Object-Oriented Programming OOP: Encapsulation: grouping related data and functions together as objects and defining an 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 on it based on its type.

Encapsulation just refers to packaging related stuff together : with classes. If someone hands us a class, we do not need to know how it actually works to use it; all we need to know about is its public methods/data 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 the car presents (the steering wheel) allows you to accomplish your goal.

Encapsulation just refers to packaging related stuff together : with classes. If someone hands us a class, we do not need to know how it actually works to use it; all we need to know about is its public methods/data 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 the car presents (the steering wheel) allows you to accomplish your goal.

Objects being boxes with buttons you can push, you can also think of the interface of a class as the set of buttons each instance of that class makes available. 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++ makes you specify public and private access specifiers: by default, it assumes that the things you define in a class are internal details which someone using your code should not have to worry about.

The practice of hiding away these details from client code is called data hiding, or making your class a black box. One way to think about what happens in an objectoriented program is that we define what objects exist and what each one knows, and then the objects send messages to each other (by calling each other s methods) to exchange information and tell each other what to do.

Inheritance: When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements a relationship between classes.

Inheritance example 1: (Shape-Rectangle): // Base class class Shape { public: void setwidth(int w) { width = w; } void setheight(int h) { height = h; } protected: int width; int height; }; // Derived class class Rectangle: public Shape { public: int getarea() { return (width * height); } }; int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); cout << "Total area: " << Rect.getArea() << endl; return 0; }

Inheritance example 2: (Vehicle-Car):

Inheritance example 2: (Animal-Cat-Dog): class Animal { public: Animal(char* name) : name(name) {} void Walk(void); private: char* name; }; class Dog : public Animal { public: Dog(char* name):animal(name){} void Bark(void); }; class Cat : public Animal { public: Cat(char* name):animal(name){} void Climb(void); };

Polymorphism: Polymorphism means many shapes. It refers to the ability of one object to have many types. If we have a function that expects a Vehicle object, we can safely pass it a Car object, because every Car is also a Vehicle. Likewise for references and pointers: anywhere you can use a Vehicle *, you can use a Car *. The idea of inheritance implements a relationship between classes. There is still a problem. Take the following example:

There is still a problem. Take the following example: (The -> notation on line 3 just dereferences and gets a member. ptr-> member is equivalent to (*ptr).member.). Because vptr is declared as a Vehicle *, this will call the Vehicle version of getdesc, even though the object pointed to is actually a Car. Usually we d want the program to select the correct function at runtime based on which kind of object is pointed to.

We can get this behavior by adding the keyword virtual before the method definition: Compiler does not statically link the code of the function but produce code that will decide what function must be called at runtime (also known as late (ή dynamic) binding). binding

Polymorphism example: struct Base { void foo(void) { printf( base );} }; struct Derived : public Base { void foo(void) {printf( derived );} }; Derived derived; Base base, *b = new Derived; base.foo(); //prints base derived.foo();//prints derived b->foo(); Problem!!! prints base but intended derived struct Base { virtual void foo(void) { printf( base ); } }; struct Derived : public Base{ void foo(void) { printf( derived ); } }; Derived derived; Base base, *b = new Derived; base.foo(); //prints base derived.foo();//prints derived b->foo(); Solve!!! Prints derived

Pure virtual συναρτήσεις λέγονται οι virtual συναρτήσεις που δεν έχουν υλοποίηση Δηλώνονται κανονικά ως virtual συναρτήσεις προσθέτοντας ένα =0 στο τέλος τους Δε μπορούμε να δημιουργήσουμε στιγμιότυπα από κλάσεις που έχουν έστω και μια pure virtual συνάρτηση Αυτές οι κλάσεις ονομάζονται abstract Χρησιμοποιούνται για να ορίσουν γενική λειτουργικότητα που θα υλοποιηθεί αργότερα από τα derived classes Αν ένα derived class δεν υλοποιεί pure virtual συναρτήσεις ενός base, τότε είναι και αυτό abstract Μπορούμε να έχουμε pointers και references σε abstract classes, όχι όμως αντικείμενα

Ενδέχεται να έχουμε δεσμεύσει δυναμικά μνήμη για πολυμορφικά αντικείμενα. Κατά τη διαγραφή τους με delete θέλουμε πάντα να κληθεί ο κατάλληλος (πιο derived) destructor. Για αυτό το λόγο ορίζουμε το destructor της base class virtual. Πάντα όταν σχεδιάζουμε μια κλάση που πιθανό να κληροδοτήσει σε κάποια άλλη ορίζουμε τον destructor της ως virtual. Στο επόμενο παράδειγμα, αν ο destructor της Shape δεν ήταν virtual, κατά το delete shape θα καλούνταν μόνο ο destructor της Base (Shape) και θα είχαμε memory leak αν είχαμε κάνει new ένα Object της Derived κλάσης Circle.

Στο παράδειγμα δίπλα, αν ο destructor της Base δεν ήταν virtual, κατά το delete b θα καλούνταν μόνο ο destructor της Base και θα είχαμε memory leak για το Object o της κλάσης Derived struct Base { Base(void) { } virtual ~Base(void) { foo(); } virtual void foo(void) { printf( base ); } }; struct Derived : public Base { Derived(void) { o = new Object; } ~Derived(void){ foo(); delete o;} void foo(void) { printf( derived ); } private: Object* o; }; Base* b = new Derived; delete b; //prints derived base Στο επόμενο παράδειγμα, αν ο destructor της Shape δεν ήταν virtual, κατά το delete shape θα καλούνταν μόνο ο destructor της Base (Shape) και θα είχαμε memory leak αν είχαμε κάνει new ένα Object της Circle.

#include <stdio.h> Virtual Destructor class Shape { public: virtual ~Shape(); virtual void draw() = 0; Pure virtual }; int main() { Shape *shape = new Circle; shape->draw(); delete shape; return 0; } class Circle : public Shape { public: ~Circle(); If you compile it and run it as: ($ g++ -Wall xxx.cpp -o xxx $./xxx Circle::draw circle destructor shape destructor virtual void draw(); simple Destructor }; Will shows: Circle::draw Shape::~Shape() { printf("shape destructor\n"); } circle destructor shape destructor // void Shape::draw() { // printf("shape::draw\n");} Circle::~Circle() { printf("circle destructor\n"); } void Circle::draw() { printf("circle::draw\n"); }

Verify your understanding of how the virtual keyword and method overriding work by performing a few experiments: Remove the virtual keyword from each location individually, recompiling and running each time to see how the output changes. Can you predict what will and will not work? Try making Shape::draw non-pure by removing = 0 from its declaration. Try changing shape (in main()) from a pointer to a stack-allocated variable.

Assignment 4

Implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setstrength (int), which sets the strength for the Tool.

Create 3 classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a constructor which will take in an int that is used to initialize the strength field. The constructor should also initialize the type field using 'r' for Rock, 'p' for Paper, and 's' for Scissors. Create a virtual function in class Tool, the Tool::play, to compare Rock paper and Scissors strength.

The strength field shouldn't change in the tool::play, which returns true if the original class wins in strength and false otherwise. Class game will be responsible for playing the game and will contain 2 tool * (pointers) one for player and one for PC as it is unknown which type class each one will choose in each round of the game.

Η Game::draw να καλεί τη Shape::draw κάθε σχήματος με παράμετρο την τοποθεσία που θα απεικονίζει το σχήμα. Η Game, θα μπορεί να γνωρίζει τις διαστάσεις του καμβά.

Paper ή square: η οποία ζωγραφίζει πάνω στον καμβά ένα τετράγωνο με περίμετρο από., που με πλευρά μήκους side και με με x, y το κέντρο του σχήματος που αντιστοιχεί σε σημείο πάνω στον κεντρικό άξονα του καμβά και απέχει τουλάχιστον x χαρακτήρες από το κέντρο του καμβά.

Rock ή circle: η οποία θα ζωγραφίζει πάνω στον καμβά ένα κύκλο με περίμετρο από αστεράκια (.), με x, y το κέντρο του σχήματος που αντιστοιχεί σε χαρακτήρα πάνω στον κεντρικό άξονα του καμβά και απέχει τουλάχιστον δέκα χαρακτήρες από το κέντρο του καμβά. Scissors ή ένα ζωγραφίζει πάνω γραμμές από.. χιαστί σχήμα: η στον καμβά δυο οποία θα χιαστί με

ΑΠΟΡΙΕΣ???