Question

C++ PROGRAMMING

Hi! My assignment prompt is below. What I'm having the most trouble understanding is where the shapes are being stored. I'm assuming an array, but I'm not sure how sizing would work. Any help is appreciated, thanks! I have also attached the .h file we must use.

Prompt: The goal of HW2 is to implement classes representing shapes. A given program will use this class to create shapes at arbitrary locations in the x-y plane and move them. The program uses a function to move shapes and to detect if shapes overlap. The board is assumed to be the entire x- y plane.

Figure I: 0 1 2 3 1 2 3 4 0 1 2 Figure 1 The cells (or squares forming a shape are indexed by a number starting from 0, as shown in Fig 1. A shape can be placed on a board by specifying the position of its zeroth cell in the x-y plane. In Figure 2 below, shapes have been placed on a board as follows:

Shape.h:

//

// Shape.h

//

#ifndef SHAPE_H

#define SHAPE_H

class Shape

{

public:

virtual ~Shape(void);

virtual char name(void) const = 0;

virtual int size(void) const = 0;

void print(void) const;

void move (int dx, int dy);

bool overlap(const Shape &t) const;

static Shape *makeShape(char ch,int posx,int posy);

protected:

int *x, *y;

};

class O: public Shape

{

public:

O(int posx, int posy);

virtual char name(void) const;

virtual int size(void) const;

};

class I: public Shape

{

public:

I(int posx, int posy);

virtual char name(void) const;

virtual int size(void) const;

};

class L: public Shape

{

public:

L(int posx, int posy);

virtual char name(void) const;

virtual int size(void) const;

};

class S: public Shape

{

public:

S(int posx, int posy);

virtual char name(void) const;

virtual int size(void) const;

};

class X: public Shape

{

public:

X(int posx, int posy);

virtual char name(void) const;

virtual int size(void) const;

};

class U: public Shape

{

public:

U(int posx, int posy);

virtual char name(void) const;

virtual int size(void) const;

};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Virtual functions makes sense only in xclasses meant to act as interface to objects of derived class which are typically allocated on heap and accessed through pointers or references.

The shaping rework is performed in those area.

Add a comment
Know the answer?
Add Answer to:
C++ PROGRAMMING Hi! My assignment prompt is below. What I'm having the most trouble understanding is...
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
  • /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) {...

    /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...

  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • This is c++ programming and here is my code. I am getting an exception thrown on...

    This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private:    char *name;    int salary; public:    PermanentWorker(const char* nam, int money) : salary(money)    {        name = new char[strlen(nam) + 1];        strcpy(name, nam);    }    int getPay() const    {        return salary;   ...

  • I'm having trouble understanding pointers in my c programming class. I posted the pointer assignment below....

    I'm having trouble understanding pointers in my c programming class. I posted the pointer assignment below. If you could leave comments pointing out where pointers are used it would be much appreciated! ----------------------------------------------------------------------------------------------------------------------------------- Write a complete C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispenses are 50s, 20s, and 10s. Write a...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • I'm having trouble getting my program to output What is your first name? damion what is...

    I'm having trouble getting my program to output What is your first name? damion what is your last name? anderson damion, Your first name is 6 characters Your last name, anderson, is 8 characters Your name in reverse is: noimad nosredna This is my code so far: #include <stdlib.h> #include <stdio.h> void sizeOfName(char** name); void printSizeOfName(int *first, int *last, char** name); void reverseString(int *first, int *last, char** name); int main() {   char** name;   name = (char**)malloc(2*sizeof(char*));   name[0] = (char*)malloc(100*sizeof(char));   name[1]...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • A C++ code error:Conditional jump or move depends on uninitialised value(s) at 0x4013EC: sdds::Ship::set(char const*, sdds::Engine*,...

    A C++ code error:Conditional jump or move depends on uninitialised value(s) at 0x4013EC: sdds::Ship::set(char const*, sdds::Engine*, int) Here'are my class and set() function: class Ship { Engine* engine; char* s_type; int engineNo; float distance; public: Ship(); Ship(const char* sh_type, Engine* e, int e_No); ~Ship(); bool empty() const; float calculatePower() const; Ship& operator+=(Engine e); void display() const; void setEmpty(); bool isValid() const; void set(const char* sh_type, Engine* e, int e_No); friend bool operator==(const Ship&, const Ship&); }; void Ship::set(const char* sh_type,...

  • I'm also having trouble on dividing the code into methods. This is my work done so...

    I'm also having trouble on dividing the code into methods. This is my work done so far and net beans give me a lot of errors. Help! package caesarcipher; import textio.TextIO; /** * * @author */ public class CaesarCipher { enum code {encodeText, decodeText }; /** * @param args the command line arguments */ public static void main(String[] args) {    String code; char x;    int totalcount = 0;    while(true){ TextIO.put("Type in an E to encode a message...

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