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 8768673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:07:40+00:00 2026-06-13T17:07:40+00:00

I got this example Parent class Vehicle Child Classes Car , Motorcycle , &

  • 0

I got this example

Parent class Vehicle

Child Classes Car, Motorcycle, & Lorry

This is what happens: In main.cpp I create

VehicleTwoD *vehicletwod[100];
Car *myCar = new Car();
Motorcycle *myMotorcycle = new motorCycle();
Lorry *myLorry = new Lorry();

This is what I do:

if(selection=="Car")
{
   vehicletwod[arrayCounter] = myCar;
   vehicletwod[arrayCounter]->setName(theName);
   vehicletwod[arrayCounter]->setYear(theYear);
}

if(selection=="Lorry")
{
   vehicletwod[arrayCounter] = myLorry;
   vehicletwod[arrayCounter]->setName(theName);
   vehicletwod[arrayCounter]->setYear(theYear);
}

if(selection=="Motorcycle")
{
   vehicletwod[arrayCounter] = myMotorcycle ;
   vehicletwod[arrayCounter]->setName(theName);
   vehicletwod[arrayCounter]->setYear(theYear);
}

cout << "Record successfully stored. Going back to the main menu " << endl;

The issue here in main.cpp is some kind of switch-case menu with a prompt, so if the user chooses to insert a new vehicle, he selects the vehicle type, and will manually input some value like theName and theYear. Then it will be set to the vehicletwod[arrayCounter].

The program runs into an issue when there is more than 1 object of the same child type in the list of vehicletwod.

If the user does something like

Car
Motorcycle
Car

The 1st car value will be overwritten by the latest Car (the 2nd car)

However, if they input

Car 
Motorcycle
Lorry

Itis fine because each object only runs once.

How do I change my declaration so it will not overwrite the data of the previous same 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-13T17:07:42+00:00Added an answer on June 13, 2026 at 5:07 pm

    You need to create a new Car , Motorcycle and Lorry instance for each new entry, since now you reuse the exiting instances and in that way rewrite data. You should do:

    if(selection=="Car")
    {
       vehicletwod[arrayCounter] = new Car();
       vehicletwod[arrayCounter]->setName(theName);
       vehicletwod[arrayCounter]->setYear(theYear);
    }
    
    if(selection=="Lorry")
    {
       vehicletwod[arrayCounter] = new Lorry();
       vehicletwod[arrayCounter]->setName(theName);
       vehicletwod[arrayCounter]->setYear(theYear);
    }
    
    if(selection=="Motorcycle")
    {
       vehicletwod[arrayCounter] = new Motorcycle();
       vehicletwod[arrayCounter]->setName(theName);
       vehicletwod[arrayCounter]->setYear(theYear);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

thank you for looking i got this example from my book but i cant
GridView CurrentGrid =Grd_UserList; Grd_UserList.Columns.Clear(); ı have got this example, ı use Columns.Clear(); fonc. with
For example I got this 2 table dbo.fc_states StateId Name 6316 Alberta 6317 British
I've placed panel from this example http://docs.sencha.com/ext-js/4-0/#!/example/dd/dnd_grid_to_grid.html to Ext.window.Window and got an issue: proxy
I was trying to run a request factory example, but, I got this error
I got the example in this link . I can't understand the addmove ,
For example, i got a method like this: - (void)addToRecents:(NSDictionary *)photo { NSUserDefaults *defaults
I have this code that uses a template parent class A that has one
I have got to this so far using the jQuery docs $('[class^=layout] > (*)').click(function(e)
Can someone give me an example of how a child class would access the

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.