Arrays of Class Objects-Data Structure Programming-Lab Task Solution, Exercises of Data Structures and Algorithms

This is code assigned in lab of course. It was assigned by Jay Singh at Aligarh Muslim University for Data Structure course. It includes: Catch, Bug, Semicolon, Error, Function, Declared, Instance, Accessed, Class, Scope, Allocated, Array

Typology: Exercises

2011/2012

Uploaded on 07/11/2012

dhanvant
dhanvant 🇮🇳

4.9

(9)

89 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 3 Solutions
2 Catch that bug
2.1
Line 20 is missing a semicolon. doubleVal() cannot be applied to myPoint because myPoint
is const.
2.2
Line 11 contains an error because the function is declared const, i.e. as not modifying any
instance variables, but it assigns x to another value.
2.3
x and y are private members and cannot be accessed outside of the class.
2.4
setX is missing the scope; the function should be declared as void Point::setX(int newX)
{ x = newX; }
2.5
Deleting a dynamically allocated array requires delete[], not delete.
2.6
p is allocated using new, but is never deallocated with delete. Every piece of memory
allocated with new must be deallocated somewhere with a corresponding delete.
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Arrays of Class Objects-Data Structure Programming-Lab Task Solution and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Lab 3 Solutions

2 Catch that bug

Line 20 is missing a semicolon. doubleVal() cannot be applied to myPoint because myPoint

is const.

Line 11 contains an error because the function is declared const, i.e. as not modifying any

instance variables, but it assigns x to another value.

x and y are private members and cannot be accessed outside of the class.

setX is missing the scope; the function should be declared as void Point::setX(int newX)

{ x = newX; }

Deleting a dynamically allocated array requires delete[], not delete.

p is allocated using new, but is never deallocated with delete. Every piece of memory

allocated with new must be deallocated somewhere with a corresponding delete.

3 Point

3.1 geometry.h

1 class Point { 2 int x , y ; 3 4 public : 5 Point ( int xx =0 , int yy =0) { x = xx ; y = yy ;} 6 int getX () const { return x ;} 7 int getY () const { return y ;} 8 void setX ( const int xx ) { x = xx ;} 9 void setY ( const int yy ) { y = yy ;} 10 };

Note: the getX and getY functions should really have been declared as const, but we

neglected to ask you to do this, so it’s fine if you did not.

4 PointArray

4.1 geometry.h

class PointArray { int size ; Point * points ;

void resize ( int size ) ;

public : PointArray () ; PointArray ( const Point pts [] , const int size ) ; PointArray ( const PointArray & pv ) ; ~ PointArray () ;

void clear () ; int getSize () const { return size ;} void push_back ( const Point & p ) ; void insert ( const int pos , const Point & p ) ; void remove ( const int pos ) ; Point * get ( const int pos ) ; const Point * get ( const int pos ) const ; };

