Question

In: Computer Science

Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...

Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written in Lion.cpp, which refers to the Lion.h using an include directive. The Lion class consists of the following components:

  • Properties (MUST be private):   
    • weight: double
    • height: double
  • Constructors:
    • No-argument constructor: the constructor with no parameter
    • Standard constructor: with the same number of parameters as properties.
  • Methods
    • For each private property, create one get() method and one set() method to provide reading and writing access to this property. Here, there should be two get() methods and two set() methods.
    • toPrint(): no return. It prints out the names and current values of all properties.  
    • eat(): no return, print with a complete sentence how this type of lions eat. For example, eat every 48 hours, eat meat only, or eat much more in rainy season.

The components of Pine class are defined in the Pine.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written in Pine.cpp, which refers to the Pine.h using an include directive. The Pine class consists of following components.

  • Properties (MUST be private):
    • age: int
    • height: double
  • Constructors:
    • No-argument constructor: the constructor with no parameter
    • Standard constructor: with the same number of parameters as properties.
  • Methods
    • For each private property, create one get() method and one set() method to provide reading and writing access to this property. Here, there should be two get() methods and two set() methods.
    • toPrint(): no return. It prints out the names and current values of all properties.
    • produceCone(): no return, print with a complete sentence how this type of pine tree grows its pinecone. For example, produce annually, or produce on new branches.

The TestLionPine.cpp files contains only the main function for testing the Lion class and Pine class. In the main() function, create two Lion objects and two Pine objects respectively. For each class, create one from the no-argument constructor, another from the standard constructor. Before calling the standard constructor, two values should be read from the user with proper prompts. At the end, call every method from each object. Preferably, follow the sequence below:

  1. Call the get() methods to display original property value.
  2. Call the set() methods to change property values. For each property, or for each set() method, a new property value should be read from the user with proper prompt.
  3. Call the toPrint() method to print the updated property values with proper prompt text.
  4. Call other regular methods.

Solutions

Expert Solution

Lion.h

#ifndef LION_H
#define LION_H

class Lion
{
    private:
    
    double weight;
    double height;
    
    public:
    
    Lion();
    
    Lion(double w, double h);
    
    double getWeight();
    void setWeight(double w);
    
    double getHeight();
    void setHeight(double h);
    
    void toPrint();
    
    void eat();
};

#endif

Pine.h

#ifndef PINE_H
#define PINE_H

class Pine
{
    private:
    
    int age;
    double height;
    
    public:
    
    Lion();
    
    Lion(int a, double h);
    
    int getAge();
    void setAge(int a);
    
    double getHeight();
    void setHeight(double h);
    
    void toPrint();
    
    void produceCone();
};

#endif

Lion.cpp

#include "Lion.h"
void Lion::setHeight(double h)
{
    height = h;
}

void Lion::setWeight(double w)
{
    weight = w;
}
    
Lion::Lion()
{
    setWeight(0);
    setHeight(0);
}

Lion::Lion(double w, double h)
{
    setWeight(w);
    setHeight(h);
}

double Lion::getWeight()
{
    return weight;
}

double Lion::getHeight()
{
    return height;
}

void Lion::toPrint()
{
    double w = getWeight();
    double h = getHeight();
    cout << "height : " << h << "\n";
    cout << "weight : " << w << "\n";
}

void Lion::eat()
{
    cout << "eat every 48 hours\n";
}

Pine.cpp

#include "Pine.h"
void Pine::setHeight(double h)
{
    height = h;
}

void Pine::setAge(int a)
{
    age = a;
}
    
Pine::Pine()
{
    setAge(0);
    setHeight(0.0);
}

Pine::Pine(int a, double h)
{
    setWeight(w);
    setHeight(h);
}

int Pine::getAge()
{
    return age;
}

double Pine::getHeight()
{
    return height;
}

void Pine::toPrint()
{
    int a = getAge();
    double h = getHeight();
    cout << "height : " << h << "\n";
    cout << "age : " << a << "\n";
}

void Pine::produceCone()
{
    cout << "produces cone annually\n";
}

TestLionPine.cpp

#include"Lion.h"
#include"Pine.h"

public static int main()
{
    cout << "Enter lion 2's weight";
    double w,h;
    int a;
    cin >> w;
    cout << "Enter lion 2's height";
    cin >> h;
    Lion L1;
    Lion L2(w,h);
    Pine P1;
    cout << "Enter pine 2's age";
    cin >> a;
    cout << "Enter pine 2's height";
    cin >> h;
    Pine P2(a,h);
    
    cout << "Lion 1's weight : " << L1.getWeight();
    cout << "Lion 2's weight : " << L2.getWeight();
    cout << "Lion 1's height : " << L1.getHeight();
    cout << "Lion 2's height : " << L2.getHeight();
    
    cout << "Pine 1's height : " << P1.getHeight();
    cout << "Pine 2's height : " << P2.getHeight();
    cout << "Pine 1's age : " << P1.getAge();
    cout << "Pine 2's age : " << P2.getAge();
    
    cout << "Enter lion 1's weight : ";
    cin >> w;
    cout << "Enter lion 1's height : ";
    cin >> h;
    L1.setHeight(h);
    L1.setWeight(w);
    
    cout << "Enter lion 2's weight : ";
    cin >> w;
    cout << "Enter lion 2's height : ";
    cin >> h;
    L2.setHeight(h);
    L2.setWeight(w);
    
    cout << "Enter pine 1's age : ";
    cin >> a;
    cout << "Enter pine 1's height : ";
    cin >> h;
    P1.setAge(a);
    P1.setHeight(h);
    
    cout << "Enter pine 2's age : ";
    cin >> a;
    cout << "Enter pine 2's height : ";
    cin >> h;
    P2.setAge(a);
    P2.setHeight(h);
    
    cout << "Lion 1's new properties:-\n";
    L1.toPrint();
    
    cout << "Lion 2's new properties:-\n";
    L2.toPrint();
    
    cout << "Pine 1's new properties:-\n";
    P1.toPrint();
    
    cout << "Pine 2's new properties:-\n";
    P2.toPrint();
    
    L1.eat();
    L2.eat();
    
    P1.produceCone();
    P2.produceCone();
    
}

Related Solutions

Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests the class implementation. The class definition and implementation should be separated into HumanBMI.h and HumanBMI.cpp files. A. The class HumanBMI consists of three private member variables: name of type string, height of type int in inches, and weight of type int in pounds. The class HumanBMI also includes the following public member functions: 1) setName to set the name member variable with a string...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a program in C that implements Conway's Game of Life. You will be expected to...
Write a program in C that implements Conway's Game of Life. You will be expected to apply the principles of Design by Contract to your code. The rules for the Game of Life are simple. The universe consists of a two-dimensional matrix of cells with each cell being alive or dead. For each generation every cell determines its next phase of life as follows: If the cell is alive: it dies if it has 0, 1, 4 or more living...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT