Starting from:

$25

CH-230-A-Assignment 11 Solved

Assignment 11 - Dynamic Memory Allocation and Inheritance

•    The problems of this assignment must be solved in C or C++ (instruction in each problem).

•    Your programs should have the input and output formatting according to the testcaseslisted after the problems.

Design and write an object oriented program for the computation of the volume of boxes (having a height, a width and a depth as double values). Your solution should have methods for setting and getting the height, the width and the depth of a box as well as constructors (a default constructor and one which sets the data members), a copy constructor and a destructor.

Name your files testbox.cpp, Box.cpp and Box.h.

The program testing your code (testbox.cpp) should do the following:

a)     Enter from the keyboard the a value for n, the number of boxes that will be entered.

b)    Dynamically create an array of 2∗n boxes.

c)     Enter from the keyboard the height, width and depth of each box one after the other for thefirst n boxes and the add their copies on the remainder n positions (in the same order). Use the copy constructor for doing this.

d)    Loop across all boxes, calculate and print on the screen the volume of each box.

You can assume that the input will be valid.

Problem 11.2 Inheritance with creatures

Each constructor, destructor and method should be implemented in such a way that each call is being documented via messages printed on the screen.

Create one instance of Wizard, two instances of the two other classes, and call for each object methods of the particular instances (as in the original example). You can assume that the setting values are always valid.

Problem 11.4 Inheritance and pointers

Change your previous testing program (testcreature.cpp) such that it runs in an endless loop and waits for input from the keyboard. If the word ”wizard”, ”object1”, or ”object2” is entered, a wizard, your other object1 or your other object2 should be dynamically created (via new), the corresponding method is being called and the object is then destroyed (via delete). Entering “quit” should stop the execution of the program.

Name the files Creature.h (same as above), Creature.cpp (same as above) and dyncreature.cpp.

You can assume that the input will be valid.

Implement the following:

a)    default constructors for all classes by initializing the properties to default values,

b)    setters and getters for all classes,

c)    the constructors Rectangle(const string& n, double nx, double ny, double nwidth,

double nheight) and Square(const string& n, double nx, double ny, double nside), d) copy constructors for all classes,

e)   the methods double perimeter() and double area() for the classes Circle, Rectangle and Square for returning the perimeter and area of corresponding instances,

f)     in your testing program create instances of Circle, Rectangle and Square, and then compute and print on the screen their perimeter and area.

Name your files Shapes.h, Shapes.cpp and testshapes.cpp.

You can assume that the setting values are always valid.

Problem 11.6 A Vector class for doubles

Create a class named Vector for storing and managing vectors of doubles. The properties of a vector are the size of the vector and a double pointer pointing to a memory location where the components of the vector are stored. The class has to provide a default constructor, another constructor for setting the properties, a copy constructor and a destructor. The non-default constructors should dynamically allocate memory for a vector and the destructor should release it.

Provide suitable setter and getter methods for each property and a method for printing the components of a vector on the screen separated by spaces. Also provide methods for computing the norm of a vector, the sum, the difference and the scalar product of two vectors. The class declaration has to be placed into Vector.h, the class definition has to be placed into Vector.cpp and the test program where the instances are created has to be in testvector.cpp. The test program should create four instances of the Vector class using the different constructors (the constructor without parameters, parametric constructor and copy constructor). The third instance should be the copy of the second one). The fourth instance should be created with parameters as well. Then the norm of the second vector should be computed and printed on the screen, the scalar product, the sum and the difference of the second and fourth vectors should be also computed and printed on the screen.

Use the const modifier properly for parameters and methods.

You can assume that the input or the setting values are valid. The prototypes should have the following form:

double norm();

Vector add(const Vector) const; The usage should be:

Vector v1, v2;

(v1.add(v2)).print();

Note: If v1 = (a1,a2,...,an) and v2 = (b1,b2,...,bn) with n ∈ N∗. Then:

  (the norm of a vector),

n

v1 · v2 = P ai · bi (the scalar product of two vectors),

i=1

v1+ v2 = (a1 + b1,a2+ b2,...,an + bn), and v1 − v2 = (a1 − b1,a2 − b2,...,an − bn).

More products