Thread: assigning values to constructor

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10: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, 08:52 PM
  5. Can't find my constructor
    By Nippashish in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 08:17 PM