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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:27:24+00:00 2026-05-16T23:27:24+00:00

My program compiles fine, but gets core dumped when an overloaded function is called.

  • 0

My program compiles fine, but gets core dumped when an overloaded function is called. Below, is the output of the program:

FoodID supplied=1
FoodBase constructor called
In K constructor
In Omlette constructor
Omlette handler constructor called
In prepare() Omlette Handler
Mix called
K Prepare called
K Egg called
DonePreparing called
In egg() Omlette Handler
Segmentation fault (core dumped)

I’m trying to overload the pure virtual function named “egg”. The egg function needs to be of two types: One that takes no variables and the other that takes an integer. The one that takes the integer crashes. Help?

#include<iostream>
#include<cstdlib>
using namespace std;

class K
{
    public:
    K() {cout<<"In K constructor"<<endl;}

    protected:
    void kPrepare() {cout<<"K Prepare called"<<endl;}
    void kEgg() {cout<<"K Egg called"<<endl;}
};

class Omlette : public K
{
    public:
    Omlette() {cout<<"In Omlette constructor"<<endl;}

    protected:
    void doPreparation()
    {
        mix();
        kPrepare();
        kEgg();
        donePreparing();
    }
    void mix() {cout<<"Mix called"<<endl;}
    void donePreparing() {cout<<"DonePreparing called"<<endl;}  
};

class FoodBase
{
    public:
    virtual void prepare()=0;
    virtual void egg()=0;
    virtual void egg(int)=0;

    FoodBase() {cout<<"FoodBase constructor called"<<endl;}

};

class OmletteHandler : public FoodBase, Omlette
{   
    public:
    OmletteHandler() {cout<<"Omlette handler constructor called"<<endl;}
    void prepare() 
    {
        cout<<"In prepare() Omlette Handler"<<endl;
        doPreparation();//from Omlette class
    }
    void egg() {cout<<"In egg() Omlette Handler"<<endl;}
    void egg(int i) {cout<<"In egg("<<i<<") Omlette Handler"<<endl;}
};

class Food
{
    public:
    FoodBase *base;

    Food(int foodID)//getting foodID from commandline just for testing purpose
    {
        OmletteHandler immediate;//changed from 'imm' to 'immediate'
        base=&immediate;
    }//Food

    void prepare() {base->prepare();}
    void egg() {base->egg();}
    void egg(int i) {base->egg(i);}
};

int main(int argc, char *argv[])
{
    int foodID=1;
    if (argc>1) {foodID = atoi(argv[1]);}

    cout<<"FoodID supplied="<<foodID<<endl; 

    Food f(foodID);
    f.prepare();
    f.egg();
    f.egg(10);

}//main
  • 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-16T23:27:24+00:00Added an answer on May 16, 2026 at 11:27 pm

    In the Food constructor you create an OmeletteHandler on the stack, this gets destroyed once the function exits.

    Food(int foodID)//getting foodID from commandline just for testing purpose 
    { 
        OmletteHandler imm; 
        base=&imm; 
    }//
    

    You could do base = new OmeletteHandler() instead, (don’t forget to delete the pointer later or you’ll have memory leak).

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

Sidebar

Related Questions

My program compiles fine, but crashes everytime throw (not inside of a try...catch block)
I have written a calculator program which compiles fine, but crashes when I hit
The example program below compiles two in-memory assemblies. The first compilation works fine. The
I'm using MinGw on Windows 7. The following simple program compiles fine, but the
This program compiles fine but seg faults when running the value->getSmall in Period::display() .
I'm writing a C program. It compiles fine but when I try to run
when i run my program it compiles fine but when it runs i get
So the program compiles fine, and the executable is generated in $(SolutionDir)/Debug. When I
I am trying to enable link time optimization in g++. My program compiles fine
I have the following program which compiles fine and with no errors or warnings:

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.