I don\'t need If sttatements in my programs because of the Preconditions: I don\
ID: 3544843 • Letter: I
Question
I don't need If sttatements in my programs because of the Preconditions: I don't need to check the conditions for that part, I assume it is already given to me
Instructor: Hazel M. Olyle
What To Do
Create a Elephants class and declare the following class member variables (you can choose thenames), which are meant to hold information describing an elephant . Make all these variables private and properly comment them. Here is an example of a declaration with a suitable comment:
char gender; // the gender of this elephant.
Explanation / Answer
ok just a couple of deletions and some changes in main.cpp...
//Elephant.cpp
#include "Elephant.h"
Elephant::Elephant(string n, int y, int m, char g)
: nickname(n), birthYear(y), birthMonth(m), gender(g),
tag(-1), mother(NULL), father(NULL), childCount(0)
{
}
Elephant::Elephant(string n, int y, int m, char g,
int t, Elephant * mom, Elephant * dad)
: nickname(n), birthYear(y), birthMonth(m), gender(g),
tag(t), mother(mom), father(dad), childCount(0)
{
++(mother->childCount);
++(father->childCount);
}
void Elephant::addMother(Elephant * mom)
{
mother = mom;
++(mother->childCount);
}
void Elephant::addFather(Elephant * dad)
{
father = dad;
++(father->childCount);
}
//main.cpp
#include <iostream>
#include "Elephant.h"
using namespace std;
void printElephantInfo(const Elephant& e)
{
cout << e.getName() << " was born in " << e.getMonth() << "/" << e.getYear()
<< ". " << (e.isMale() ? "His" : "Her")
<< " father is " << (e.getFather() ? e.getFather()->getName() : "unknown")
<< " and mother is " << (e.getMother() ? e.getMother()->getName() : "unknown")
<< ". " << (e.isMale() ? "He" : "She") << " has " << e.getNumChildren() << " child(s). ";
if (e.getTag() == -1)
cout << "Tag: none ";
else
cout << "Tag: " << e.getTag() << " ";
}
int main()
{
Elephant elephantA("Elephant A ?D", 1999, 5, 'M');
Elephant elephantB("Elephant B ??", 2004, 2, 'F');
Elephant elephantC("Elephant C A?", 2006, 12, 'F');
Elephant elephantD("Elephant D ??", 2003, 7, 'F');
Elephant elephantE("Elephant E AB", 2012, 8, 'M', 1, &elephantB, &elephantA);
Elephant elephantF("Elephant F ?C", 2013, 10, 'F', 3, &elephantC, &elephantA);
elephantA.setTag(1);
elephantB.setTag(1);
elephantA.addMother(&elephantD);
printElephantInfo(elephantA);
printElephantInfo(elephantB);
printElephantInfo(elephantC);
printElephantInfo(elephantD);
printElephantInfo(elephantE);
printElephantInfo(elephantF);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.