Jump to content
  • Advertisement
Sign in to follow this  
blacksmit049

Help with Constructors

This topic is 2916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

//Plant Caretaker
//My first own created game
//ver 0.1
#include<string>
#include<vector.h>
#include<iostream.h>

using namespace std;

class Plant{
public:
Plant(const string& name="",int waterLevel=3,int fertilizerLevel=3);
void waterPlant(int water=3);
void fertilizePlant(int fertilizer=3);

void checkPlant();
string GetName() const
{
return m_Name;
}

private:
int m_waterLevel;
int m_fertilizerLevel;
string m_Name;
void passTime();
int GetMood();
};

Plant::Plant(const string& name="", int waterLevel, int fertilizerLevel):m_Name(name),
m_waterLevel(waterLevel),
m_fertilizerLevel(fertilizerLevel){}

void Plant::waterPlant(int water)
{ cout<<"Blop Blop Blop";
m_waterLevel+=water;
passTime();
}

void Plant::fertilizePlant(int fertilizer)
{ cout<<"Nom Nom Nom";
m_fertilizerLevel+=fertilizer;

passTime();
}

void Plant::passTime(){
m_waterLevel--;
m_fertilizerLevel--;
}

int Plant::GetMood()
{
return (m_waterLevel + m_fertilizerLevel);
passTime();
}

void Plant::checkPlant()
{
cout << "I'm a plant and I feel ";
int mood = GetMood();
if (mood > 10)
cout << "super.\n";
else if (mood < 5)
cout << "okay.\n";
else if (mood < 0)
cout << "sad.\n";
else
cout << "sorrowful.\n";
passTime();
}



int main(){

int choice;
string name;
cout<<"Welcome to Plant caretaker";

cout<<"\n\nName your plant first\n";
cin>>name;
Plant pName(name);
do
{
cout<<"0 - Exit\n";
cout<<"1 - Water your plant";
cout<<"2 - Fertilize your plant";
cout<<"3 - Check your plant";
cin>>choice;
switch (choice)
{
case 0:
cout << "Good-bye.\n";
break;
case 1:
pName.waterPlant();
break;
case 2:
pName.fertilizePlant();
break;
case 3:
pName.checkPlant();
break;
default:
cout << "\nSorry, but " << choice << " isn't a valid choice.\n";
}
}
while (choice != 0);

return 0;
}


Can someone help me what's wrong with the constructor of the class Plant. I'm just new to classes and constructors and I'm using Netbeans with the cygwin compiler and the book Beginning C++ Game Programming by Michael Dawson. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
It would be easier to help you, if would
a) Wrap code inside code tags (see the forum faq)
b) State what error you are getting when compiling / at runtime

I guess the problem is that you define the default values for your constructor twice, once when you declare the constructor:

Plant(const string& name="",int waterLevel=3,int fertilizerLevel=3);


and once when you define the constructor:


Plant::Plant(const string& name="", int waterLevel, int fertilizerLevel):m_Name(name),
m_waterLevel(waterLevel),
m_fertilizerLevel(fertilizerLevel){}

void Plant::waterPlant(int water)
{ cout<<"Blop Blop Blop";
m_waterLevel+=water;
passTime();
}



Simply get rid of the ="" in the definition and the constructor should compile (if I haven't missed anything else).

Share this post


Link to post
Share on other sites
what Shadowman said.

Also note that "sad" will never be printed from Plant::checkPlant()
and "sorrowful" will be printed between "okay" and "super".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!