Question
C++
IJU U Construct a user-defined object class named Bug that models a bug moving along a horizon- tal line. The bug moves eithe
For example, if you named your application file str1.cpp, then youd use : g++ Bug.cpp str1.cpp ... to compile your code for
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


Bug.hpp
======
#ifndef BUG_HPP
#define BUG_HPP
class Bug
{
   private:
   int position;
   int dir;
   public:
   Bug();
   Bug(int pos);
   void move();
   void turn();
   void display();
};
#endif


Bug.cpp
======
#include "Bug.hpp"
#include <iostream>
using namespace std;

Bug::Bug(){
   position = 0;
   dir = 1;
}

Bug::Bug(int pos){
   position = pos;
   dir = 1;
}

void Bug::move(){
   position = position + dir;
}

void Bug::turn(){
   dir = -dir;
}

void Bug::display(){
   cout << "position = " << position << ", direction = " << dir << endl;
}


TestBug.cpp
=======
#include <iostream>
#include "Bug.hpp"
using namespace std;

int main(){
   Bug b(10);
  
   b.display();
  
   b.move();  
   b.display();

   b.turn();
   b.display();

   b.move();
   b.display();
  
   return 0;
}

  

E:\ws\vscode HomeworkLib\raji\test>a.exe position = 10, direction = 1 position = 11, direction = 1 position = 11, direction = -1 po

Add a comment
Know the answer?
Add Answer to:
C++ IJU U Construct a user-defined object class named Bug that models a bug moving along...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Write a class bug that models a bug moving along a horizontal line. The bug...

    Python Write a class bug that models a bug moving along a horizontal line. The bug moves either to the right to 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. Provide a constructor def__init__(self, initial position) and methods . def turn(self) . def move(self . def getposition(self) Sample usage: bugsy = bug(10) bugsy.move() # Now the position is 11...

  • ** MUST BE PROGRAMMED IN PYTHON** Thank you. 1. Write a class bug that models a...

    ** MUST BE PROGRAMMED IN PYTHON** Thank you. 1. Write a class bug that models a bug moving along a horizontal line. The bug moves either to the right to 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. Provide a constructor def__init__(self, initial position) and methods . def turn(self) . def move(self . def getposition(self) Sample usage: bugsy =...

  • *using c++* Construct a class named Savings containing three floating-point data members named balance, rate, and...

    *using c++* Construct a class named Savings containing three floating-point data members named balance, rate, and interest and a constructor that initializes each data member to 0. This class should be established with typical two files(.h and .cpp). Include a member function that inputs a balance and rate and then calculates an interest. The rate should be stored as a   percent, such   as   6.5   for   6.5%,   and   the   interest   is   computed   as   interest   = (balance)(rate/100). Add a member function to...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • Construct a class named Room that declares two double-precision data members named length and width (2...

    Construct a class named Room that declares two double-precision data members named length and width (2 points). Include a constructor that initializes each data member to 1.0 when a Room object is created (1 point). Add two accessor functions for displaying the length and width values of a Room object and two mutator functions that allow changing these data member values. (2 points) Write a program that tests all functionality of this class. (3 points) Extra Credit: Create this class...

  • Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part...

    Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part 1: In this assignment, you are asked: Stage1:?Design and implement a class named memberType with the following requirements: An object of memberType holds the following information: • Person’s first name (string)?• Person’s last name (string)?• Member identification number (int) • Number of books purchased (int)?• Amount of money spent (double)?The class memberType has member functions that perform operations on objects of memberType. For the...

  • Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has...

    Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour and minute data members. Write a single C++ statement to create an object of the Timer class using dynamic memory allocation and assign it to a pointer variable named timePt r. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. b) Write the definition...

  • Create a class named BankAccount with data fields for a count number and a balance. Include...

    Create a class named BankAccount with data fields for a count number and a balance. Include a constructor that takes arguments for each field. The constructor sets the balance to 0 if it is below the required $200.00 minimum for an account. Include a method that displays the account details, including an explanation if the balance was reduced to 0. Write the class TestAccount in which you instantiate two BankAccount objects, prompt a user for values of the account number...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT