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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:02:48+00:00 2026-05-21T10:02:48+00:00

I’ve got a problem in a C++ program that I can’t solve. I’m trying

  • 0

I’ve got a problem in a C++ program that I can’t solve.
I’m trying to pass an object as a construction parameter of an other but i can’t figure out how to do this.

My main is creating a Simulation object. In simulation i’m creating two objects a planete and a modelisation. The thing is, i want to pass the planete object in the modelisation object.

My first object is Simulation.

#include "Simulation.h"
#include "Modelisation.h"
#include "Planete.h"

Planete *Obj;
Modelisation *Model;

Simulation::Simulation (int argc, char **argv)
{
    Obj = new Planete ("Terre", (5.98*pow(10.0,24.0)), 6378.137, 0, 0, 0);
    Model = new Modelisation (argc, argv,"Modelisation",1600 ,1004, 306, 0);
};
Simulation::~Simulation ()
{
    delete Obj;
    delete Model;
};

Modelisation.h

class Modelisation
{
private:
    int hauteur, largeur, x, y;
    int fenetre;
    //Planete ClonePlanete
public:
    Modelisation (int argc, char **argv, char[], int, int, int, int);
    ~Modelisation ();
    static void Dessiner ();
    static void Redessiner (int, int);
    void InitCallBack ();
};

Modelisation.cpp

#include "stdafx.h"
#include "Modelisation.h"
#include "Camera.h"
#include "Planete.h"

Camera *camera;

Modelisation::Modelisation (int argc, char **argv, char nomFenetre [] ,int hauteur, int largeur, int x, int y)
{

    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutInitWindowSize (hauteur, largeur);
    glutInitWindowPosition (x, y);
    fenetre = glutCreateWindow (nomFenetre);
    camera = new Camera;
//  Planete ClonePlanete = new Planete(planete);  // Basicaly that's what i'm trying to do
};

Modelisation::~Modelisation ()
{
    delete camera;
};

Planete.h

class Planete
{
private:
    std::string nom;
    double masse;
    double diametre;
    struct position { double x, y, z;};
    double x,y,z;

public:
    Planete (std::string nom, double masse, double diametre, double x, double y, double z);
    Planete (const Planete &);
    ~Planete ();
};

Thanks for your help, it will be very appreciated!

  • 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-21T10:02:49+00:00Added an answer on May 21, 2026 at 10:02 am

    You can pass it in by constant reference in the constructor then have a private members that gets initialized like so:

    Modelisation(const Planete& planRef) : m_planete(planRef)

    Also nifty french variable names 😉

    Edit:

    There are some interesting design choices here you may want to reconsider global variables in favor of class members, favor smart pointers instead of dynamic allocation and deallocation the traditional way, and consider the relationships your objects have. Your simulation is a prime candidate for composition, your planets could use inheritance and polymorphism as you progress to make drawing them with the model easier later.

    Edit: I was bored …

    class Planete
    {
    public:
        Planete (const std::string& nom, double masse, double diametre, double x, double y, double z)
            : m_nom(nom), m_masse(masse), m_position(x, y, z) {}
        virtual ~Planete() {}
    
        // default implementation as most planets don't have people
        virtual unsigned long HumanPopulation() { return 0; } // virtual with default implemenation, each planet can change it
        virtual double Temp() = 0; // pure virtual each planet must have it's own version
    protected:
        std::string m_nom;
        double m_masse;
        double m_diametre;
        class Position
        {
        public:
            Position(double x, double y, double z)
                : x_(x), y_(y), z_(z) {}
        private:
            double x_, y_, z_;
        };
        Position m_position;
    };
    
    class Terre : public Planete // Earth is a Planet
    {
    public:
        Terre(double masse, double diametre, double x, double y, double z)
            : Planete("Terre", masse, diametre, x, y, z) {}
        ~Terre() {}
    
        // override base class default virtual
        unsigned long HumanPopulation() { return CalculatePeople(); }
        double Temp() { /* yata yata */ }
    
    private:
        unsigned long CalculatePeople() { /* figure out how many people on earth */ }
    };
    
    class Mercure : public Planete // Mecury is also a Planet
    {
    public:
        Mercure(double masse, double diametre, double x, double y, double z)
            : Planete("Mercure", masse, diametre, x, y, z) {}
        ~Mercure() {}
    
        double Temp() { /* tres chaud */ } 
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.