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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:51:33+00:00 2026-06-04T17:51:33+00:00

I am trying to write a series of programs that use the same main

  • 0

I am trying to write a series of programs that use the same main (main.cpp) file, but with different secondary source files (object1.cpp, object2.cpp, etc). So I will be compiling them essentially like this:

g++ -o program1.exe main.cpp object1.cpp
g++ -o program2.exe main.cpp object2.cpp

What I want to do is have objectN.cpp define a class with certain methods implemented, which will be called from the main file. The source codes look something like this:

header file (object.hpp)

#ifndef INCLUDE_OBJECT_HPP
#define INCLUDE_OBJECT_HPP

class MyObjectInterface
{
public:
  MyObjectInterface();
  virtual ~MyObjectInterface() {};
  virtual void MethodA() = 0;
  virtual void MethodB() = 0;
};

#endif

object1.cpp

#include <iostream>
#include "object.hpp"

using namespace std;

class MyObject : public MyObjectInterface
{
private:
  int member;

public:
  MyObject(int a)  { member = a; }
  void MethodA() { cout << member << endl; }
  void MethodB() { cout << member*2 << endl; }
};

main.cpp

#include <iostream>
#include "object.hpp"

using namespace std;

MyObjectInterface *x;

int main()
{
  x = new MyObject(1);
  x->MethodA();
  x->MethodB();
  return 0;
}

object2.cpp would have a similar structre to object1.cpp but the class would have different data members.

I can’t get this to work, because I need to include the the class declaration of MyObject in main.cpp. But each object*.cpp file will declare the MyObject class with different members, so I cannot just simply make a seperate object.hpp header and include it in main, because I need different headers for different objects. The only thing main should have to know about is the methods.

I hope I’ve explained the problem clearly; if not leave a comment and I’ll try to clarify. It seems like there should be a really simple way of doing something like this but I can’t figure it out.

Thanks

  • 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-04T17:51:35+00:00Added an answer on June 4, 2026 at 5:51 pm

    can’t get this to work, because I need to include the the class declaration of MyObject in main.cpp

    NO you don’t. Make a factory method

    *.h:

    MyObjectInterface* makeObject();
    

    *.cpp:

    MyObjectInterface* makeObject(){
        return new MyObject;
    }
    

    However, having identically named classes with identical structure declared in different files might confuse some compilers in certain circumstances (rare thing, but it can happen).

    Also, better solution would be to rethink your program structure. Because your derived classes are supposed to have different data members, it means you’ll probably need to access those data members. Because you’ll probably need to access those memebrs, then you’ll need to know something about that class, so you’ll have to put its declaration into header.

    Another problem is that class represents some kind of concept, so picking unique name should be fairly trivial. If you have many different classes with SAME name, you might have a design problem – your classes do not represent unique concepts, and you create too many classes needlessly.

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

Sidebar

Related Questions

I am trying write a function that generates simulated data but if the simulated
Trying to write a couple of functions that will encrypt or decrypt a file
I am trying to write a macro that will be attached to a series
I'm trying to write some code that populates a List (actually, it's a series
Trying to write a simple jQuery function that will multiple the index of an
Trying to write out syslog entries containing strings but they don't register. // person.name
Trying to write a code at the moment that basically tests to see if
I am trying to write a small app that uses the subprocess module. My
I am trying to write a function that takes as arguments the name of
I am trying to write a function that will output every other attachment caption

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.