1) Bug Class Construct a user-defined object class named Bug that models a bug m
ID: 3711010 • Letter: 1
Question
1) 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, the bug’s position changes by one unit in the direction it is facing.
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 facing. A dir value of (positive) 1 indicates that the bug is moving to the right, and a value of -1 signifies that it is moving to the left. There are five member functions of the Bug class, described as follows.
a default constructor that initializes the bug’s position to 0 and dir to +1.
a constructor with a single argument that initializes the initial position of the bug to a value set by the user (the initial value of dir should always be +1)
voidmove() moves the bug one space in direction dir
voidturn() changes the direction the bug is facing
voiddisplay() displays the data members of a Bug object on the terminal screen or writes them to a file (see example below)
Write your bug class code in two files: an interface (or header) 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 to make sure the Bug class is only defined once.
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. Your main program should be in an application file, i.e., a separate file from your bug files. That application file should also have a directive#include “Bug.hpp”.
Here is a sample of what the output should look like.
Explanation / Answer
// ===========Bug.hpp ============//
#ifndef Bug_hpp
#define Bug_hpp
#include <stdio.h>
using namespace std;
//Interface
class IBug
{
public:
//A pure virtual function is specified by placing "= 0" in its declaration
virtual void voidmove()=0;
virtual void voidturn()=0;
virtual void voiddisplay()=0;
};
#endif /* Bug_hpp */
// .============= Bug.CPP ===============//
#include "Bug.hpp"
#include <iostream>
using namespace std;
class Bug:IBug{
//Data Member
int position,dir;
/*
Data functions
*/
//calculate and tract the position Data Member
public: void voidmove(){
position = position + dir;
}
//it changes the value of the dir from 1 to -1 or -1 to 1
public: void voidturn(){
dir = dir * -1;
}
//Display position and direction
public: void voiddisplay(){
cout<<"position = "<<position<<", "<<"direction = "<<dir<<" ";
}
//Default Constructor
public: Bug(){
position = 0;
dir = 1;
}
//Parameterized Constructor
public: Bug(int pos){
position = pos;
dir = 1;
}
};
int main(int argc, const char * argv[]) {
// Test code here...
Bug bug = Bug(10);
bug.voiddisplay();
bug.voidmove();
bug.voiddisplay();
bug.voidturn();
bug.voiddisplay();
bug.voidmove();
bug.voiddisplay();
return 0;
}
// ====================OutPut====================//
position = 10, direction = 1
position = 11, direction = 1
position = 11, direction = -1
position = 10, direction = -1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.