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

  • Home
  • SEARCH
  • 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 8742849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:34:18+00:00 2026-06-13T11:34:18+00:00

I need to create a vector that can contain my parent class and sub

  • 0

I need to create a vector that can contain my parent class and sub class data.

This is what i do..

Vehicle is the parent class

Car is the child class

About Car.cpp , it got the following

struct Point
{
    int x,y
};

class Car : public Vehicle
{
private:
    Point wheelPoint[4];
    double area;

public:
    void setPoint();
};

void Car::setPoint()
{
    int xData,yData;

    cout << "Please enter X:";
    cin >> xData;

    cout << "Please enter Y:";
    cin >> yData;

    wheelPoint[i].x = xData;
    wheelPoint[i].y = yData;
}

Then at my main.cpp

At my main.cpp

vector<VehicleTwoD> list;
VehicleTwoD *vehicle;
Car *car = new Car;
string vehicleName;

cout << "Please input name of vehicle";
cin >> vehicleName;

vehicle = new Car;
car->setPoint();

list.push_back( Vehicle() );
list.back().setName(vehicleName);

Here the issues.. how i put my wheelPoint of car into this vector also.

What i want to achieve is a vector that can contain

Vehicle Name: Vehicle Name (private variable at Vehicle - Parent Class)
Wheel Point[0]: Point (X,Y) ( private var at Car - Child Class)
Wheel Point[1]: Point (X,Y) ( private var at Car - Child Class)
Wheel Point[2]: Point (X,Y) ( private var at Car - Child Class)
Wheel Point[3]: Point (X,Y) ( private var at Car - Child Class)
  • 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-13T11:34:19+00:00Added an answer on June 13, 2026 at 11:34 am

    Containers of objects suffer from object slicing. You’ll need a vector of pointers (preferably smart):

    vector<std::unique_ptr<Vechicle>> vehicleVector;
    

    to which you can do:

    vehicleVector.push_back(new Vehicle);
    vehicleVector.push_back(new Car);
    

    Having a vector of objects will cut off all type information that is beyond Vechicle – so, a Car will be turned into a Vehicle, losing all additional information.

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

Sidebar

Related Questions

i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I need to create a method to generate a unit vector in three dimensions
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
I need to create a function which appends a value to a vector and
I noticed that I'm often in the need of a container class. For example
I'm attempting to create a type-agnostic vector in C++ that is distinguished by two
How can I use apply or a related function to create a new data
I need a way to create a string of n chars. In this case
I have an assignment in which I need to create a function that tells

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.