0 like 0 dislike
1.5k views

Exercise 1

Using the file point.cpp (Click to download), add a member function print to the class Point so that it prints out coordinates as:

	(2, 3)

for a point with coordinates (2, 3). Test it in main().

Exercise 2

Write a definition of a class Rectangle using the Point class. A rectangle is specified by two corner points (bottom left and top right). The sides of the rectangle are parallel to the coordinate axes. The implementation of the class should be as follows: 

  • The private data members of the class include all 4 corner points of the rectangle.
  • There are two constructors: one takes two points as arguments and creates a rectangle with the first point as the bottom left corner and the second as the top right corner, the other (default) constructor creates a rectangle with the corners (0,0), (1,0), (0,1), (1,1). 
    Hint: use functions Set_X, Set_Y from the class Point to set the values of the corner points.
  • The print member function prints all 4 corners of the rectangle, using the member function print of the class Point.

Test the class Rectangle in main(), demonstrate that all member functions work as specified.

[Exercise] Essay (Open question) - asked in C++
ID: 24536 - Available when: Unlimited - Due to: Unlimited
| 1.5k views
0 0
Called for Help
0 0
Called for Help
0 0
Called for Help

20 Answers

0 like 0 dislike
Hidden content!
* * * *** * *** ** ** * ** * * ** * *
answered by (126 points)
0 like 0 dislike
Hidden content!
// file *

// used as part of *** * lab

// * ** **** ** Point

// *** **** * *** ** * * to * * class ** **

// * ****** *** * ** * * to test * *


** * *** * *



// * *** *** * **** *** ** * ** * ** * *** *

// * * *** ** of class Point

// * **** * ***** *** * * * * * * *



// class Point ** ** a ** * * point
** Point

{
**
* * * * * *** class ***** (with no ** * *
* ** ** * * * **


******* ***** class * ** that sets the * **** ** x, y to the * xval,
* * **** yval:
* *** * **** * * * * int * * **


* ** * * for a point by dx, dy:
* ** * * * ** * * dx, int dy);


***** * * * ** **** for * * ** of x, y:
* * **** ** ** **
* * * * ** ** *


** * ** * * * for * * x, y to xval, yval *** * *
* ** * * ** *** **
* * * * * *


* ** 1. Add ** of ** ** * Print here:
* *** ** **** * *



// *** data ** x, y * ** * of the * *
*** *
*** * * * ** X;
** * *** * Y;

};



// **** * ** * ********* ********* ***

// * *** for class ** *

// * ***** *** * ** * ** **** * *



// class ***** ** * sets X, Y to zero when no ** ** are * **
** ***

{
** * * * = 0;
* * * * * = 0;

}



// class * ** sets X, Y to given ** * xval, yval:
* ** **** xval, int yval)

{
* * ** * * = xval;
*** ** * * *** = yval;

}



// * ** ** Move: * **** the x * * by dx and the y

// * * * by dy.

void * ** * dx, int dy)

{
**** **** * += dx;
*** *** += dy;

}



// Get_X * * the value of the X * *

int ***** ** const

{
**** * X;

}



// Get_Y ** the value of the Y *******

int * ** * const

{
* * ***** ****** Y;

}



// Set_X sets the value of X ** ** to xval

void * *** * xval)

{
** ** * * = xval;

}



// Set_Y sets the value of Y ** to yval

void *** * ** yval)

{
* **** * = yval;

}


*** ** ** 1. Add * * of * * ** *



// * * ***** * * ******* *** * * * ******** **

// * **** * of class ** *

// * * *** * ** *** * ******** *** *

void * ****

{
** * ** * *** * ***** * * * ** ** ** ******* ** *** * ** * * ** ** * * * * ** * *** ** *** *

}


****


* * * 2: Add class * * *** HERE:


**** * *

{
**
* *** * ** ** * * * * ** *
** * ** **** *** * * * *
*** * *** * *** *** ** * ** *
** ** * * * * * ** ** * *
** * * *** ** * ** ** *** ** **
* * *** *** ** ** *** * ** *** *
* ** * **** * * *** * ** ***
** * ** **** * * ** * ** **

** **** ** ** * **** **
*** ** ** * ** * **** ** blcb, int trca, int trcb, int c1, int c2, int d1, int d2);
*** * ** * ****** ** ** * ** *
*** ***** * * * ** *** ** ** * * *
** ** * ** *** ** * * * ***

};



// ** ** ** * *** * * * * *

// * *** * for class * **

// * ** ***** ****** **** ** * * * **
* ** ** * * *

{
* ** ** ** ** *** **** ** *** = 0;
* ** * * * ** ** * *** = 0;
** ** * ** * **** ******** * = 0;
** *** * ** * ** * ** = 0;
* *** * ** ** * **** **** = 0;
* ** * * ** * * **** = 0;
** *** **** ***** ** = 0;
* **** * * ** * ** ****** * = 0;

}


* *** * *** * int ** int trcb, int c1, int c2, int d1, int d2)

{
* **** * *** ** * ** *** * = blca;
* * *** * ** * * ****** ** * = blcb;
* * * * **** * * ** = trca;
* *** * *** * ** = trcb;
***** ** * *** ** ***** = c1;
* * *** * ** * ** ** * * * = c2;
* ** * ** * * = d1;
*** * * *** *** * * ** = d2;

}



void *** *** xval)

{
* * *** * ** = xval;
* * ***** * * = xval;

}

void ** * ** * yval)

{
* * *** * * = yval;
* ** * ** = yval;

}

void ** ** * ** *

{
** ** * * ***** *** * ****** * ***** * * ** ** * ** * * ** ********* * ** *** *** ** *** ** * * * * * * * ** ** **** *** * ***
* * *** * *** ** * *** * ** * * ** **** ** * * *** * *** ** *** * * * *** * ** ** * * **
*** **** ** * **** *** * *** * * *** ***** ** * * *** * *** **** * *** * * * *** *** ** ** * * * * ** ** ***** * **
**** * * *** * * * * * ** ****** * * * ** ** * ** * * ***** *** ** * * ** *** * * *** * * * * * * * ** ** * *

}
** ** 2. Add class * * * ** HERE:


* *****



// ****** ** * * *** ** *** *** *** * **

// * * * * for ** * ** ** and * * ***

// * * ** *** ** * *** * * *** ** * * * * * *


******



// * ** Point and *** *

int * *

{
* * * ** * * ** **** *** * * a point * ** * class *** ** * (x = y = 0):
* *** *** ** ** ** p1;
* **** *** * * ** *** *** * * * ** ** * * **** x value for p1 is ** * * * * * * ** * * * *** *** ***
* ******** **** ** * ** * * * * * * ** * ** y value for p1 is ** * ** ** ** ** ** * * *


***** * ** * ** * * * **** * * * a point with ** ** X = 2, Y = 3:
* * ** ** * p2(2, 3);
*** **** * * * ** * **** * * * * * ** x value for p2 is * * * * * * * * ** ** ** * * ** *
**** * *** ** * ** **** * ** ** * * * * ** * ** y value for p2 is **** ** ** * *** ******* * *** **


** * *** * * ***** * ** point p2 by (1, -1):
* ** * * *** * *** ***** * -1);
*** ****** **** * * * ** ***** * * * **** *** the *** ** * ** * *
** ** * * * ** ** ** ***** * * ** ** x value for p2 is ** * ** ** **** *** ** ** * * * * ** *
***** *** ** ** **** ** ***** ** ** **** ** * * y value for p2 is ** * * * * * **** ** ** ** * ** * ****


** * * ***** * * * * * * * 1. Test ** * * print on ** p1, p2:
* ** *** * **** * ******** ** **
***** *** ***** * ** ** * * * ** * **
***** ** ************ *
* * ***** * **** *** ** ** **** * *** **
** * * * ** **** * ** ** 2. ** of the * * *** goes here:


* * ** * **** **** *** **** r1;
* * * ** * *** *** * **** ** * ** for r1 is *** ** ** *** ***
* * ** ** ***** *** *** *


***** * * *** ** * *** * ** * * *** * * *****
** * ****** * * *** ** * ***** *** * * ** *
* ** ******** * * **** * ** * **** * * * * **** for r2 is * * * * *** * **
* * *** * * * * ***


*** * * * ** * * * *** * 0;

}


**** * ** **** * * * ** ** * *** **
answered by (126 points)
0 like 0 dislike
Hidden content!
* ** * ** * ** * * *** *** * * *
answered by (-215 points)
0 like 0 dislike
Hidden content!
// file * * *

// used as part of **** lab

// * * * * * Point

// ** *** *** ** to * class * ***

// ******* *** *** * to test * *


** * ** * * * **



// *** ** * * * ** * ****** * **** **

// ** * ** * of class Point

// * ** ** * **** * ****** *** * * **** * *



// class Point ** *** a *** point
* Point

{
* **
* * * **** * * class * (with no *** *
* * ** ***


* ** *** * class *** that sets the **** x, y to the * xval,
** * * *** *** * yval:
** *** * * * * * int * **


*** * ** * * * *** for ** a point by dx, dy:
* * * * ** * * dx, int dy);


* * ** * * * * * for ** *** of x, y:
* * * * ** * * ** *
*** * ** * ** ** *


* *** ** ** for ** x, y to xval, yval * *
* *** * **** *** ** * **
** *** *** ** ** ** ***


* * ** 1. Add **** of * * ** ** Print here:
* * ***** * * * * **



// * * * data * * x, y * * * * * * * of the * *
* *
** * **** *** X;
* * ** * * Y;

};



// ** **** ** *** *** *** ** ** * * * **

// **** * * * for class

// ***** *** ** * * ** *** **** * * ***



// class * * * sets X, Y to zero when no * are ** * *
***

{
*** ** ** = 0;
** * * *** * = 0;

}



// class * ** sets X, Y to given * * xval, yval:
** * xval, int yval)

{
* ******* * * = xval;
* ** **** * ** = yval;

}



// * * Move: **** the x **** by dx and the y

// *** by dy.

void ** * dx, int dy)

{
* * * * * **** += dx;
* * * *** += dy;

}



// Get_X * the value of the X *

int * ** const

{
** * ** * X;

}



// Get_Y **** the value of the Y *

int * ****** const

{
*** * ** * * Y;

}



// Set_X sets the value of X ** ** to xval

void * * * xval)

{
** * * ** = xval;

}



// Set_Y sets the value of Y **** * to yval

void * * ** yval)

{
* ** ** * * = yval;

}


* *** * 1. Add ** * of * * * *



// ** * **** * **** * * * ** ** ** * * *

// * * *** of class * ****

// * * ** ****** *** ** * * ***** ** **

void ***** * *

{
* * * ****** * ** ** * * * ** * * ** *** * ** ** * * *** ***** ** * * * * * ***** ****** ** * * **** *

}


* ****


*** *** 2: Add class * ** ** HERE:


* ** ***

{
**
** * ***** ** * * * * ** * *
**** *** ** **** ** ***
*** * * *** **** * *
***** *** * * * ** * ** **
** * ** ***** * ** * ** ****
* * ** ******* ** ** ** *
** ** *** * ** * * * ** ** * * *
**** ** * ** ** *** * *****
*
*** *** ** * ** **** ** * ** *
* * * * ******* * * **** * ** ** *** blcb, int trca, int trcb, int c1, int c2, int d1, int d2);
* * * * * * ** * ***
** ** ** ** ** * ** *** **
** * * *** *** * *** ** * *** *

};



// * *** * *** * *** ** * * ** *

// ** ** *** for class * **

// * * ** ***** * * ** * ** ** *
** * *** * **

{
**** ******* ** ** * ** * = 0;
***** *** * * ******* ** ** * = 0;
** *** ** * * * ** * * * = 0;
* * * *** *** ** * = 0;
* * * ***** **** *** * * ** = 0;
* * ** * * **** ****** * * ** = 0;
* * * * **** ** *** * = 0;
* * ** * * * * * * * = 0;

}


** ** ****** * * * *** * * int ** * int trcb, int c1, int c2, int d1, int d2)

{
** * * *** * * **** * * ** * = blca;
**** ** * ** ** * = blcb;
** * * * * * *** ** ****** ** = trca;
*** * * ** ** * *** = trcb;
* ** * * * * **** * * *** ** = c1;
** * ** * * * *** * * = c2;
**** * ** *** * *** * * = d1;
**** * * ****** ** *** * * = d2;

}



void * * * ** xval)

{
* **** *** ****** = xval;
* * *** ** * = xval;

}

void * * * * yval)

{
** * ***** = yval;
* * *** * = yval;

}

void * *****

{
** * * * *** *** *** * * ********* * ** *** *** ** ******* * * * ** * *** *** * ** ** ** *** * ** ********* * *****
****** *** * * ** ** *** * * * * * * **** * * * **** * ** ****** * * * * * * * ******** *** * ** ** * * * * ****** * *
* * ***** *** ** * * **** * * ** * * * * * ** ** ** * * *** ** ******* ** * **** * ** ** * ***
* *** * * * ** * ******* *** ** * * * * * * * **** ** ** * ***** ** ** * *** ***** *** **** ** **** ** * ** **** ** ***** *

}
* * 2. Add class ** ** ** HERE:


* **** *



// **** ** * ** ** * * **** * ** **

// * * for * * *** * and ** ****

// * *** * **** * *** ** ** * * **


** *



// * * * Point and ** *

int

{
** * ***** * ** * * **** a point ** * * class * * (x = y = 0):
* ** * * *** * * * p1;
** ** ** * * * **** **** ** * ***** x value for p1 is * * * ** * * *** * * * * * * * *
* * * ***** *** **** * * * * **** ** ** ** y value for p1 is ** * * * *** * ** ** * ** * ** * **


* * ***** * ** * * * * * ** a point with *** * X = 2, Y = 3:
** *** ** * **** * ** * p2(2, 3);
* * ** * * ***** * * *** * * * x value for p2 is * * * *** * ** * * * **** * * ***
** * * *** * * * ** * * ** ** * * y value for p2 is * * * * ** * *** * * ** ** * ***


* ****** * ** * *** ** ** ** point p2 by (1, -1):
* * ***** * * * * * * -1);
* ** ***** * * **** * * * * ** ** ** * ** *** the * ** ** **** * * * ***
** *** * ** ** ***** * * * * * ** *** * ** ** x value for p2 is ** ***** *** * ***** * *** * **
*** * ** *** ** **** * *** * ** y value for p2 is ** ** * * *** * * ** * **** ** *


** * * ** * * * **** * * * 1. Test * ** ** * print on * * p1, p2:
** *** * *** ****** ** * * **
* *** * * * *** ** * *** *** *** * * *
** * * ******* * * * *
* *** * *** * ** ***** *** * ** ** ** * * *
*** ** *** * * * **** * *** * 2. * * * of the * * ** goes here:


* * * ** **** * * * * ***** ** ** r1;
* * * ** * * * ** * ** * **** * ** * ** *** for r1 is ** * * *****
*** ******* * ** * ****** *** *


* * * * * * * * ** * *** *
** *** * * *** * ** * * * * * * *
***** * *** * * * ** * ** * ** * for r2 is * ** *** *** **
* ** * * * ** * * * *


* ** * * ****** ** * 0;

}


* * * **** * * ** * ***** ** *** **
answered by (-215 points)
0 like 0 dislike
Hidden content!
* ***** * * * * ** * * ** **** ** ****** *
answered by (237 points)
0 like 0 dislike
Hidden content!
****** ** *** *



using * std;



class Shape



{


*


* *** * * * * ** width, **


*


* ** *** ** * * * * * ***** ** * (float a, float b)


** **** * * * ** ** **


**** ** *** * * ** ************* * * ** ****** ** = a;


*** *** *********** * * * * ** ******* * * = b;


* * * ** * ****** ******



};



class * ** public Shape



