Вы находитесь на странице: 1из 4

 Implement a class to

Questions:
represent a rectangle
 What are the functions of this class?
 Use the declaration below as a guide
 What do we call functions of a class?
class Rectangle {
 What arguments do they accept?
public:  What are their return types?
void setWidthHeight (int,int);  What are the access specifiers?
int area (void);  Why do we use them?
int perimeter(void);
 Why do we need the setWidthAndHeight()
private:
method?
int width_, height_;
};  Can you see any problems with this class?
 Where should we place the
implementation of the class?
 Create an executable that
What do we need to alter in
uses the class to compute the

CMakeLists.txt to compile the executable?
area and perimeter of several
rectangles
Create a class for generating an array of random numbers (doubles)

 The class should:


◦ In Constructor
a) Accept a seed for a Random Number Generator
b) Number N - specifying how many random numbers are generated each time
◦ Have a Function
a) Accepts an array and populates the array with N numbers generated at each time

Write a program that uses this function to populate an array of numbers

Hint: http://www.cplusplus.com/reference/random/uniform_real_distribution/

Questions:
 What are the pinch points here, how to ensure the program does not cause a
segmentation fault?
 What is a segmentation fault?
 Why do we have N in constructor?
 Who is responsible for defining the max length of the array?
 Is there any way to make this allocation dynamic?
Write a function that accepts an Array of
Rectangles and returns the combined area of
all Rectangles in the array

Write a program that creates an Array of


Rectangles with random sides and uses the
function to computes the combined area

Hint:
Use the two previous examples to build upon
Use the declaration of rectangle class below to
work on polymorphism.
class Rectangle {
public:
Rectangle();
void setWidthHeight (double width, double height);
double area ();
double perimeter();
private:
double width_, height_;
};
How could we use polymorphism to allow the class
to be used for a square?

Вам также может понравиться