Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ constructor help

Options
  • 04-11-2006 6:26pm
    #1
    Closed Accounts Posts: 4,199 ✭✭✭


    Hey, guys. I have created a class with a few integer variables. I then created 4 seperate objects using this class. Now, when I use one of these obects I want the constructor to initialise the values in the object in question. But, each of the four objects requires different values to be in the variables. How can I use constructors to initialise different values in these objects that are all of the same class? Sample code would be a great help. Thanks.


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Sandor wrote:
    Sample code would be a great help. Thanks.
    Yes it would, why don't you give us a sample of your code and then somebody will help? One of the things they don't teach you in college is how to ask a question on public forums. It's generally a good idea to give a sample of you code, explain your efforts and then ask for help. Otherwise it just looks like your asking somebody else to do your homework for you. It's a simple enough task and you'll never learn unless you try.Try googling on Using Constructors or something, that should start you in the right direction (Searching the web is another important skill for a developer).


  • Registered Users Posts: 141 ✭✭coward


    something like this?

    class A
    {
    public:
    A(int valueA, int valueB, int valueC)
    {
    i1 = valueA;
    i2 = valueB;
    i3 = valueC;
    }
    private:
    int i1;
    int i2;
    int i3;
    };

    int main()
    {
    A myObject1(1,2,3);
    A myObject2(4,5,6);
    }


  • Closed Accounts Posts: 4,199 ✭✭✭Shryke


    Thanks coward, but it's ok. I figured out what I was trying to do.
    Phil, if I wasn't so nice i'd tell you where to go. People like you can be found everywhere on the net. You stroll into my topic, make a stupid, overlong speech, talking down to me all the while and then leave without giving any help or input. If you have nothing to say keep your mouth shut. If you want a sample of my code, then ask. It doesn't hurt. As it is I thought I made things pretty clear.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Sandor wrote:
    Thanks coward, but it's ok. I figured out what I was trying to do.
    Phil, if I wasn't so nice i'd tell you where to go. People like you can be found everywhere on the net. You stroll into my topic, make a stupid, overlong speech, talking down to me all the while and then leave without giving any help or input. If you have nothing to say keep your mouth shut. If you want a sample of my code, then ask. It doesn't hurt. As it is I thought I made things pretty clear.
    May i refer you to the charter on homework. In other words coward could (technically speaking) be slapped for breaking the charter as he gave you the solution without you learning anything.

    The problem with just giving the answer is some people come here and just want a solution so they can hand something in. Then along comes their end of year exams and they fail horribly. The fail because they haven't a clue, as they never worked through a problem themselves. Hence people here generally will *not* tell you how to answer a question, but *will* tell you where you went wrong with your solution.


Advertisement