Understanding the problem

closed account (9G3v5Di1)
Write a class that will represent a LeggedMammal. Consider the number of legs, kind of fur, presence of tail.

What should I do with this? Am I correct?

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
#include <string>
#include <sstream>
#include <iostream>
#include "_pause.h"

using namespace std;

class LeggedMammal{
  private:
    int leg;
    string fur;
    string tail;
  
  public:
    LeggedMammal (int legs, string furs, string tails){
      leg = legs;
      fur = furs;
      tail = tails;
    }
    string getInfo(){
      ostringstream s;
      s << "It has " << leg << " legs. With " << fur << " and has " << tail;
      return s.str();
    }
};
I'd say tail would be better as a bool, rather than a string.
I'd make fur an enum rather than a string. You might also add a name.
closed account (9G3v5Di1)
how do I do these?
closed account (9G3v5Di1)
should I do enums in public or private
closed account (9G3v5Di1)
I'm so confused. Please give me codes using enums and change the tail data type to bool. This will be my first time to use these two functions please help. I am about to do a program using these codes that I have made but as Repeater and dhayden said,

I'd make fur an enum rather than a string. You might also add a name.

I'd say tail would be better as a bool, rather than a string.

please make those codes for me, I asure you I will study about it. And please include a brief explanation through comments. It would be highly appreciated.
https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
You do already have:
1
2
    int leg;
    string fur;

The int is a type.
The string is a type.

Why should we "write codes", like one that has bool as the type of tail? That is part of your learning.


Public/private:
Who needs access to the definition?
1
2
int main() {
  LeggedMammal swan( 7, LeggedMammal::rabbit, false );

If the main() could/should use LeggedMammal::rabbit, then the main() needs access and the enum must be public.
closed account (9G3v5Di1)
so both the furs and strings will be printed as integers?
Do you mean furs and tails? If so then no. Should you make enum private? I would say yes.
closed account (9G3v5Di1)
how do I use enums and bool? Please write a code for me. I haven't used it once this will be my first time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class LeggedMammal{
  private:
    int leg;
    bool tail;
    enum fur{
      Beaver, Faux, Fisher, Fox, Golden jackal, Marten,
      Mink, Nutria, Otter, Rabbit, Raccoon, Sable,
      Skunk, Grey wolf, Australian brushtail possum
    }
  
  public:
    LeggedMammal (int legs, enum furs, bool tails){
      leg = legs;
      fur = furs;
      tail = tails;
    }
    string getInfo(){
      ostringstream s;
      s << "It has " << leg << " legs. With " << fur << " and has " << tail;
      return s.str();
    }
};
Last edited on
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
#include <iostream>
#include <string>

struct mammal {

    enum hair_type { NONE, CURLY, SHORT, LONG } ;

    explicit mammal( std::string name, int nlegs = 4,
                     hair_type hair = NONE, bool tailed = false )
               : its_name(name), num_legs(nlegs), type_of_hair(hair), has_tail(tailed)
    { if( num_legs < 0 ) num_legs = 0 ; }

    std::string name() const { return its_name ; }
    int number_of_legs() const { return num_legs ; }
    hair_type hair() const { return type_of_hair ; }
    bool is_tailed() const { return has_tail ; }

    std::string to_string() const {

        static const std::string hair_type_str[] = { "none", "curly", "short", "long" } ;

        return "mammal{ name:" + its_name + ", legs:" + std::to_string(num_legs) +
               ", hair:" + hair_type_str[type_of_hair] +
               ", tailed:" + ( has_tail ? "yes" : "no" ) + " }" ;
    }

    private:

        std::string its_name ;
        int num_legs = 4 ;
        hair_type type_of_hair = NONE ;
        bool has_tail = false ;

    friend std::ostream& operator<< ( std::ostream& stm, const mammal& the_mammal ) {
        return stm << the_mammal.to_string() ;
    }
};

int main() {

    const mammal ibex( "ibex", 4, mammal::SHORT, true ) ;
    std::cout << ibex << '\n' ;
}

http://coliru.stacked-crooked.com/a/94655d9555189fb6
Topic archived. No new replies allowed.