44 void PointArray :: push_back ( const Point & p ) { 45 resize ( size + 1) ; 46 points [ size - 1] = p ; 47 // Could also just use insert ( size , p ) ; 48 } 49 50 void PointArray :: insert ( const int pos , const Point & p ) { 51 resize ( size + 1) ; 52 53 for ( int i = size - 1; i > pos ; i - -) { 54 points [ i ] = points [i -1]; 55 } 56 57 points [ pos ] = p ; 58 } 59 60 void PointArray :: remove ( const int pos ) { 61 if ( pos >= 0 && pos < size ) { // pos < size implies size > 0 62 // Shift everything over to the left 63 for ( int i = pos ; i < size - 2; i ++) { 64 points [ i ] = points [ i + 1]; 65 } 66 resize ( size - 1) ; 67 } 68 } 69 70 Point * PointArray :: get ( const int pos ) { 71 return pos >= 0 && pos < size? points + pos : NULL ; 72 } 73 74 const Point * PointArray :: get ( const int pos ) const { 75 return pos >= 0 && pos < size? points + pos : NULL ; 76 }

1. We need the const versions so that we can return read-only pointers for const PointArray

objects. (If the PointArray object is read-only, we don’t want to allow someone to

modify a Point it contains just by using these functions.) However, many times we

will have a non-const PointArray object, for which we may want to allow modifying

the contained Point objects. If we had only const accessor functions, then even in

such a case we would be returning a const pointer. To allow returning a non-const

pointer in situations where we might want one, we need non-const versions of these

functions, as well.

5 Polygon and friends

5.1 Polygon

5.1.1 geometry.h

1 class Polygon { 2 protected : 3 static int numPolygons ; 4 PointArray points ; 5 6 public : 7 Polygon ( const PointArray & pa ) ; 8 Polygon ( const Point points [] , const int numPoints ) ; 9 virtual double area () const = 0; 10 static int getNumPolygons () { return numPolygons ;} 11 int getNumSides () const { return points. getSize () ;} 12 const PointArray * getPoints () const { return & points ;} 13 ~ Polygon () { - - numPolygons ;} 14 };

5.1.2 geometry.cpp

1 int Polygon :: n = 0; 2 3 Polygon :: Polygon ( const PointArray & pa ) : points ( pa ) { 4 ++ numPolygons ; 5 } 6 7 Polygon :: Polygon ( const Point pointArr [] , const int numPoints ) : points ( pointArr , numPoints ) { 8 ++ numPolygons ; 9 }

5.2 Rectangle

5.2.1 geometry.h

1 class Rectangle : public Polygon { 2 public : 3 Rectangle ( const Point &a , const Point & b ) ;

2 public : 3 Triangle ( const Point &a , const Point &b , const Point & c ) ; 4 virtual double area () const ; 5 };

5.4 geometry.cpp

1 Triangle :: Triangle ( const Point &a , const Point &b , const Point & c ) 2 : Polygon ( updateConstructorPoints (a , b , c ) , 3) {} 3 4 double Triangle :: area () const { 5 int dx01 = points. get (0) -> getX () - points. get (1) -> getX () , 6 dx12 = points. get (1) -> getX () - points. get (2) -> getX () , 7 dx20 = points. get (2) -> getX () - points. get (0) -> getX () ; 8 int dy01 = points. get (0) -> getY () - points. get (1) -> getY () , 9 dy12 = points. get (1) -> getY () - points. get (2) -> getY () , 10 dy20 = points. get (2) -> getY () - points. get (0) -> getY () ; 11 12 double a = std :: sqrt ( dx01 * dx01 + dy01 * dy01 ) , 13 b = std :: sqrt ( dx12 * dx12 + dy12 * dy12 ) , 14 c = std :: sqrt ( dx20 * dx20 + dy20 * dy20 ) ; 15 16 double s = ( a + b + c ) / 2; 17 18 return std :: sqrt ( s * (s - a ) * (s - b ) * (s - c ) ) ; 19 }

5.5 main.cpp

1 # include < iostream > 2 using namespace std ; 3 4 # include " geometry. h " 5 6 void printAttributes ( Polygon * p ) { 7 cout << " p ’s area is " << p - > area () << " .\ n " ; 8 9 cout << " p ’s points are :\ n " ; 10 const PointArray * pa = p - > getPoints () ; 11 for ( int i = 0; i < pa - > getSize () ; ++ i ) { 12 cout << " ( " << pa - > get ( i ) -> getX () << " , " << pa - > get ( i ) -> getY () << " ) \ n " ; 13 }

16 int main ( int argc , char * argv []) { 17 cout << " Enter lower left and upper right coords of rectangle as four space separated integers : " ; 18 int llx , lly , urx , ury ; 19 cin >> llx >> lly >> urx >> ury ; 20 Point ll ( llx , lly ) , ur ( urx , ury ) ; 21 Rectangle r ( ll , ur ) ; 22 printAttributes (& r ) ; 23 24 cout << " Enter three coords of triangle as six space separated integers : " ; 25 int x1 , y1 , x2 , y2 , x3 , y3 ; 26 cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 ; 27 Point a ( x1 , y1 ) , b ( x2 , y2 ) , c ( x3 , y3 ) ; 28 Triangle t (a , b , c ) ; 29 printAttributes (& t ) ; 30 31 return 0; 32 }

5.6 Questions

1. If the constructors were private, then we would not be able to create any Point objects.

2. When a Polygon is destroyed, the counter for number of Polygons created is decre

mented, and the PointArray’s destructor is implicitly called.

3. We had to make the fields of Polygon protected so that they could be accessed from

Rectangle and Triangle, but not by arbitrary outside code.

4. The getNumSides from Polygon would be called, because the function is not virtual.

6 Strings

1 const string vowels = " aeiou " ; 2 3 string pigLatinify ( const string s ) { 4 if ( s. size () == 0) { 5 // oops , empty string 6 return s ; 7 } 8

MIT OpenCourseWare http://ocw.mit.edu

6.096 Introduction to C++

January (IAP) 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.