CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2007
    Posts
    5

    Constructor to allow interactive entry

    My class of a item of furniture, and sofa with item inherited is below:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Item
    
    {
    	public:
    		string modelName;
    		int	numberInstock;
    
    		Item(){}			
    		Item(int name){}
    
    		virtual void UpdateNumber(){}
    		virtual void printDetails()
    		{
    
    		}
    
    };
    
    class Sofa:public Item
    
    {
    	
    public:
    
    	Sofa (string a , string b) : Item () 
    	{
    	fabric = a;
    	colour = b;
    	}
    
    	Sofa () : Item ()
    	{
    		cout << "Enter type of fabric: ";                 //// This constructor
    		cin >> fabric;
    		cout << endl << "Enter colour of fabric: ";
    		cin >> colour;
    		cout << endl;
    	}
    
    	string fabric;
    	string colour;
    	
    	void printDetails(){}
    };
    The constructor which i have marked "This constructor" is the one I am working on. I want a constructor with no parameters which allows the user to enter data members interactively. How is this done?

    Thanks

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Constructor to allow interactive entry

    Is there a reason why the constructor must have no parameters? I don't understand why you can't just read the values and then create the object. By doing it the way you suggest, you're severely limiting your options for creating an object. What if you later decide you want to get the values from a file? By reading the values first, you can validate them and get them corrected if necessary.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Apr 2007
    Posts
    5

    Re: Constructor to allow interactive entry

    This is part of the actual question which I am trying to answer

    " Include two constructor declarations for each of the subclasses: one constructor with parameters for passing the values of all data members and another constructor with no parameters, but which allows the user to enter the values for all data members interactively."

    sofa is one of the subclasses

    Do you think the second part just means initialising the variables so they can be modified later?

    Thanks

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Constructor to allow interactive entry

    That is how I would take it...

    If you want to embody the two together consider a Factory or Builder Pattern....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    May 2007
    Posts
    13

    Re: Constructor to allow interactive entry

    Init using the subclass. See main().


    Code:
    int main() {
      Item *sofa = new Sofa();
      sofa->printDetails();
      delete sofa;
      return 0;
    }
    Last edited by crei; May 5th, 2007 at 03:27 PM.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor to allow interactive entry

    Quote Originally Posted by crei
    Init using the subclass. See main().
    If you're going to post answers please use code tags when posting code.
    Code:
    class Sofa:public Item
    {
    public:
      Sofa (string a , string b) : Item ()
    Pass strings by const reference. Use the initializer list to initialize the members. Correction below.
    Code:
    class Sofa : public Item
    {
       public:
          Sofa (const std::string& a , const std::string& b) : Item (),
                 fabric(a), colour(b) { }
    Code:
      Sofa () : Item ()
    Error -- No return type specified.
    Code:
    int main() {
      Item *sofa = new Sofa();
      sofa->printDetails();
    }
    Your main() function has a memory leak.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor to allow interactive entry

    Quote Originally Posted by DrZork101
    My class of a item of furniture, and sofa with item inherited is below:
    Your item class has no virtual destructor. If you have virtual functions, and you derive from Item, then Item should have a virtual destructor.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured