How

closed account (9G3v5Di1)
Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators. Display all the properties of the Dog object using the accessors.

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

using namespace std;

class LeggedMammal{
  protected:
    int leg;
    string fur, tail;
  
  public:
    void setLegs(string tails, string furs, int legs){
      leg = legs;
      fur = furs;
      tail = tails;
    }
};

class Dog : public LeggedMammal{
  private:
    string breed, registered;
    int size;

  public:
    void setAso(string breeds, string registereds, int sizes){
      breed = breeds;
      registered = registereds;
      size = sizes;
    }
    string getInfoo(){
      ostringstream s;
      s << "It is a " << breed << ", size " << size << " and was " << registered << ". It has " << leg << " legs. With " << fur << " fur and has " << tail;
      return s.str();
    }
};

int main(){
  LeggedMammal o1;
  o1.setLegs("dog", "a tail", 4);
  Dog o;
  o.setAso("Shitzu", "registered", 6);

  cout << o.getInfoo();

  _pause();
  return EXIT_SUCCESS;
}


My codes are crap, I know it. But that's my capability as a beginner. Don't mind if I didn't get what the problem wants me to do, but I just want to ask. Why the variables 'fur' and 'tail' don't print at all? And the legs were 2002676014



output:

It is a Shitzu, size 6 and was registered. It has 200676014 legs. With “blank” fur and has “blank”
Please enter any key to continue. .  .

Last edited on
You create a LeggedMammal named o1.
You set some details on that object.


Then, you create a completely different Dog object, named o.
On that object, you set the breed, the registration, and the size; and then you print out some information about that Dog object.
On that Dog object, you never set the fur. You never set the legs. So what did you expect them to be? How can it print out information that doesn't exist?

Are you expecting the Dog object o to magically contain information that you put into the completely different LeggedMammal object o1?


Here is your question in a simpler form:
1
2
3
int x = 5;
int y;
cout << y;


How come the output isn't 5?
Last edited on
closed account (9G3v5Di1)
I did some changes and it worked!

1
2
3
4
5
6
7
8
9
10
int main(){
  Dog o;
  o.setLegs("a tail", "dog", 4);
  o.setAso("Shitzu", "registered", 6);

  cout << o.getInfoo();

  _pause();
  return EXIT_SUCCESS;
}


so basically you mean that after inheriting the properties from the LeggedMammals, I could just set values of those from the Dog object.
so basically you mean that after inheriting the properties from the LeggedMammals, I could just set values of those from the Dog object.


That is true, but that's not what your misunderstanding was. Your misunderstanding was thinking that two completely different objects were in fact the same object, because one of them was of a type that inherited from the type of the other.
Topic archived. No new replies allowed.