|
-
May 2nd, 2007, 04:35 PM
#1
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
-
May 2nd, 2007, 04:55 PM
#2
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
-
May 2nd, 2007, 05:14 PM
#3
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
-
May 2nd, 2007, 08:29 PM
#4
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
-
May 2nd, 2007, 09:51 PM
#5
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.
-
May 3rd, 2007, 05:26 AM
#6
Re: Constructor to allow interactive entry
 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) { }
Error -- No return type specified.
Code:
int main() {
Item *sofa = new Sofa();
sofa->printDetails();
}
Your main() function has a memory leak.
Regards,
Paul McKenzie
-
May 3rd, 2007, 05:27 AM
#7
Re: Constructor to allow interactive entry
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|