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
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.
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
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....
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;
}
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) { }
Error -- No return type specified.
Code:
int main() {
Item *sofa = new Sofa();
sofa->printDetails();
}
Your main() function has a memory leak.
Regards,
Paul McKenzie
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