Help creating a class based on tests it must pass

Basically what the title says, I'm new to classes but know the basics. However I have found a problem that requires me to create a class named "Sheep" based on some tests it must pass. How do I go about it?

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
  void assrt(bool e, const char* m) {
    if (!e) {
        std::cout << "Assertion failed : " << m << std::endl;
    }
}
void test1() {
    Sheep w{"white", 125, 23}; //color, weight, price per kg
    Sheep b{"black", 120, 25.5};
    assrt(0==strcmp("white", w.color()), "Wrong color");
    assrt(0==strcmp("black", b.color()), "Wrong color");
}

void test2() {
    Sheep w{"white", 125, 23}; //color, weight, price per kg
    Sheep b{"black", 120, 25.5};
    assrt(0==strcmp("white", w.color()), "Wrong color");
    assrt(1==w.id(), "Wrong id");
    assrt(0==strcmp("black", b.color()), "Wrong color");
    assrt(2==b.id(), "Wrong id");
}

void test3() {
    Sheep w{"white", 125, 23};
    {
        Sheep* ps = new Sheep{w};
        ps->setColor("black");
        assrt(0==strcmp("black", ps->color()), "Wrong color");
        assrt(2==ps->id(), "Wrong id");
        delete ps;
    }
    assrt(0==strcmp("white", w.color()), "Wrong color");
    assrt(1==w.id(), "Wrong id");
}


Thank you for reading!
Lines 17, 19, 32: You refer to a function named id() which is presumably a getter for such and attribute, but nowhere do you set such an attribute.

Lines 9,10,16,18,27,31: I recommend you use std::string to store color, Do not use a C-string. It makes your comparison easier:
 
assrt(w.color()) == "white", "Wrong color");


The declaration for your class is pretty obvious:
1
2
3
4
5
6
7
8
9
10
class Sheep
{  std::string  color;
    int weight;
    double price;
    int id;
public:
    // Constructor
    //  Getters
    //  Setters
};

Last edited on
Topic archived. No new replies allowed.