I have implemented the following logic: a base class, Animal, has a vector, friends. A derived class, Dog, then inherits from Animal.I then want to implement a set_friends() function that can be passed a Dog object and then fill its friends vector. I then want to be able to call print_friends() from the base class to print out the friends that were set in set_friends().When executed it produces the desired effect. However, I am curious if people know of any cleaner way to achieve the same end.#include <iostream>#include <vector>using namespace std;class Animal{    private:        int height;        int weight;        string name;    public:        int getHeight(){ return height; }        int getWeight(){ return weight; }        string getName(){ return name; }        vector<Animal> *friends;        void print_friends() {            for(Animal a : *friends) {                cout << a.getName() << endl;            }        };         Animal(int height, int weight, string name) :        height(height), weight(weight), name(name) {};};class Dog : public Animal {    private:        string sound = woof;    public:        void getSound() { cout << sound << endl; }        Dog(int h, int w, string n): Animal(h, w, n) {};};void set_friends(Dog& d) {    Animal b(1, 2, bb);    vector<Animal> d_friends;    d_friends.push_back(b);    d.friends = &d_friends;}int main(){    Dog d(1, 2, doggy);    set_friends(d);    d.print_friends();    return 0;}
OOP exercise involving Animal subclasses
c++;beginner;object oriented
So there are some things to be improved.Do not use namespace::std; Google it or just drop it.Add all the necessary headers. You are missing string #include <string>If you have to implement getter functions declare them const.int getHeight() const { return height; }I dont really get why your animal class has no sound member but your dog does.As @fernando.reyes said, your setFriends method is buggy. this should be a member function of you animal class that takes a animal and adds it to the friends. If friends are bidirectional you can add a wrapper that takes two animalsvoid makeFriends(Animal& oneFriend, Animal& otherFriend) {    oneFriend.addFriend(otherFriend);          otherFriend.addFriend(oneFriend);}void Animal::addFriend(const Animal& newFriend) {    friends.push_back(&newFriend);}Dog d1(1, 2, doggy);Dog d2(1, 2, lassy);makeFriends(d1, d2);I think your friends vector is wrong. You should have std::vector<Animal*> rather than std::vector<Animal> *. Otherwise you get problems with ownership.I would suggest to use auto &elem for range based loops. In any case you should at least use Animal* a : friends rather than passing by copyvoid print_friends() {    for(auto &animal : friends) {        std::cout << animal->getName() << endl;    }};