Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : Day1-live.cpp
- // Author :
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <math.h>
- using namespace std;
- /*
- * Definire classe Animale con nome e eta' (privati) con proprieta' accessorie (getter + setter).
- * Definire inoltre costruttore, costruttore copia, distruttore e to_string
- *
- * Definire classi Zebra + Delfino che ereditano da Animale.
- * Zebra aggiunge il numero di arti (come dato).
- * Delfino aggiunge il numero di pinne (come dato).
- *
- * Aggiungere a entrambe le classi una funzione sayHello che stampera' un log personalizzato
- * per ogni animale a partire dal to_string ereditato da Animale.
- *
- * N.B.: tutte le variabili dovranno essere private e accessibili solo attraverso le proprieta'. Se
- * possibile aggiungere qualche controllo di integrita' sui valori in ingresso sia per il
- * costruttore che per il richiamo delle proprieta' pure.
- *
- */
- class Animal {
- string name;
- int age;
- public:
- Animal(string, int);
- Animal(const Animal&);
- ~Animal();
- string getName();
- void setName(string);
- int getAge();
- void setAge(int);
- string to_string();
- };
- class Zebra : public Animal {
- int artCount;
- public:
- Zebra(string, int, int);
- Zebra(const Zebra&);
- ~Zebra();
- int getArtCount();
- void setArtCount(int);
- void say_hello();
- string to_string();
- };
- class Dolphin : public Animal {
- int fins;
- public:
- Dolphin(string, int, int);
- Dolphin(const Dolphin&);
- ~Dolphin();
- int getFins();
- void setFins(int);
- void say_hello();
- string to_string();
- };
- // DEF
- Animal::Animal(string name, int age) {
- setName(name);
- setAge(age);
- }
- Animal::Animal(const Animal& animal) {
- name = animal.name;
- age = animal.age;
- }
- Animal::~Animal() {
- cout << "killing animal: " << endl << to_string() << endl;
- }
- string Animal::getName() { return name; }
- void Animal::setName(string name) {
- if (name.length() > 3) { // controllo integrita'
- this -> name = name;
- }
- }
- int Animal::getAge() { return age; }
- void Animal::setAge(int age) {
- if (age > 0) { // controllo integrita'
- this -> age = age;
- }
- }
- string Animal::to_string() {
- return "Animal " + name + ": " + std::to_string(age);
- }
- // -------------------------------------------------------------------
- Zebra::Zebra(string name, int age, int artCount)
- : Animal(name, age) {
- setArtCount(artCount);
- }
- Zebra::Zebra(const Zebra& zebra)
- : Animal(zebra) {
- artCount = zebra.artCount;
- }
- Zebra::~Zebra() {
- cout << "killing zebra" << endl << to_string() << endl << endl;
- }
- int Zebra::getArtCount() { return artCount; }
- void Zebra::setArtCount(int artCount) {
- if (artCount > 0) { // vincolo integrita'
- this -> artCount = artCount;
- }
- }
- void Zebra::say_hello() {
- cout << "Hello from Zebra: " << endl << to_string() << endl << endl;
- }
- string Zebra::to_string() {
- return "Zebra: " + std::to_string(artCount) + "\n" + Animal::to_string();
- }
- // -------------------------------------------------------------------
- Dolphin::Dolphin(string name, int age, int fins)
- : Animal(name, age) {
- setFins(fins);
- }
- Dolphin::Dolphin(const Dolphin& dolphin)
- : Animal(dolphin) {
- fins = dolphin.fins;
- }
- Dolphin::~Dolphin() {
- cout << "killing dolphin" << endl << to_string() << endl << endl;
- }
- int Dolphin::getFins() { return fins; }
- void Dolphin::setFins(int fins) {
- if (fins > 0) // controllo integrita'
- this -> fins = fins;
- }
- void Dolphin::say_hello() {
- cout << "Hello from Dolphin: " << endl << to_string() << endl << endl;
- }
- string Dolphin::to_string() {
- return "Dolphin: " + std::to_string(fins) + "\n" + Animal::to_string();
- }
- int sum(int x, int* y, int &z) {
- return x+*y+z;
- }
- void sum(int x, int y, int* sum) {
- *sum = x+y;
- }
- void return_ODD(int *array_in, int *array_out) {
- for(int i=0, k=0; i<5; i++) {
- if (*(array_in+i)%2!=0) {
- *(array_out+k)=*(array_in+i);
- k++;
- }
- }
- }
- // -------------------------------------------------------------------
- int main() {
- Animal animal1("nome", 10),
- animal2("nome2", 20),
- animal3 = animal2;
- cout << "a1:" << endl << animal1.to_string() << endl << endl;
- cout << "a2:" << endl << animal2.to_string() << endl << endl;
- cout << "a3:" << endl << animal3.to_string() << endl << endl;
- //------------------------------------------------------------
- Zebra zebra1("zeb1", 10, 4),
- zebra2("zeb2", 20, 3),
- zebra3 = zebra2;
- cout << "z1: " << endl << zebra1.to_string() << endl << endl;
- cout << "z2: " << endl << zebra2.to_string() << endl << endl;
- cout << "z3: " << endl << zebra3.to_string() << endl << endl;
- zebra1.say_hello();
- zebra2.say_hello();
- zebra3.say_hello();
- //------------------------------------------------------------
- Dolphin dolphin1("dol1", 1, 3),
- dolphin2("dol2", 2, 4),
- dolphin3 = dolphin2;
- cout << "d1: " << endl << dolphin1.to_string() << endl << endl;
- cout << "d2: " << endl << dolphin2.to_string() << endl << endl;
- cout << "d3: " << endl << dolphin3.to_string() << endl << endl;
- dolphin1.say_hello();
- dolphin2.say_hello();
- dolphin3.say_hello();
- //------------------------------------------------------------
- //generare array di 5 elementi e stampare tutti gli elementi in ordine diretto e inverso
- //con cella di memoria attraverso un puntatore ad array
- int arr[5]={10,20,30,40,50};
- cout << "\nDIRECT ACCESS" << endl;
- for(int i=0; i<5; i++) {
- cout << "Array element " << i << " (" << &arr[i] << "): " << *(arr+i) << endl;
- }
- cout << "\nINVERSE ACCESS" << endl;
- for(int i=4; i>=0; i--) {
- cout << "Array element " << i << " (" << &arr[i] << "): " << *(arr+i) << endl;
- }
- //------------------------------------------------------------
- //completare le seguenti funzioni somma
- //
- //int sum(int x, int* y, int &z) { }
- //void sum(int x, int y, int* sum) { }
- //
- int x = 10;
- int y = 20;
- int z = 30;
- int z_sum;
- cout << "sum1: " << sum(x,&y,z) << endl;
- sum(x,y,&z_sum);
- cout << "sum2: " << z_sum << endl;
- //------------------------------------------------------------
- //stampare tutte le lettere dell'alfabeto sfruttando i puntatori
- char *letter = new char;
- *letter = 'a';
- for(*letter = 'a'; *letter<='z'; (*letter)++)
- cout << "letter: " << *letter << endl;
- //------------------------------------------------------------
- //generare un array di 10 elementi piu un intero. Cercare l'intero all'interno dell'array
- //e stampare la cella di memoria corrispondente (nell'array)
- int _array[10] = {0,10,20,30,40,50,60,70,80,90};
- int l=35;
- for(int i=0; i<10;i++) {
- if (*(_array+i)==l) {
- cout << "Array element of value " << *(_array+i) << " in pos " << i << " with memory cell " << &_array[i] << endl;
- return 0;
- }
- }
- cout << "Array element of value " << l << " doesn't exist in the selected array" << endl;
- //------------------------------------------------------------
- //generare due array da 5 elementi e confrontarli tra loro. Bloccare l'esecuzione nel caso i valori contenuti nei 2 array
- //non siano uguali. Navigare entrambi gli array con i puntatori
- int array1[5] = {0,10,20,30,40};
- int array2[5] = {0,10,20,30,40};
- for(int i=0; i<5;i++) {
- if (*(array1+i)!=*(array2+i)) {
- cout << "Arrays with different elements" << endl;
- return 0;
- }
- }
- cout << "Identical arrays" << endl;
- //------------------------------------------------------------
- //generare un array di 5 elementi, passarlo ad una funzione che restituisce i soli elementi dispari
- int array_in[5] = {0,1,7,3,4}, array_out[5] = {0};
- return_ODD(&array_in[0],&array_out[0]);
- cout << "Array_in elements: " << endl;
- for(int i=0; i<5;i++) {
- cout << *(array_in+i) << ", ";
- }
- cout << "\nArray_out elements: " << endl;
- for(int i=0; i<5;i++) {
- cout << *(array_out+i) << ", ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement