I have a base class tree. Using this class, I have to create two derived classes, oak and pine.
Here is the base class tree:
Code:
#ifndef TREE_H
#define TREE_H
//Tree class declaration
#include<iostream>
using namespace std;
#define dead 0
#define live 1
#define burning 2
#define newt 3
class tree
{
protected:
string type;
double probCatch;
int status;
double wetness;
int burnTime;
public:
tree(); //default constructor
tree(string t, double prob, int stat, double wet, int burn); //constructor
double getProbCatch() const; //accessor
void setProbCatch(double prob); //mutator
int getStatus() const;
void setStatus(int stat);
double getWetness() const;
void setWetness(double wet);
int getBurnTime() const;
void setBurnTime(int burn);
ostream& showSymbol(ostream& out);
friend ostream& operator<<(ostream& out, tree& a);
};
#endif //TREE_H
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "tree.h"
#include "oak.h"
#include "pine.h"
using namespace std;
//-------------------------------------------------------------------------
//Default Tree Constructor
//-------------------------------------------------------------------------
tree::tree(): type("not_a_tree"), probCatch(0), status(dead), wetness(1), burnTime(1)
{
//empty body; initialization
}
//-------------------------------------------------------------------------
//Tree Constructor
//-------------------------------------------------------------------------
tree::tree(string t, double prob, int stat, double wet, int burn): type(t), probCatch(prob), status(stat), wetness(wet), burnTime(burn)
{
//empty body; intialization
}
//-------------------------------------------------------------------------
//Fetching Probability
//-------------------------------------------------------------------------
double tree::getProbCatch() const
{
return probCatch;
}
//-------------------------------------------------------------------------
//Setting Probability
//-------------------------------------------------------------------------
void tree::setProbCatch(double prob)
{
probCatch=prob;
}
//-------------------------------------------------------------------------
//Fetching Status
//-------------------------------------------------------------------------
int tree::getStatus() const
{
return status;
}
//-------------------------------------------------------------------------
//Setting Status
//-------------------------------------------------------------------------
void tree::setStatus(int stat)
{
status=stat;
}
//-------------------------------------------------------------------------
//Fetching Wetness
//-------------------------------------------------------------------------
double tree::getWetness() const
{
return wetness;
}
//-------------------------------------------------------------------------
//Setting Wetness
//-------------------------------------------------------------------------
void tree::setWetness(double wet)
{
wetness=wet;
}
//-------------------------------------------------------------------------
//Fetching Burn Time
//-------------------------------------------------------------------------
int tree::getBurnTime() const
{
return burnTime;
}
//-------------------------------------------------------------------------
//Setting Burn Time
//-------------------------------------------------------------------------
void tree::setBurnTime(int burn)
{
burnTime=burn;
}
//-------------------------------------------------------------------------
//Overloading Output <<
//-------------------------------------------------------------------------
ostream& operator<<(ostream& out, tree& a)
{
out << "Species: "<< a.type <<endl;
out << "Probability: "<< a.probCatch <<endl;
out << "Status: "<< a.status <<endl;
out << "Wetness: "<< a.wetness <<endl;
out << "Burn time: "<< a.burnTime <<endl;
return out;
}
//-------------------------------------------------------------------------
//Show Symbols
//-------------------------------------------------------------------------
ostream& tree::showSymbol(ostream& out)
{
out<<"\033[32;40m!"<<endl;
return out;
}
Here is the oak class:
Code:
//Oak class declaration
#include "tree.h"
#include<iostream>
using namespace std;
#define dead 0
#define live 1
#define burning 2
#define newt 3
class oak : public tree
{
public:
oak(); //default constructor
oak(string t, double prob, int stat, double wet, int burn); //constructor
ostream& showSymbol(ostream& out);
friend ostream& operator<<(ostream& out, tree& a);
};
Code:
#include <string>
#include <cstdlib>
#include "oak.h"
#include "tree.h"
using namespace std;
//-------------------------------------------------------------------------
//Default Tree Constructor
//-------------------------------------------------------------------------
oak::oak(): type("not_a_tree"), probCatch(0), status(dead), wetness(1), burnTime(1)
{
//empty body; initialization
}
//-------------------------------------------------------------------------
//Tree Constructor
//-------------------------------------------------------------------------
oak::oak(string t, double prob, int stat, double wet, int burn): type(t), probCatch(prob), status(stat), wetness(wet), burnTime(burn)
{
//empty body; intialization
}
//-------------------------------------------------------------------------
//Show Symbols
//-------------------------------------------------------------------------
ostream& oak::showSymbol(ostream& out)
{
out<<"\033[32;40m@"<<endl;
return out;
}
The pine class is the same as the oak class except for the symbol.
I'm getting the following errors when it compiles:
Code:
oak.cpp: In constructor ‘oak::oak()’:
oak.cpp:14: error: class ‘oak’ does not have any field named ‘type’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘probCatch’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘status’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘wetness’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘burnTime’
oak.cpp: In constructor ‘oak::oak(std::string, double, int, double, int)’:
oak.cpp:23: error: class ‘oak’ does not have any field named ‘type’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘probCatch’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘status’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘wetness’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘burnTime’
pine.cpp: In constructor ‘pine::pine()’:
pine.cpp:13: error: class ‘pine’ does not have any field named ‘type’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘probCatch’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘status’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘wetness’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘burnTime’
pine.cpp: In constructor ‘pine::pine(std::string, double, int, double, int)’:
pine.cpp:22: error: class ‘pine’ does not have any field named ‘type’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘probCatch’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘status’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘wetness’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘burnTime’
This is my first time doing polymorhpism and I'm not sure how to do it. I'm making some mistakes that are messing up the program and I can't catch them. Please help me if you can.