讲解: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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,099评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,828评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,540评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,848评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,971评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,132评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,193评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,934评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,376评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,687评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,846评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,537评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,175评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,887评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,134评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,674评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,741评论 2 351

推荐阅读更多精彩内容

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