viewing paste Class Rabbit | C++

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include <iostream>
 
class Rabbit{
    enum Colour{BLACK,WHITE,BROWN,GRAY};
    Rabbit();
    Rabbit(int months, Colour fur);
    int getAge() const;
    friend bool operator!=(const Rabbit&, const Rabbit&);
    friend const Rabbit operator+(const Rabbit& r1, const Rabbit& r2);
    friend Rabbit* operator*(const Rabbit& r, int n);
    
private:
    int age;
    Colour colour;
};
 
Rabbit::Rabbit(){
    
    age = 0;
    colour = WHITE;
    
}
Rabbit::Rabbit(int months, Colour fur){
    if(months < 0){
        std::cout << "Error";
        exit(1);
    }
    age = months;
    colour = fur;
    
}
 
int Rabbit::getAge() const{
    return age;
}
bool operator !=(const Rabbit& r1, const Rabbit& r2){
    
    return((r1.colour != r2.colour) || (r1.age != r2.age));
    
}
 
const Rabbit operator+(const Rabbit& r1,const Rabbit& r2){
    
    if(r1.colour == r2.colour)
        return Rabbit(0,r1.colour);
    else
        return Rabbit(); //or return Rabbit(0,WHITE);
    
}
 
Rabbit* operator*(const Rabbit& r, int n){
    
    Rabbit* A = new Rabbit[n];
    for(int i = 0; i < n; i++){
        A[i].age = r.age;
        A[i].colour = r.colour;
    }
    
    return A;
    
}
 
int* everyOther(int* A, int& n){
    
    if(n == 0)
        return A; //or null or nullptr
    if(n == 1){
        delete[] A;
        n = 0;
        return NULL; //or nullptr
    }
    n = n/2;
    int * B = new int[n];
    for(int i = 0; i < n; i++){
        B[i] = A[2*i];
    }
    delete[] A;
    return B;
    
}
 
int main () {
    
    
    return 0;
}
Viewed 1544 times, submitted by Streusel.