Advertisement
Guest User

Day2_Cpp

a guest
Nov 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. //============================================================================
  2. // Name : Day1-live.cpp
  3. // Author :
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <math.h>
  11. using namespace std;
  12.  
  13.  
  14. /*
  15. * Definire classe Animale con nome e eta' (privati) con proprieta' accessorie (getter + setter).
  16. * Definire inoltre costruttore, costruttore copia, distruttore e to_string
  17. *
  18. * Definire classi Zebra + Delfino che ereditano da Animale.
  19. * Zebra aggiunge il numero di arti (come dato).
  20. * Delfino aggiunge il numero di pinne (come dato).
  21. *
  22. * Aggiungere a entrambe le classi una funzione sayHello che stampera' un log personalizzato
  23. * per ogni animale a partire dal to_string ereditato da Animale.
  24. *
  25. * N.B.: tutte le variabili dovranno essere private e accessibili solo attraverso le proprieta'. Se
  26. * possibile aggiungere qualche controllo di integrita' sui valori in ingresso sia per il
  27. * costruttore che per il richiamo delle proprieta' pure.
  28. *
  29. */
  30.  
  31. class Animal {
  32.  
  33. string name;
  34. int age;
  35.  
  36. public:
  37. Animal(string, int);
  38. Animal(const Animal&);
  39. ~Animal();
  40.  
  41. string getName();
  42. void setName(string);
  43.  
  44. int getAge();
  45. void setAge(int);
  46.  
  47. string to_string();
  48. };
  49.  
  50. class Zebra : public Animal {
  51.  
  52. int artCount;
  53.  
  54. public:
  55. Zebra(string, int, int);
  56. Zebra(const Zebra&);
  57. ~Zebra();
  58.  
  59. int getArtCount();
  60. void setArtCount(int);
  61.  
  62. void say_hello();
  63.  
  64. string to_string();
  65. };
  66. class Dolphin : public Animal {
  67.  
  68. int fins;
  69.  
  70. public:
  71. Dolphin(string, int, int);
  72. Dolphin(const Dolphin&);
  73. ~Dolphin();
  74.  
  75. int getFins();
  76. void setFins(int);
  77.  
  78. void say_hello();
  79.  
  80. string to_string();
  81. };
  82.  
  83. // DEF
  84.  
  85. Animal::Animal(string name, int age) {
  86.  
  87. setName(name);
  88. setAge(age);
  89. }
  90. Animal::Animal(const Animal& animal) {
  91.  
  92. name = animal.name;
  93. age = animal.age;
  94. }
  95. Animal::~Animal() {
  96.  
  97. cout << "killing animal: " << endl << to_string() << endl;
  98. }
  99.  
  100. string Animal::getName() { return name; }
  101. void Animal::setName(string name) {
  102.  
  103. if (name.length() > 3) { // controllo integrita'
  104. this -> name = name;
  105. }
  106. }
  107.  
  108. int Animal::getAge() { return age; }
  109. void Animal::setAge(int age) {
  110.  
  111. if (age > 0) { // controllo integrita'
  112. this -> age = age;
  113. }
  114. }
  115.  
  116. string Animal::to_string() {
  117.  
  118. return "Animal " + name + ": " + std::to_string(age);
  119. }
  120.  
  121. // -------------------------------------------------------------------
  122.  
  123. Zebra::Zebra(string name, int age, int artCount)
  124. : Animal(name, age) {
  125.  
  126. setArtCount(artCount);
  127. }
  128. Zebra::Zebra(const Zebra& zebra)
  129. : Animal(zebra) {
  130.  
  131. artCount = zebra.artCount;
  132. }
  133. Zebra::~Zebra() {
  134.  
  135. cout << "killing zebra" << endl << to_string() << endl << endl;
  136. }
  137.  
  138. int Zebra::getArtCount() { return artCount; }
  139. void Zebra::setArtCount(int artCount) {
  140.  
  141. if (artCount > 0) { // vincolo integrita'
  142.  
  143. this -> artCount = artCount;
  144. }
  145. }
  146.  
  147. void Zebra::say_hello() {
  148.  
  149. cout << "Hello from Zebra: " << endl << to_string() << endl << endl;
  150. }
  151.  
  152. string Zebra::to_string() {
  153.  
  154. return "Zebra: " + std::to_string(artCount) + "\n" + Animal::to_string();
  155. }
  156.  
  157. // -------------------------------------------------------------------
  158.  
  159. Dolphin::Dolphin(string name, int age, int fins)
  160. : Animal(name, age) {
  161.  
  162. setFins(fins);
  163. }
  164. Dolphin::Dolphin(const Dolphin& dolphin)
  165. : Animal(dolphin) {
  166.  
  167. fins = dolphin.fins;
  168. }
  169. Dolphin::~Dolphin() {
  170.  
  171. cout << "killing dolphin" << endl << to_string() << endl << endl;
  172. }
  173.  
  174. int Dolphin::getFins() { return fins; }
  175. void Dolphin::setFins(int fins) {
  176.  
  177. if (fins > 0) // controllo integrita'
  178. this -> fins = fins;
  179. }
  180.  
  181. void Dolphin::say_hello() {
  182.  
  183. cout << "Hello from Dolphin: " << endl << to_string() << endl << endl;
  184. }
  185.  
  186. string Dolphin::to_string() {
  187.  
  188. return "Dolphin: " + std::to_string(fins) + "\n" + Animal::to_string();
  189. }
  190.  
  191. int sum(int x, int* y, int &z) {
  192. return x+*y+z;
  193. }
  194. void sum(int x, int y, int* sum) {
  195. *sum = x+y;
  196. }
  197.  
  198. void return_ODD(int *array_in, int *array_out) {
  199. for(int i=0, k=0; i<5; i++) {
  200. if (*(array_in+i)%2!=0) {
  201. *(array_out+k)=*(array_in+i);
  202. k++;
  203. }
  204. }
  205. }
  206. // -------------------------------------------------------------------
  207.  
  208. int main() {
  209.  
  210. Animal animal1("nome", 10),
  211. animal2("nome2", 20),
  212. animal3 = animal2;
  213.  
  214. cout << "a1:" << endl << animal1.to_string() << endl << endl;
  215. cout << "a2:" << endl << animal2.to_string() << endl << endl;
  216. cout << "a3:" << endl << animal3.to_string() << endl << endl;
  217.  
  218. //------------------------------------------------------------
  219.  
  220. Zebra zebra1("zeb1", 10, 4),
  221. zebra2("zeb2", 20, 3),
  222. zebra3 = zebra2;
  223.  
  224. cout << "z1: " << endl << zebra1.to_string() << endl << endl;
  225. cout << "z2: " << endl << zebra2.to_string() << endl << endl;
  226. cout << "z3: " << endl << zebra3.to_string() << endl << endl;
  227.  
  228. zebra1.say_hello();
  229. zebra2.say_hello();
  230. zebra3.say_hello();
  231.  
  232. //------------------------------------------------------------
  233.  
  234. Dolphin dolphin1("dol1", 1, 3),
  235. dolphin2("dol2", 2, 4),
  236. dolphin3 = dolphin2;
  237.  
  238. cout << "d1: " << endl << dolphin1.to_string() << endl << endl;
  239. cout << "d2: " << endl << dolphin2.to_string() << endl << endl;
  240. cout << "d3: " << endl << dolphin3.to_string() << endl << endl;
  241.  
  242. dolphin1.say_hello();
  243. dolphin2.say_hello();
  244. dolphin3.say_hello();
  245.  
  246. //------------------------------------------------------------
  247.  
  248. //generare array di 5 elementi e stampare tutti gli elementi in ordine diretto e inverso
  249. //con cella di memoria attraverso un puntatore ad array
  250.  
  251. int arr[5]={10,20,30,40,50};
  252.  
  253. cout << "\nDIRECT ACCESS" << endl;
  254. for(int i=0; i<5; i++) {
  255. cout << "Array element " << i << " (" << &arr[i] << "): " << *(arr+i) << endl;
  256. }
  257.  
  258. cout << "\nINVERSE ACCESS" << endl;
  259. for(int i=4; i>=0; i--) {
  260. cout << "Array element " << i << " (" << &arr[i] << "): " << *(arr+i) << endl;
  261. }
  262.  
  263. //------------------------------------------------------------
  264.  
  265. //completare le seguenti funzioni somma
  266. //
  267. //int sum(int x, int* y, int &z) { }
  268. //void sum(int x, int y, int* sum) { }
  269. //
  270.  
  271. int x = 10;
  272. int y = 20;
  273. int z = 30;
  274. int z_sum;
  275.  
  276. cout << "sum1: " << sum(x,&y,z) << endl;
  277.  
  278. sum(x,y,&z_sum);
  279.  
  280. cout << "sum2: " << z_sum << endl;
  281.  
  282. //------------------------------------------------------------
  283.  
  284. //stampare tutte le lettere dell'alfabeto sfruttando i puntatori
  285.  
  286. char *letter = new char;
  287.  
  288. *letter = 'a';
  289.  
  290. for(*letter = 'a'; *letter<='z'; (*letter)++)
  291. cout << "letter: " << *letter << endl;
  292.  
  293. //------------------------------------------------------------
  294.  
  295. //generare un array di 10 elementi piu un intero. Cercare l'intero all'interno dell'array
  296. //e stampare la cella di memoria corrispondente (nell'array)
  297.  
  298. int _array[10] = {0,10,20,30,40,50,60,70,80,90};
  299. int l=35;
  300.  
  301. for(int i=0; i<10;i++) {
  302. if (*(_array+i)==l) {
  303. cout << "Array element of value " << *(_array+i) << " in pos " << i << " with memory cell " << &_array[i] << endl;
  304. return 0;
  305. }
  306. }
  307. cout << "Array element of value " << l << " doesn't exist in the selected array" << endl;
  308.  
  309. //------------------------------------------------------------
  310.  
  311. //generare due array da 5 elementi e confrontarli tra loro. Bloccare l'esecuzione nel caso i valori contenuti nei 2 array
  312. //non siano uguali. Navigare entrambi gli array con i puntatori
  313.  
  314. int array1[5] = {0,10,20,30,40};
  315. int array2[5] = {0,10,20,30,40};
  316.  
  317. for(int i=0; i<5;i++) {
  318. if (*(array1+i)!=*(array2+i)) {
  319. cout << "Arrays with different elements" << endl;
  320. return 0;
  321. }
  322. }
  323. cout << "Identical arrays" << endl;
  324.  
  325. //------------------------------------------------------------
  326.  
  327. //generare un array di 5 elementi, passarlo ad una funzione che restituisce i soli elementi dispari
  328.  
  329. int array_in[5] = {0,1,7,3,4}, array_out[5] = {0};
  330.  
  331. return_ODD(&array_in[0],&array_out[0]);
  332.  
  333. cout << "Array_in elements: " << endl;
  334. for(int i=0; i<5;i++) {
  335. cout << *(array_in+i) << ", ";
  336. }
  337.  
  338. cout << "\nArray_out elements: " << endl;
  339. for(int i=0; i<5;i++) {
  340. cout << *(array_out+i) << ", ";
  341. }
  342.  
  343. cout << endl;
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement