assigning values to constructor

This is a discussion on assigning values to constructor within the C++ Programming forums, part of the General Programming Boards category; hi,i was writing a hospital program to manage 3 types of patients.(outpatient,inpatient,daypatient) For example for int variables. we can initially ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    assigning values to constructor

    hi,i was writing a hospital program to manage 3 types of patients.(outpatient,inpatient,daypatient)

    For example for int variables. we can initially declare it as int a; then do, a=10;
    can the same be done for class?

    lets say we have a
    Code:
    class patient{
        private:
             int name;
             int id;
        public:
             patient(int newName,int newId);
    }
    then we have patient a;
    then we have a=(name,id);

    can that be done for classes ??just like it was done for int variables as show above.

    thanx.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    You can initialise your member variables in a constructor by using an initialisation list, e.g.,
    Code:
    class patient {
        private:
             int name;
             int id;
        public:
             patient(int newName, int newId) : name(newName), id(newId) {}
    };
    Of course, you can still do assignment if you prefer, in pretty much the same way as you would in a normal function, except that you would not declare the member variables, e.g.,
    Code:
    class patient {
        private:
            int name;
            int id;
        public:
            patient(int newName, int newId) {
                name = newName;
                id = newId;
            }
    };
    Constructor initialisation lists are usually preferred, and watch the semi-colon at the end of your class declaration.

    If you are talking about how to use the constructor, then it would be as simple as:
    Code:
    patient sujeet1(1, 2);
    Though I do not understand why you take an int for a name
    Last edited by laserlight; 05-08-2007 at 09:18 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    24
    the reason y i took a int was for example.

    Code:
    for int  we would have:
    
    int sujeet;
    sujeet=100;
    
    so can we do like ....
    patient sujeet;
    //have some code to assign sujeet some data???
    hope you all get what i meant . thanx

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Look at my constructor use example for initialisation with multiple values. If you want to perform assignment of multiple values, then provide a member function to do it. If you want to perform assignent of a single value, then that can be done with the copy assignment operator and an appropriate constructor, but it is usually better to initialise instead of assign if you can.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Replies: 2
    Last Post: 11-26-2008, 09:25 AM
  3. Replies: 1
    Last Post: 06-17-2008, 04:00 PM
  4. Assigning values to arrays
    By napkin111 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2003, 07:52 PM
  5. Can't find my constructor
    By Nippashish in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21