C++:
questions are in fish.h. Write the code in fish.cpp and main.cpp
SOURCE CODE
//fish.h
#ifndef FISH_H
#define FISH_H
#include <vector>
#include <iostream>
#include <cstdlib> //to use rand() and srand()
#include <ctime> // to use time()
using namespace std;
class Fish{
public:
//set edible to is_edible
Fish(bool is_edible){ edible = is_edible; }
//print size and age
void Print()
{
cout << "\nSize: " << size << ", Age: " << age << endl;
}
//reproduce with probablity reproduce_probability
//only if age >= reproduce_age
Fish *Reproduce()
{
//check age
if(age >= reproduce_age)
{
//generate new fish with same edibility as parent
//with given probablity
if(rand() % 10 < reproduce_probability * 10)
return new Fish(edible);
}
//return NULL if conditions are not satisfied
return NULL;
}
//grow fish based on conditions
void Grow(const vector<Fish*>& fish_in_cell)
{
age++;
size += size*0.5;
if(rand() % 10 < 1) alive = false;
}
//getter functions
float getSize(){ return size; }
bool isEdible(){ return edible; }
bool alive = true;
private:
//setting values as mentioned
constexpr static const float reproduce_probability = 0.6;
static const int reproduce_age = 3;
float size = 1;
int age = 0;
bool edible;
};
#endif
//main.cpp
#include <iostream>
#include <vector>
#include "fish.h"
//tetsing the Fish class using main functions
using namespace std;
int main()
{
const vector<Fish *> fish_in_cell;
srand(time(0)); // to generate a new random function everytime rand() is called
Fish f(false); // declaring an unedible fish
//print size and age
f.Print();
//grow fish to age 1
f.Grow(fish_in_cell);
if(f.alive) f.Print();
//grow fish to age 3, in order to reproduce
f.Grow(fish_in_cell);
f.Grow(fish_in_cell);
//declare new fish pointer, set it to NULL
Fish *f1 = NULL;
//if old fish is alive, reproduce
if(f.alive)
{
f.Print();
f1 = f.Reproduce();
}
//if reproduction successful, print details of new fish
if(f1) f1->Print();
return 0;
}
OUTPUT
C++: questions are in fish.h. Write the code in fish.cpp and main.cpp #ifndef FISH_H #define FISH_H...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...
Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node { T value; Int_Node<T> *pre, *next; }; template <typename T> class Link_List { template <typename U> friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list template <typename U> friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...
// Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...
//header files #ifndef Contact_H // header files should always have this to avoid #define Contact_H // multiple inclusion in other files #include <string> // this is the only programming assignment which will use this statement. // normally "using namespace std" is looked down upon because it // introduces many common keywords that could be accidentally used, but // it identifies useful types such as string and would normally be used // std::string or std::vector. using namespace std; class Contact...
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...
//main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...
lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream; enum Token { // keywords PRINT, BEGIN, END, IF, THEN, // an identifier IDENT, // an integer and string constant ICONST, RCONST, SCONST, // the operators, parens, semicolon PLUS, MINUS, MULT, DIV, EQ, LPAREN, RPAREN, SCOMA, COMA, // any error returns this token ERR, // when completed (EOF), return this token DONE }; class LexItem { Token token; string lexeme; int lnum; public: LexItem()...