Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:20:16+00:00 2026-06-17T12:20:16+00:00

Bear with me. There are 3 classes. Person is the base class with name

  • 0

Bear with me. There are 3 classes. Person is the base class with name and age. Child is a derived class with a grade in school. Parent is another derived class who can have a child (yes or no)

Before we continue there are a couple of things I must point out:
This is an exercise which I thought up so I can practise inheritance a bit. The idea is to end up with a vector which contains pointers from the base class to the derived classes objects.

The “program” depends on the user entering correct values, has no error checking and so on, but that is not the point of this exercise, so that is why I haven’t done anything about it.

Feedback on how to fix the problems I am getting is greatly appreciated. Thanks in advance.

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

class Person
{
private:
    string m_name;
    int m_age;
public:
    Person(string name, int age)
    {
        m_name = name;
        m_age = age;
    }
    string get_name()
    {
        return m_name;
    }
    virtual void info() =0;
};

class Child : public Person
{
private:
    int m_grade;
public:
    Child(string name, int age, int grade) : Person(name, age)
    {
        m_grade = grade;
    }
    void info()
    {
        cout <<"I am a child. I go to the " << m_grade << " grade."<<endl;
    }
};

class Parent : public Person
{
private:
    bool m_child;
public:
    Parent(string name, int age, bool child) : Person(name, age)
    {
        m_child = child;
    }
    void info()
    {
        if(m_child == true)
        {
            cout << "I have a child." << endl;
        }
        else
        {
            cout << "I do not have a child" << endl;
        }
    }
};

vector create_list(const int& x)
{
    vector <Person> a;
    for(int a = 0; a < x; a++)
    {
        cout << "enter the name" << endl;
        string o;
        cin >> o;
        cout << "enter the age" << endl;
        int age;
        cin >> age;
        cout << "What would you like your person to be: a Child or a Parent?" << endl;
        string choice;
        cin >> choice;
        if(choice == "Child")
        {
            cout << "enter it's grade" << endl;
            int grade;
            cin >> grade;
            Child* c  = new Child(o, age, grade);
            a.push_back(c);
        }
        else
        {
            cout <<"enter if the parent has a child (yes/no)" << endl;
            string wc;
            cin >> wc;
            if(wc == "yes")
            {
                Parent* p = new Parent(o, age, true);
                  a.push_back(p);
            }
            else
            {
                Parent* p = new Parent(o, age, false);
                  a.push_back(p);
            }
        }
    }
    return a;
}

int main()
{
    cout << "How many people would you like to create?" << endl;
    int x;
    cin >> x;
     vector<Person> a = create_list(x);
     a[0]->getname();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:20:16+00:00Added an answer on June 17, 2026 at 12:20 pm
    1. You are using the same variable name a for both vector<Person> and int in your for loop. So when you get to the line a.push_back(c); the program will think that a is an integer, not a vector.

      Make your variable names unique.

    2. As others have mentioned, your container is a vector of type Person, but you instantiate new derived classes of type Child * and Parent *, so your vector should be of type Person*.

    3. On that same note, the return type of your function should read vector<Person*>

    4. Although it is not necessary in this case since your application ends immediately, it is good practice to make sure every call to new corresponds to a call to delete. In that case you would write a free_list method which goes through and deletes each Person object pointed to in the list. Note that the vector itself requires no cleanup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's all. Didn't find any similar topic so bear with me it there is.
There are too many types of data streaming classes in the Java API. I
Note to readers : Bear with me. I promise there's a question. I have
I use Castle Active Record' ActiveRecordBase class as a base class for my db
Bear with my rudimentary question. http://jsbin.com/css-play-notfound/6 There is this blue underline thats annoyingly creeping
I’m trying to do the following: class Animal { class Bear : public Animal
(There's a tiny bit of history to this question, please bear with me) In
I know that there are better ways to check for an empty string—bear with
I'm sure there is an answer present for this question but bear with me
(bear with me, there's a lot of explanation before my question) As a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.