{


***


** ** * * *** ** area ()


* * *** ** *** *** * ***


* * * * * *** ** *** ** * *** *** * ** **** (width *


** ** * *** ** *** *** **



};



class * ** public Shape



{


***


* ******** * ** ***** area ()


* ******* * * * * * *


* ** * ** ***** * ** ***** * * ** * * (width * height / 2);


** **** ** ****



};



int main (){


***** ** ** **


* **** * * * ** **** ** rect;


*** *** * *** *** *** * tri;


*** * * * * * * ***** * **** * (5,3);


* **** ***** * * * (2,5);


* * * *** * * ** * ** *** * * * * *** * **** ** * * ** * *** *** * * ******* endl;


* * * ** *** ***** * * * * 0;



}
answered by (-215 points)
0 like 0 dislike
Hidden content!
**** ** * *** ******



using * ** std;



class Shape



{


**


** * ** * * * **** width, *


*


***** *** * ** * *** (float a, float b)


*** *** *** * *


*** * ** ** * *** * * ***** * ** ** ** * ** = a;


** * *** * ** * ** * *** ** * ******* * ****** = b;


****** ** ****** * * **



};



class * * public Shape



{


*****


**** *** **** ****** ** area ()


** ** *** **** * * **


* **** * * **** ******** * * * * **** *** * (width * *


***** * ****** * * *



};



class ** * public Shape



{


**


******** * * * *** **** *** area ()


* ** ***** ***** *


***** ** ** * * ***** * ** *** * * *** * (width * height / 2);


* ***** * ****** * ****



};



int main (){


* *** *** * * *


** * * * * * *********** rect;


***** ** *** * * **** tri;


*** **** **** * ** * * ** * * (5,3);


* **** *** * ** **** ** * **** (2,5);


*** **** * ** ** * * * ** ** * * * * **** * * **** *** * * ** *** * * * *** * endl;


* ***** * * * ** ** 0;



}
answered by (-215 points)
0 like 0 dislike
Hidden content!
#include * *** * * *



using ** * std;



class Shape

{
***
** ** * ** * ** * * * * width, height;

public:
* ** * *** ** * * ** set_data (float a, float b)
*** * ** * * * ** ** **
* * * ** * *** * ****** *** ** *** ** * = a;
* * * * *** * *** * * *************** * * = b;
** ** *

};



class ** * public Shape

{

public:
* * **** * * ** ** area ()
* ** * * * ***** ***** *
* * ** ** * * ** *** * * ** ***** ** * *** * *** ** *** (width * height);
** * ** * * * **

};



class * **** public Shape

{
* *
* *** ** * * * * * * area ()
*** **** **** * * * **** *
* **** *** ** ** * ** **** ***** ** * * * ** ***** * (width * height / 2);
* ***** ***** * *** * *

};



int main (){
** * * * * ** * ***
** * * * *** ***** *** * rect;
********* * ** *** ** * * tri;
* * * * * * * * ** ***** (5,3);
* *** *** * * * * * **** ** (2,5);
* ** * * ***** * ****** *** * * ** ***** * * ** ** **** ***** ** * **** * * * * * endl;
*** *** * * ** * ** * * 0;

}
answered by (126 points)
0 like 0 dislike
Hidden content!
* * **** * ** * ********** ** *** *** **
answered by (-134 points)
edited by
0 like 0 dislike
Hidden content!
* *** ** *** * * * * * *** * * ** ***** **
answered by (-368 points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.71.254.94
©2016-2024

Related questions

0 like 0 dislike
27 answers
[Exercise] Coding (C) - asked Apr 27, 2017 in C++
ID: 24389 - Available when: Unlimited - Due to: Unlimited
| 1.8k views
0 like 0 dislike
23 answers
[Exercise] Coding (C) - asked May 11, 2017 in C++
ID: 24589 - Available when: Unlimited - Due to: Unlimited
| 1.7k views
0 like 0 dislike
26 answers
[Exercise] Coding (C) - asked May 11, 2017 in C++
ID: 24587 - Available when: Unlimited - Due to: Unlimited
| 1.5k views
0 like 0 dislike
30 answers
[Exercise] Coding (C) - asked May 18, 2017 in C++
ID: 24769 - Available when: Unlimited - Due to: Unlimited
| 1.8k views
0 like 0 dislike
17 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24855 - Available when: Unlimited - Due to: Unlimited
| 1.2k views
12,783 questions
183,443 answers
172,219 comments
4,824 users