Thread: Help needed on constructors!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    Help needed on constructors!

    Modify the default constructor to that the data member is initialized to zero in the initialization list. Provide a second constructor to allow "Integer" objects to be created and initialized with a value.
    In your test program, write a function that takes an Integer object as an argument, first by value and then by reference. What do you see printed from constructors and destructors when the function is called? Explain what you understand in a few statements.

    Code:
    #include <iostream>
    using namespace std;
    //Muhammad Danish
    //67
    //SEECS
    
    class R{
    public:
        int danish1;
        int danish2;
        int danish3;
        R();
    
    R(int a,int b,int c);
    };
    
    int main()
    {R f;
        cout<<"Danish1="<<f.danish1;
    }
    
    
    R::R(int a=7,int b=9,int c=7){
    danish1=a;
    danish2=b;
    danish3=c;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, what's the problem that you are facing?
    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
    Nov 2011
    Posts
    64
    can you differentiate b/w different types of constructors and how they are called??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, a default constructor is one that can be invoked with no arguments. That should have been stated somewhere in your learning material.

    As to the code that you posted: I do not see the definition of an Integer class anywhere.
    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

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    integer class is R.

    Plus what if I want to initialize the integers by zero using default constructor.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by danishzaidi
    integer class is R.
    Why does your integer class have member variables named danish1, danish2 and danish3? Oh, and why it is named R rather than Integer?

    Quote Originally Posted by danishzaidi
    Plus what if I want to initialize the integers by zero using default constructor.
    Your current default constructor does not do that because:
    • Its default arguments are integers other than 0.
    • You assign to the member variables in the body of the constructor instead of using the intialiser list as per your instructions.
    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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    To get rid of plagiarism,I named it R instead of Integer and more Danish1,danish2 ,Danish 3 are integers to be initialized first by zero and other two by a value other than zero.

    Code:
    #include <iostream>
    using namespace std;
    //Muhammad Danish
    //67
    //SEECS
    
    class R{
    public:
        int danish1;
        int danish2;
        int danish3;
        int danish4;
        R();
    
    R(int l,int w,int h);
    };
    
    int main()
    {R f();
        cout<<"\n Initialised by zero using default constructor is \n"<<f().danish1;
        
    R f2(10,3,6);
    cout<<"\nOther by Initialised by value using Initialising list are\n"<<f2().danish2<<f().danish3<<f().danish4;
    }
    
    
    R::R():danish1(0){
    
    cout<<"\nR Default constructor is called\n";
    }
    R::R(int l=1, int w=2, int h=3): danish2(l),danish3(w),danish4(h)
         {cout<<"\nBox contructor is invoked";
    }

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    dont know what to do now?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I notice that your code also has another default constructor declared. That would be an error. If you want to declare a constructor with no parameters, then your constructor that has three parameters should not have default arguments for all the parameters, otherwise it would also be a default constructor. A class can only have one default constructor.
    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

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    what do u mean by default arguments????

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by danishzaidi
    what do u mean by default arguments????
    Look at this part:
    Code:
    R::R(int l=1, int w=2, int h=3)
    You are saying that if the argument that corresponds to the parameter h is not supplied when this constructor is invoked, then h will default to having a value of 3. Likewise, if the arguments corresponding to both h and w are both not supplied, then w will have default to having a value of 2. If no arguments are supplied, then l will default to having a value of 1.

    Problem is, you also declared:
    Code:
    R();
    defining it as:
    Code:
    R::R():danish1(0){
        cout<<"\nR Default constructor is called\n";
    }
    So, if you create an object of R without supplying any arguments to its constructor, which constructor should be called? That is an ambiguity there.
    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

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    Quote Originally Posted by laserlight View Post
    Look at this part:
    Code:
    R::R(int l=1, int w=2, int h=3)
    You are saying that if the argument that corresponds to the parameter h is not supplied when this constructor is invoked, then h will default to having a value of 3. Likewise, if the arguments corresponding to both h and w are both not supplied, then w will have default to having a value of 2. If no arguments are supplied, then l will default to having a value of 1.

    Problem is, you also declared:
    Code:
    R();
    defining it as:
    Code:
    R::R():danish1(0){
        cout<<"\nR Default constructor is called\n";
    }
    So, if you create an object of R without supplying any arguments to its constructor, which constructor should be called? That is an ambiguity there.
    Thanks mam.!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Constructors
    By jjetson in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2012, 05:01 PM
  2. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  3. ()'s in constructors
    By blight2c in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2002, 01:16 AM
  4. Constructors
    By wireless in forum C++ Programming
    Replies: 6
    Last Post: 03-03-2002, 11:25 AM
  5. Constructors
    By incognito in forum C++ Programming
    Replies: 6
    Last Post: 12-30-2001, 10:55 PM