讲解:C++ Homework 2C/C++、Shape

#include “Shape.h”#include #include using namespace std;int main(){Shape t1, t2;char ch;int x,y;try{cin >> ch >> x >> y;t1 = Shape::makeShape(ch,x,y);t1->print();cin >> ch >> x >> y;t2 = Shape::makeShape(ch,x,y);t2->print();t2->move(1,-1);t2->print();if ( t1->overlap(t2) )cout elsecout delete t1;delete t2;}catch ( invalid_argument exc ){cout }}所有用例均已测试完毕编译命令:make执行命令./testShape RequirementECS40 Winter 2017 2017-02-08Homework 2 due Fri 2017-02-17 at 20:00Use the handin directory hw2 to submit your workShape editorNote: the concepts and software developed in Homework2 may be used in Homework3.DescriptionIn this assignment you will write a program that manipulates geometric shapes.We will consider the following set of shapes, named O, I, L, S, X, U (the names are reminiscentof the shapes). We can represent shapes by drawing the squares forming the shape as shown inFigure 1:Figure 1The cells (or squares) forming a shape are indexed by a number starting from 0, as shown in Fig1. A shape can be placed on a board by specifying the position of its “zeroth cell” in the x-yplane. In Figure 2 below, shapes have been placed on a board as follows:Figure 2- A shape of type “L” placed at (0,7)- A shape of type “L” placed at (5,8)- A shape of type “I” placed at (9,6)- A shape of type “X” placed at (5,4)- A shape of type “O” placed at (8,5)- A shape of type “U” placed at (0,2)- A shape of type “O” placed at (1,0)- A shape of type “S” placed at (4,0).- A shape of type “S” placed at (7,1).HW2 AssignmentThe goal of HW2 is to implement classes representing shapes. A given program will use thisclass to create shapes at arbitrary locations in the x-y plane and move them. The program uses afunction to move shapes and to detect if shapes overlap. The board is assumed to be the entire x-y plane.The class Shape is an abstract base class from which six classes are derived, named: O, I, L, S,X, U. A detailed specification of the class Shape is given below. You are given the header fileShape.h (at ). Your task is to write theimplementation file Shape.cpp containing the implementation of all classes. You are notallowed to modify the file Shape.h (Do not insert your name or SID in it since we will use thecmp command to check that the file is the same as the original). Your class implementation fileShape.cpp should compile without warning using the command$ g++ -Wall –c Shape.cppYou are also given a program testShape.cpp and a Makefile (same web site as above).These two files should not be modified. The testShape.cpp program tests the functionalityof the Shape class. You should be able to build the executable testShape using thecommand$ makeYou will use the testShape executable and the input aC++代写 Homework 2代写留学生C/C++课程设计、代写Shape问题作业nd output test files provided on the website to check the functionality of your classes and verify that your program reproduces the testoutput exactly. Use the diff command to compare your output file with the output file provided.Note that these test files do not check all the functionality and you are responsible for verifyingyour implementation. Other test files will also be used when grading your implementation. ThetestShape program reads input from stdin and writes to stdout. It can be used e.g. as$ ./testShape It can be assumed that input will consist of two lines. Each line consists of one characterfollowed by two integers. The character and integers are separated by white space as in e.g.X 2 -3Specification of the Shape classWe describe here the detailed specification of the Shape class. The positions of the cellsoccupied by a shape are stored in two integer arrays whose size depends on the shape, i.e. ashape occupies the cells located at (x[0],y[0]), (x[1],y[1]), etc…Study carefully the file Shape.h. Some members are public and some protected. Somemembers are virtual. Make sure you understand why. Study the file testShape.cpp in orderto understand how the classes will be used.Public membersvirtual char name(void) = 0;This pure virtual function returns the name of the class instance as a character, i.e. O, I, L, S, X,U.void print(void)This function prints information about the location of the shape by showing the position of thecells that the shape occupies:name at (x[0],y[0]) (x[1],y[1]) …For example, from Figure 2:X at (5,4) (4,5) (5,5) (6,5) (5,6)bool overlap(const Shape s)This function should return true if the shape calling the function overlaps with the shape s.This can be checked by comparing the arrays x and y of both shapes. If there is no overlap, i.e.no cells are shared by the two shapes, the function should return false.void move(int dx, int dy)This function moves the shape by a displacement (dx, dy). This function modifies the values ofthe arrays x and y. Note that this function is the same for all shapes and need not be implementedas a virtual function.static Shape makeShape(char ch, int posx, int posy)This function is a “shape factory”. It is used to generate instances of shapes. It creates an instanceof the appropriate kind of shape (specified by the character ch, with values ‘O’, ‘I’, ‘L’,‘S’,’X’, ‘U’) at the given position (posx,posy) using the operator new, and returns a pointerto the instance. If the character ch is not one of the allowed letters, the function should throw aninvalid_argument exception. The error printed by the program should correspond to theexample given in one of the test files.protected membersint x,yThe arrays holding the coordinates of the cells occupied by the shape.SubmissionCreate a tar file named hw2.tar containing the files Shape.h, Shape.cpp,testShape.cpp and Makefile. Do not compress the tar file. The files Shape.h,testShape.cpp and Makefile must be identical to the files provided. Include your nameand Student ID in a comment at the top of the Shape.cpp file. Submit your project using:$ handin cs40 hw2 hw2.tar& 转自:http://ass.3daixie.com/2018052454222858.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,724评论 0 3
  • Data Visualization with D3 D3: SVG中的jQurey 1. Add Documen...
    王策北阅读 787评论 0 2
  • 1 健康为本 一直前进的基本,是健康的身体。跑步、健身,一定要坚持 2 跟随时代 既要沉心读书,也要了解时事和新闻...
    墨鱼年轮阅读 229评论 0 7
  • 春游这个害羞的孩子,终于在一干学生娃的翘首以盼、千呼万唤里始出来,在孩子们三番五次询问无果里终有肯定答复咯。春游咯...
    爱琴海湾阅读 857评论 1 16
  • 2016年9月3日,职工运动会在长春市经开体育场隆重召开。职工运动会每四年召开一次,共XX家单位参与此项赛事。我公...
    _戒糖阅读 371评论 0 0