hello all
i don't know what is the problem with this code

when i choose a p for prof ........all the objects be prof objects

and when i choose s for student all objects be student objects

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

class person
{
protected:
    int id;
    string name;

public:

    person() : id(0), name("")
    {
    }

    virtual void GetData()
    {
        cout << "ID:";
        cin >> id;
        cout << "Name:";
        cin >> name;
    }

    virtual void ShowData()
    {
        cout << "ID:" << id << "\nName" << name;
    }
};

class prof : public person
{
protected:
    int students;
    int years;

public:

    prof()
    {
        person();
        students = 0;
        years = 0;
    }

    void GetData()
    {
        person::GetData();
        cout << "\nStudents No.:";
        cin >> students;
        cout << "\nYears No.:";
        cin >> years;
    }

    void ShowData()
    {
        person::ShowData();
        cout << "\nStudents No.:" << students << "\nYears No.:" << years;
    }

};

class student : public person
{
protected:
    int AVGGrade;
    int year;
public:

    student()
    {
        person();
        AVGGrade = 1;
        year = 1;
    }

    void GetData()
    {
        person::GetData();
        cout << "\nStudent Grade.:";
        cin >> AVGGrade;
        cout << "\nYear No.:";
        cin >> year;
    }

    void ShowData()
    {
        person::ShowData();
        cout << "\nStudent AVGG.:" << AVGGrade << "\nYear No.:" << year;
    }

};

int main(int argc, char* argv[])
{
    vector<person*> arr;
    person *ptr;
    char c;
    do
    {
        cin >> c;
        if (c == 'e')
            break;
        switch (c)
        {
        case 'p':
            ptr = new prof;
            arr.push_back(ptr);
            (arr.front())->GetData();
            break;
        case 's':
            ptr = new student;
            arr.push_back(ptr);
            (arr.front())->GetData();
        }

    }
    while (true);
    for (int i = 0; i < arr.size();i++)
        arr[i]->ShowData();

        return 0;
}

1) GetData() must be declare virtual in each class for that to work.

2) (arr.front())->GetData(); . Don't call the GetData() method like that because the front() item in the array will most likely not be the item that was most recently added. push_back() puts the item at the tail of the array, not at the front. An easier way to code it is to use the pointer that was allocated:

In the code below I changed ShowData() in the base class to be a pure virtual function which must be implemented by all the derived classes. That's how the last loop in main() is able to display the data regardless of what derived class is used.

class person
{
protected:
    int id;
    string name;

public:

    person() : id(0), name("")
    {
    }

    virtual void GetData()
    {
        cout << "ID:";
        cin >> id;
        cout << "Name:";
        cin >> name;
    }


    virtual void ShowData() = 0;
    void Show()
    {
        cout << "ID:" << id << "\nName" << name;
    }
};

class prof : public person
{
protected:
    int students;
    int years;

public:

    prof()
    {
        students = 0;
        years = 0;
    }

    virtual void GetData()
    {
        person::GetData();
        cout << "\nStudents No.:";
        cin >> students;
        cout << "\nYears No.:";
        cin >> years;
    }

    virtual void ShowData()
    {
        person::Show();
        cout << "\nStudents No.:" << students << "\nYears No.:" << years;
    }

};

class student : public person
{
protected:
    int AVGGrade;
    int year;
public:

    student()
    {
        AVGGrade = 1;
        year = 1;
    }

    virtual void GetData()
    {
        person::GetData();
        cout << "\nStudent Grade.:";
        cin >> AVGGrade;
        cout << "\nYear No.:";
        cin >> year;
    }

    virtual void ShowData()
    {
        person::Show();
        cout << "\nStudent AVGG.:" << AVGGrade << "\nYear No.:" << year;
    }

};

int main(int argc, char* argv[])
{
    vector<person*> arr;
    person *ptr;
    char c;
    do
    {
        cout << "Enter e, p, or s\n";
        cin >> c;
        if (c == 'e')
            break;
        switch (c)
        {
        case 'p':
            ptr = new prof;
            arr.push_back(ptr);
            dynamic_cast<prof*>(ptr)->GetData();

            break;
        case 's':
            ptr = new student;
            arr.push_back(ptr);
            dynamic_cast<student*>(ptr)->GetData();
            break;
        }

    }
    while (true);
    for (size_t i = 0; i < arr.size();i++)
        arr[i]->ShowData();

        return 0;
}

Other than that I don't know what you want.

thanks alot Ancient Dragon
but i dont understand why i must change getdata into virtual function in all classes

what i done is to change
arr.front()
to
arr.back()

and the program worked will

thanks alot for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.