Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming: Bug Class Construct a user-defined object class named Bug that models a bug moving along a horizontal line. The bug moves either to

C++ Programming:

Bug Class

Construct a user-defined object class named Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction.

The Bug class will have data members position (type int) for the bug's position on the horizontal line and dir (type int) for the direction the bug is going. A dir value of (positive) 1 indicates the bug is moving to the right and a value of -1 signifies that it is moving left. There are five member functions (functions inside the class):

a default constructor to initialize position to 0 and dir to +1.

a constructor with a single argument to initialize the initial position of the bug to a value set by the user (the initial value of dir should always be +1)

void move() to move the bug one space in dir

void turn() to change the direction the bug is moving

void display() to display the data members of a Bug object on the terminal screen or write them to a file (note example below).

Write your bug class code in two files: a interface (or declaration) file named Bug.hpp, and an implementation (or definition) file named Bug.cpp. Remember that the implementation file will need to include the directive #include Bug.hpp. Also use #ifndef, #define, and #endif in your Bug.hpp file.

When you have your bug files completed, write a test program to create a Bug object at position 10, move the bug, turn the bug, and move the bug again. At each position, display the bug's data members on the terminal screen. You main program should be in an application file, i.e., a separate file than your bug files. That application file should have a directive #include Bug.hpp. Remember that to compile your program you will need to include both the application file name and the file name Bug.cpp in the compilation command.

For example, if you named your application file str1.cpp, then youd use : g++ Bug.cpp str1.cpp ... to compile your code for this problem.

Here is what sample output looks like:

position = 10, direction = 1

position = 11, direction = 1

position = 11, direction = -1

position = 10, direction = -1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

Organize and support your main points

Answered: 1 week ago

Question

Show the properties and structure of allotropes of carbon.

Answered: 1 week ago

Question

1. How will you, as city manager, handle these requests?

Answered: 1 week ago