812336A C++ -kielen perusteet, 21.8.2010 1. Vastaa lyhyesti seuraaviin kysymyksiin (1p kaikista): a) Mitä tarkoittaa funktion ylikuormittaminen (overloading)? b) Mitä tarkoittaa jäsenfunktion ylimääritys (overriding)? c) Mikä on oletusmuodostin (oletuskonstruktori)? d) Mikä on kopiomuodostin (kopiokonstruktori)? e) Mitä tarkoittaa monimuotoisuus (polymorfismi)? f) Mainitse jokin ongelmatilanne, joka voi aiheutua moniperinnän käytöstä. 2. Laadi Asunto- luokka, jolla on attribuutteina asunnon pinta-ala neliömetreinä (int; private) ja hinta euroina (double; private). Luokkaan tulee toteuttaa attribuuttien asettamiseen tarvittavat metodit ja niiden arvot palauttavat metodit. Lisäksi on toteutettava muodostin (konstruktori), jonka parametreina ovat asunnon pinta-ala ja hinta. Edelleen tuhoaja (destruktori) ja kopiomuodostin sekä asunnon neliöhinnan palauttava julkinen metodi on kirjoitettava. 3. Oletetaan, että luokka A perii luokan B. Mitkä seuraavista väittämistä ovat oikeita ja mitkä vääriä? (3p) a) Kun luokan A olio luodaan, kutsutaan ensin luokan A konstruktoria ja tämän jälkeen luokan B konstruktoria. b) Kun luokan A olio luodaan, kutsutaan ensin luokan B konstruktoria ja tämän jälkeen luokan A konstruktoria. c) Kun luokan A olio luodaan, kutsutaan luokan A konstruktoria. Luokan B konstruktoria ei kutsuta. d) Kun luokan A olio tuhotaan, kutsutaan ensin luokan A destruktoria ja tämän jälkeen luokan B destruktoria. e) Kun luokan A olio tuhotaan, kutsutaan ensin luokan B destruktoria ja tämän jälkeen luokan A destruktoria. f) Kun luokan A olio tuhotaan, kutsutaan luokan A destruktoria. Luokan B destruktoria ei kutsuta. Kirjoita ohjelma, jonka tulostuksesta voidaan päätellä väitteiden todenmukaisuus. Kuvaa myös, miten ohjelman tulostus ilmaisee ko. asian. (3p) Jatkuu toisella puolella -> 4. a) Mitä tarkoitetaan geneerisellä ohjelmoinnilla? Kirjoita geneerinen funktio vaihda, jonka avulla voi vaihtaa minkä tahansa kahden perustietotyypin muuttujan
arvot keskenään. (3p) b) Mitä seuraava ohjelma tulostaa? Perustele vastauksesi! (3p) class BaseClass{ std::cout << "Announcement from class BaseClass\n"; ; class SubClass: public BaseClass{ std::cout << "Announcement from class SubClass\n"; ; void printannouncement(baseclass bcobj){ bcobj.announce(); void printannouncementref(baseclass &bcobref){ bcobref.announce(); SubClass sc; printannouncement(sc); printannouncementref(sc); Jatkuu toisella ppaperilla ->
5. a) Mikä on funktio-olio? (2p) b) Kuvaa tarkasti seuraavan ohjelman toiminta (4p): #include <vector> typedef std::vector<double> dubvec; typedef dubvec::iterator dubveciter; class Op{ private: double res; int n; Op() : res(0),n(0) { ; void operator()(double elem) { res += elem; n++; double getvalue(){ return (res/n); double findvalue(dubveciter from, dubveciter to){ Op o; dubveciter iter; for(iter=from; iter!=to;iter++){ o(*iter); return o.getvalue(); dubvec numbers; numbers.push_back(2.1); numbers.push_back(3.2); numbers.push_back(4.0); std::cout << findvalue(numbers.begin(),numbers.end()) << std::endl; Tentin tulokset julkistetaan viimeistään 15.9.2010.
812336A Basics of C++ programming language (C++ -kielen perusteet), 21.8.2010 1. Answer briefly following questions (1p each): a) What does overloading a function mean? b) What does overriding a member function mean? c) What is a default constructor? d) What is a copy constructor? e) What does polymorphism mean? f) Mention a problem that can occur, when using multiple inheritance. 2. Write an Apartment class, having as its attributes the apartment s area in square meters (int; private) and its price (double; private). You should implement methods to get and set the attributes in the class. Moreover, write a constructor with the apartment s area and price as its parameters. Furthermore, copy constructor, destructor and a public method to return the price per square meter should be written. 3. Assume that class A inherits class B. Which of the following claims are right and which are wrong? (3p) a) When an object of class A is created, the constructor of class A is first called and then the constructor of class B is called. b) When an object of class A is created, the constructor of class B is first called and then the constructor of class A is called. c) When an object of class A is created, the constructor of class A is called. The constructor of class B is not called. d) When an object of class A is destroyed, the destructor of class A is first called and then the destructor of class B is called. e) When an object of class A is destroyed, the destructor of class B is first called and then the destructor of class A is called. f) When an object of class A is destroyed, the destructor of class A is called. The destructor of class B is not called. Write a program, from whose output one can verify, which claims are correct. Also explain how the output indicates this. (3p) Continued on the other side ->
4. a) What does generic programming mean? Write a generic function swap that exchanges values of two variables. The variables can be any basic data type. (3p) b) What is the output of the following program? Justify your answer! (3p) class BaseClass{ std::cout << "Announcement from class BaseClass\n"; ; class SubClass: public BaseClass{ std::cout << "Announcement from class SubClass\n"; ; void printannouncement(baseclass bcobj){ bcobj.announce(); void printannouncementref(baseclass &bcobref){ bcobref.announce(); SubClass sc; printannouncement(sc); printannouncementref(sc); Continued on the other paper ->
5. a) What is a function object? (2p) b) Describe in detail the operation of the following program (4p): #include <vector> typedef std::vector<double> dubvec; typedef dubvec::iterator dubveciter; class Op{ private: double res; int n; Op() : res(0),n(0) { ; void operator()(double elem) { res += elem; n++; double getvalue(){ return (res/n); double findvalue(dubveciter from, dubveciter to){ Op o; dubveciter iter; for(iter=from; iter!=to;iter++){ o(*iter); return o.getvalue(); dubvec numbers; numbers.push_back(2.1); numbers.push_back(3.2); numbers.push_back(4.0); std::cout << findvalue(numbers.begin(),numbers.end()) << std::endl; The results will be available not later than 15.9.2010.