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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:22:10+00:00 2026-06-15T04:22:10+00:00

Another question for those far wiser than I: I’m trying to create 3 instances

  • 0

Another question for those far wiser than I:

I’m trying to create 3 instances of a Player Class like so:

Player *player1 = new Player("Aaron",0.3333333);
Player *player2 = new Player("Bob",0.5);
Player *player3 = new Player("Charlie",1);

You can see their constructor below. It’s really simple:

Player::Player(string n, double hr)
{
    name = n;
    hitrate = hr;
}

(just assume name and hitrate are defined properly)

Now my problem is this, when I try to check each individual player for their name, it seems they have all become aliases of sorts for player3

//Directly after the player instantiations:
cout << player1->getName() << "\n";
cout << player2->getName() << "\n";
cout << player3->getName() << "\n";

//In the Player.cpp file:
string Player::getName(){
    return name;
}


Outputs: 
Charlie
Charlie
Charlie

Alright, so I’d really like to know the best solution to get around this problem but more importantly I just want to understand why it’s behaving this way. It seems like such a simple thing (being spoiled by java as I am).

Also it’s important to note: this is for a school assignment and I am told that I MUST use dynamically allocated objects.

Thanks so much, and do let me know if anything needs clarifying.

Edit: By demand, here are the full files:

PlayerTest.cpp

#include <iostream>
#include <player.h>
using namespace std;

int main(){
    Player *player1 = new Player("Aaron",0.3333333);
    Player *player2 = new Player("Bob",0.5);
    Player *player3 = new Player("Charlie",1);
    cout << player1->getName() << "\n";
    cout << player2->getName() << "\n";
    cout << player3->getName() << "\n";
    return 0;
}

Player.h

#ifndef PLAYER_H
#define PLAYER_H
#include <string>
using namespace std;

class Player
{
    public:
        Player(string, double);
        string getName();
};

//Player.cpp
#include "Player.h"

string name;
double hitrate;

Player::Player(string n, double hr)
{
    name = n;
    hr = hitrate;
}


string Player::getName(){
    return name;
}

#endif // PLAYER_H
  • 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-15T04:22:11+00:00Added an answer on June 15, 2026 at 4:22 am

    The name and hitrate variables need to be inside the Player class declaration so that each object gets its own separate copies.

    class Player
    {
        public:
            Player(string, double);
            string getName();
    
        private:
            string name;
            double hitrate;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another question related to this one . I have a List<SortableObjects> that is the
Another question of mine about optimizing Objective C programs inspired the following: does anyone
Answering another question about how const string data was stored in an executable a
From another question I recently posted, it seems that Classic ASP might not be
In another question, a user posted the following reflog: 8c48bab HEAD@{16}: checkout: moving from
In another question , I asked something similar but I ended up just posting
In another question, the answer states that on Unixes with /proc , the really
In another question , the accepted answer suggested replacing a (very cheap) if statement
In another question I asked (why is Rake test so slow ), part of
In another question I had the problem to port the code: unsigned long stack[]

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.