100% Guaranteed Results


Programming in C and C++
$ 24.99
Category:

Description

5/5 – (1 vote)

Assignment 12 – Inheritence, static Members, Operator Overloading, Polymorphism
• 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.
Problem 12.1 Hexagon class (1 point)
Language: C++ Download the files:
Note, that the area of a hexagon of side t can be computed by using the formula.
Name the files Shapes.h and Shapes.cpp. Then write a testing program testHexagon.cpp that:
a) creates a blue hexagon that has the side of 9,
b) creates a green hexagon that has the side of 15,
c) creates a copy for the second hexagon using the copy constructor, and
d) computes the perimeter and area of all three hexagons and prints the results on the screen.
Problem 12.2 TournamentMember class (2 points)
Language: C++
Imagine that you are in the design stage of a software system for handling the data of the participants of a major soccer tournament. As different roles will be present (players, coaches, referees, etc.) you are required to develop a class handling the data of a generic league member. For each person the following data is at least needed
• first name as character array (36 characters characters including ’’)
• last name as character array (36 characters)
In addition the whole team is located somewhere, so location is an additional static property of the class.
Design and implement a class for holding these data. In addition add at least two other general properties to this class.
The class, which will be called TournamentMember, should provide constructors (empty and parametric), a destructor and also a copy constructor. The class should also provide inline setter and getter methods (either inside or outside of the class).
Moreover, in order to carry out the functionality of the application, the following methods are required:
• a method which prints the information of a tournament member on the screen,
• a method which changes the location.
Also all constructors and the destructor should print a short informational message on the screen, such that you can see which is being called when.
You should provide three files: a header file named TournamentMember.h with the declaration of the class, a file named TournamentMember.cpp with its definition, and an additional file called testTournamentMember.cpp with a main() function which tests the functionality of the class.
The needed data can be initialized in the code from the main() function.
Problem 12.3 Player class (1 point)
A Player class should be derived from the TournamentMember class. It holds additional properties such as number, position, number of goals scored, and whether the player is left-footed or right-footed. For each property appropriate setter (except the number of goals scored) and getter methods need to be provided as inline methods, and it should not be possible to manipulate data directly. An appropriate constructor to set all properties on creation should be provided as well as a copy constructor that creates a correct copy of a player, and a destructor. Also all constructors and the destructor should print a short informational message on the screen such that you can see which is being called when. Also the following methods are required:
• a method which prints all the information of a player on the screen,
• a method which increments the number of goals scored by a player.
Add code to the files TournamentMember.h, TournamentMember.cpp, and write a testing program named testPlayer.cpp that tests the functionality of the Player class. Create three players with different properties. Then move all players to the location “Hamburg”. The needed data can be initialized in the code from the main() function.
Problem 12.4 Fractions I (2 points)
• As a starting point, use the files fraction.h, fraction.cpp, testfraction.cpp (which you can download from:
• Replace the method print() by an overloaded operator << such that you can use cout << for printing a fraction on the screen.
• Overload the operator >> such that you can enter from the keyboard a fraction using cin >>. Check the validity of the input (you can assume that the numerator and denominator will be numbers).
• Overload the operators ∗ and / for computing the multiplication and division of two fractions.
• In your testing program you should then be able to enter two fractional numbers (usingcin >>), then the product and quotient are printed on the screen (one per line using the overloaded operator and cout <<).
Use the suggestions from slide 10 (Tutorial 12) for choosing to write member methods or friend functions.
You can assume that the input will be valid.
Problem 12.5 Fractions II (2 points)
Continue with your program for Problem 12.4 in the following manner. Remember to check the mathematical validity of the parameters (you can assume that the nominator and the denominator are always numbers). Use the suggestions from slide 10 (Tutorial 12) for choosing to write member methods or friend functions.
Also consider the suggestions regarding the return types and parameter types.
• Overload the operators +, – for computing the sum and difference of two fractions.
• Overload the operator = for assigning.
• Overload the operators < and > to compare two fractions.
• In your testing program you should be able to enter two fractions from the keyboard usingcin >>. Determine the greater fraction and print it on the screen using cout <<. Also compute the sum and the difference of the two fractions (storing the result in other objects) and print them on the screen (one per line using the overloaded operator and cout <<).
In order to implement the addition and subtraction of two fractions you will have to calculate the lowest common multiple (LCM) of the denominators of the two fractions. The LCM can be com-
a · b
puted according to the formula: LCM(a,b) = , where GCD is the greatest common
GCD(a,b)
divisor.
Operations with fractions
Addition:
Subtraction:
Multiplication:
Division: a c a · LCM(b,d)/b+c · LCM(b,d)/d
+ = b d LCM(b,d)
a c a · LCM(b,d)/b − c · LCM(b,d)/d
− = b d LCM(b,d)
a c a · c
· = b d b · d
a c a · d
/ = b d b · c
Problem 12.6 Polymorphism I (2 points)
• Draw (using ASCII characters) a diagram how these classes relate to each other and putthis into testvirtual.cpp as part of your comments.
• For each numbered point in the file testvirtual.cpp add a detailed comment about what is happening in the program.
• Like in Circle.cpp, output a message on the screen when the method calcArea() is being called in any of the classes.
• Add a method to calculate the perimeter for each class definition.
• Change the test program to additionally print the total perimeter of all objects.
• Also print a message on the screen when the method calcPerimeter() is being called.
• Add a Square class (consisting of a header file and a cpp file), and add a square object to your test program. Consider the relation of a square to the other classes.
Submit a zip file containing all your .h and .cpp files related to this problem.
Problem 12.7 Polymorphism II (1 point)
Change testvirtual.cpp such that 25 objects (circles, rings, rectangles, squares) are randomly created at runtime. Their colors (RED, BLACK, VIOLET and BLUE) and sizes (between 5 and 100) should also be randomly chosen.
Compute the area and the perimeter for each object and print them on the screen. Submit a zip file containing all your .h and .cpp files related to this problem.
How to submit your solutions
• Your source code should be properly indented and compile with gcc or g++ depending on the problem without any errors or warnings (You can use gcc -Wall -o program program.c or g++ -Wall -o program program.cpp). Insert suitable comments (not on every line …) to explain what your program does.
• Please name the programs according to the suggested filenames (they should match the descriptionof the problem) in Grader. Otherwise you might have problems with the inclusion of header files. Each program must include a comment on the top like the following:
/* CH-230-A
*/
If there are problems (but only then) you can submit the programs by sending mail to

Reviews

There are no reviews yet.

Be the first to review “Programming in C and C++”

Your email address will not be published. Required fields are marked *

Related products