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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:42:51+00:00 2026-05-24T08:42:51+00:00

I’m sure this has something to do with virtual functions, but I’m struggling to

  • 0

I’m sure this has something to do with virtual functions, but I’m struggling to work out how.

Here is my (simplified) situation:

Roughly what the program does is have one pair of files (computer.h) draw a computer with a blank screen, and another pair (program.h) which has a function that needs to draw on that computer screen

The computer class is going to be re-used in many different situations, so the screen draw function needs to be passed in a generic fashion

in computer.h:

include "screen.h"

class computer {

    void drawComputer(); //this function draws a picture of a computer
    void drawMonitor();

};

in computer.cpp:

void computer::drawComputer(){

    //draws all the components then the monitor

    drawMonitor(); //this is where the external function (from class screen) needs to execute
}

void computer::drawMonitor(){
    //draws the background and border of screen
}

in program.h:

class program {

    //many other program functions

    void drawScreen();

};

in program.cpp:

//many other program functions

void program::drawScreen(){
    //this function draws the contents of the screen
}

My question is, from program.cpp, how do I ‘send’ the drawScreen() function to execute within the drawMonitor() function in computer.cpp?

Edit

@Chris’ solution seems to be nearly exactly what I am after, however when I attempt to implement it I get the following errors:

testApp.h:40: error: 'testApp::prog' cannot appear in a constant-expression
testApp.h:40: error: `&' cannot appear in a constant-expression
testApp.h:40: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
testApp.h:40: error: ISO C++ forbids initialization of member 'isprog'
testApp.h:40: error: making 'isprog' static
testApp.h:40: error: invalid in-class initialization of static data member of non-integral type 'IScreen*'
testApp.h:41: error: 'isprog' has not been declared
testApp.h:42: error: ISO C++ forbids declaration of 'comp1' with no type
testApp.h:42: error: expected ';' before '.' token

The lines are

39    Program prog;
40    IScreen *isprog = dynamic_cast<IScreen*>(&prog);
41    OP1 comp1(isprog);
42    comp1.drawScreen();

Anyone know where I’m going wrong with the implementation?

  • 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-24T08:42:52+00:00Added an answer on May 24, 2026 at 8:42 am

    Well you’re half-way there. For something like this, yes I’d use virtual functions (in order to define an abstract interface). Here’s the basic outline of how I’d do it:

    // First create a class to define an interface for drawing a screen
    class IScreen
    {
    public:
        // Defines an interface named drawScreen
        virtual void drawScreen() = 0;
    };
    
    // Next actually implement the interface
    class Program : public IScreen
    {
    public:
        // Here we actually implement it
        virtual void drawScreen()
        {
            // Draw some stuff here
        }
    };
    
    // You can implement this more than once if you want
    class BlueScreenOfDeathScreen : public IScreen
    {
    public:
        virtual void drawScreen()
        {
            // Draw a BSOD on the screen
        }
    };
    
    // Finally, use the interface
    class Computer
    {
    private:
        IScreen* myScreen;
    
    public:
        Computer(IScreen* screen)
            : myScreen(screen)
        {
        }
    
        void drawComputer()
        {
            // ...
        }
    
        void drawMonitor()
        {
            // Draw the monitor
            // ...
    
            // Draw the screen
            myScreen->drawScreen();
        }
    };
    

    Doing it this way, you can easily define multiple IScreen implementations and quickly swap them out with minimal changes to your code.

    // Render using the "Program" class
    Program prog;
    IScreen *iprog = dynamic_cast<IScreen*>(&prog);
    Computer comp1(iprog);
    comp1.drawScreen();
    
    // Render using the "BlueScreenOfDeathScreen" class
    BlueScreenOfDeathScreen bsod;
    IScreen *ibsod = dynamic_cast<IScreen*>(&bsod);
    Computer comp2(ibsod);
    comp2.drawScreen();
    

    Easy, no?

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.