Hello.
I was fooling around with abstract classes (Possibly in a way you're not supposed to use them). The following code asks questions about food, and has two possible branches, Member->FruitMember and Member->NotMember, The first branch will ask questions about a fruit type of food, the second will ask general questions and output "its not a fruit!". Everything works perfectly fine, you could actually test it by replacing (see code below)
myFood = new Member;
with
1 2
|
myFood = new FruitMember2; //Or NotMember2
myFood->Info()
| |
As you might notice, by initializing FruitMember2 we initialize all contructors that form the base of this class (Food, Member, FruitMember, FruitMember2).
The problem is, I want the program to determine which branch to take, right now its a simple check (if name of the fruit is "Apple")... Problem is, if I just declare myFood = FruitMember2, It will launch all constructors *again*, this will force the user to input the same data twice. Any ideas how to intialize only the FruitMember and FruitMember2 contructors, while preserving all data from Food and Member constructors? Seeing the code below will propably make everything clear... Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class Food
{
public:
Food()
{
cout << "What is the name of this food? - ";
cin >> mName;
cout << "How many calories in this food? - ";
cin >> mCals;
cout << "What is the shape of this food? - ";
cin >> mShape;
}
virtual void Info() const = 0;
virtual bool Check() const = 0;
int mCals;
string mShape;
string mName;
};
class Member : public Food
{
public:
Member()
{
cout << "What colour is it? - ";
cin >> mColour;
}
virtual void Info() const
{
cout << "\n" << mName << " has " << mCals
<< " it is a " << mColour << " " << mShape;
}
virtual bool Check() const
{
if(mName == "Apple") { return true; }
return false;
}
string mColour;
};
class NotMember : public Member
{
public:
NotMember()
{
cout << "How much does it weight? - ";
_flushall();
getline( cin, mHeavy, '\n' );
}
virtual void Info() const
{
cout << "\n" << mName << " has " << mCals << " calories"
<< ", it is a " << mColour << " " << mShape
<< "\nIt weights " << mHeavy << ", and its not a fruit!";
}
protected:
string mHeavy;
};
class NotMember2 : public NotMember
{
public:
NotMember2()
{
cout << "Is it an Expensive fruit? (y/n)";
if (_getch() == 'y') { mExpensive = "true"; }
else { mExpensive = "false"; }
}
virtual void Info() const
{
cout << "\n" << mName << " has " << mCals << " calories"
<< ", it is a " << mColour << " " << mShape << "."
<< "\nIt weights " << mHeavy << ", and its not a fruit!\n"
<< "It is " << mExpensive << " that this food is expensive.";
}
protected:
string mExpensive;
};
class FruitMember : public Member
{
public:
FruitMember()
{
cout << "Is it a tropical fruit? (y/n)";
if (_getch() == 'y') { mTropical = "true"; }
else { mTropical = "false"; }
}
virtual void Info() const
{
cout << "\n" << mName << " has " << mCals << " calories"
<< ", it is a " << mColour << " " << mShape
<< ". It is " << mTropical << " that this fruit is tropical.";
}
protected:
string mTropical;
};
class FruitMember2 : public FruitMember
{
public:
FruitMember2()
{
cout << "\nIs it a European fruit? (y/n)";
if (_getch() == 'y') { mEuropean = "true"; }
else { mEuropean = "false"; }
}
virtual void Info() const
{
cout << "\n" << mName << " has " << mCals << " calories"
<< ", it is a " << mColour << " " << mShape
<< ".\n It is " << mTropical << " that this fruit is tropical.\n"
<< "It is " << mEuropean << " that this fruit is European.";
}
protected:
string mEuropean;
};
int main()
{
Food* myFood;
myFood = new Member;
if (myFood->Check())
{
//What I need is myFood = new FruitMember2; but without initializing
//All the constructors that were already constructed (Food and Member)
//Instead, I need all the data already in myFood to be passed to FruitMember
}
else
{
//Same as above, but similar to myFood = new NotMember2;
}
return 0;
};
| |