-1

How we can create a linked list which can contain a different class of object. for example, if a person is base class and it has student and teacher as a derived class so How I can create a linked list which can contain both classes of objects like student also and teacher also.

LinkedList example for understanding my problem:
head->studentobject->teacherobject->studentobject->teacherobject... so on

Arthur Tacca
  • 8,833
  • 2
  • 31
  • 49
  • 3
    The data in the list nodes should be a pointer to a base class object. – Barmar Feb 04 '20 at 13:14
  • 4
    You had the "C" tag on this question, which I removed because this question is obviously not about C, which does not have the notion of "base class" and "derived class" (note that C is a different language to C++). – Arthur Tacca Feb 04 '20 at 13:17

2 Answers2

2

Try it here. A std::list is a linked list . .

#include <list>
#include <iostream>
#include <memory>
using namespace std;

class Person {
public:
    virtual ~Person() = default;
    virtual void talk()=0;
};

class Student : public Person {
public:
    void talk() {
        cout << "Teach me!\n";
    }
};

class Teacher : public Person {
    public:
    void talk() {
        cout << "Listen!\n";
    }
};

int main() {
    list<std::unique_ptr<Person>> people;
    people.push_back(unique_ptr<Person>(new Student()));
    people.push_back(unique_ptr<Person>(new Teacher()));

    for (auto& p : people) {
        p->talk();
    }

    return 0;
}
learnvst
  • 15,455
  • 16
  • 74
  • 121
  • Hey, thank you so much for the answer. Is there any other way because I am implementing that linked list from scratch So how I can declare structure for this which contains the member which references to derived objects and next link? – sahil makandar Feb 05 '20 at 04:55
  • @sahilmakandar Don't bundle all the functionality into a single concept. Try emulate your own implementation of std::list using templates, and then use the logic like in my answer. Something like this https://www.softwaretestinghelp.com/doubly-linked-list/, maybe where you make the node a template. – learnvst Feb 06 '20 at 11:26
  • .... I mean I don't know why they are teaching use of raw pointers in 2019 in that link, but you get the gist. – learnvst Feb 06 '20 at 11:31
1

Usually you would create a base class and derived classes:

#include <iostream>
#include <list>
#include <memory>

class Base {
public:
    virtual ~Base() = default;

    virtual void printClass() = 0;
};

class Student : public Base {
public:
    virtual void printClass() { std::cout << "Student\n"; }
};

class Teacher : public Base {
public:
    virtual void printClass() { std::cout << "Teacher\n"; }
};

class Node {
public:
    void setTeacher() {
        data = std::make_unique<Teacher>();
    }
    void setStudent() {
        data = std::make_unique<Student>();
    }

    void printClass() { data->printClass(); }

private:
    std::unique_ptr<Base> data;
};

int main() {
    std::list<Node> l;
    l.push_back({});
    l.front().setTeacher();
    l.front().printClass();
}

Use some kind of pointer to store the references. You can use raw pointers, references, smart pointers, ...

Here you can read when to use virtual destructors

  • Hey, thank you so much for the answer. Is there any other way because I am implementing that linked list from scratch So how I can declare structure for this which contains the member which references to derived objects and next link? – sahil makandar Feb 05 '20 at 05:10