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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:26:32+00:00 2026-05-22T00:26:32+00:00

I have a base class called Mijloc_transport and 2 derived classes called Masina and

  • 0

I have a base class called Mijloc_transport and 2 derived classes called Masina and Tramvai. I must do an menu type program that can create different kind of objects from the derived classes and show them. I’m using the UPCASTING method and when I press 1 the if(tasta=="1") creates an object and I can see what type it is using: tabp[0]->show();. It seems that when the program enters again in the do loop (using the main label), the object created as 1 is destroyed and I can’t see tabp[0]->show(); because the reference is deleted. What shall I do to keep the objects alive so I can put them in tabp?

Masina b(30);
p=&b;
tabp[i]=p;

so later I can display them….10x for your help

#include "include.hpp"
#include <ctype.h>
#include <cstdlib>

int main(void)
{    

    int i=0;

    Mijloc_transport *tabp[MAX];
    Mijloc_transport *p;

    int tasta;
main:
    do
    {
        //cout<<"\n\n\n\n\n";
        cout<<endl<<"apasa orice tasta pentru a reveni la meniu "<<endl; getch();
        system("cls");
        cout<<"Tasteaza numarul optiunii dorite din meniu:"<<endl; 
        cout<<"1. creaza o masina cu 30 cai putere"<<endl;      //create an Masina derived Oject
        cout<<"2. creaza un tramvai cu 20 de locuri"<<endl;//create an Tramvai derived Oject
        cout<<"3. afiseaza memoria"<<endl;                       //print the objects
        cout<<"4. iesire"<<endl;
        tasta=cin.get();
    }
    while(tasta!='1' && tasta!='2'&& tasta!='3'&& tasta!='4');


    if(tasta=='1')
    {
        Masina b(30);
        p=&b;
        tabp[i]=p;

        tabp[0]->show();

        cout<<endl<<"apasa orice tasta pentru a reveni la meniu "<<endl;
        getch();

        //i++;            //TREBUIE INCREMENTAT cand ai mai mult de un obiect
        goto main;
    }

    if(tasta=='3')
    {
        //afiseaza elementele din memorie
        //for(int j=0;j<=i;j++)                      //J<=i
        //tabp[j]->show();

        tabp[0]->show();
        cout<<endl<<"apasa orice tasta pentru a reveni la meniu "<<endl;
        getch();

        return 1;
    }
}
  • 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-05-22T00:26:33+00:00Added an answer on May 22, 2026 at 12:26 am

    Instead of:

    Masina b(30);
    p=&b;
    tabp[i]=p;
    

    do:

    tabp[i] = new Masina(30);
    

    and do not forget to do:

    for ( unsigned int i = 0; i < MAX; ++i )
    {
        delete tabp[i];
    }
    

    When you no longer need tabp (before existing main).

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

Sidebar

Related Questions

I have a base class called Items and 3 derived classes, and within the
I have a base class called Component which has a number of classes derived
Ok so I have an abstract base class called Product, a KitItem class that
I have a base-class called Element. Some other classes (like Label and Image) both
I have a base class called ModelBase and I have a derived class called
Basically I have a Base class called Program. I then have more specific program
Quirky question: Imagine that I have a base class called BaseFoo. I have an
I have an abstract base class called Shape from which both Circle and Rectangle
I have a base class that represents a database test in TestNG, and I
I have a base class that has a private static member: class Base {

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.