Arvutiteaduse instituut
  1. Kursused
  2. 2023/24 kevad
  3. Programmeerimine keeles C++ (MTAT.03.158)
EN
Logi sisse

Programmeerimine keeles C++ 2023/24 kevad

  • Home
  • Labs
    • Submission of solutions
    • Results
  • Examination times
  • Guides
  • References

Lab 2: Classes and Objects

General conditions

For me, the base program does not compile. I need to remove the =default in Point2() = default; in the file Point2.h

Important! Read the conditions for completing the assignments on the module website! Tasks are submitted via the form on the module website. Solutions will not be accepted via email. If you have any questions, please contact the subject list or the lab supervisor.

There are 14 days to solve the exercise.

Deadline: 10.03.2024 23:59:59

Task - A simple geometry library

The goal of the homework is to create a simple geometry library. The aim is to compile a library called libgeometry.a. Library headers must be created so that so that the test program geometrytest that accompanies the base files is compiled. During the evaluation, we check both this and the codeof the methods. make test compiles the test program.

We will also start structuring the code in directories from this task. Place headers in folder include, library source code in folder src, the finished library goes in folder lib, test application in folder tests, documentation in folder docs. Files Makefile and Doxyfile remain in the root directory. Use the files provided as an example. !!!Please do not change the names of the class members and keep them public.!!!

1. Point (2 points)

Write a class Point2 that defines the type of a point in a two-dimensional space with coordinates x and y.

MethodPurpose
Point2 ()initializes the coordinates with zeros
Point2 (float nx, float ny)parameterized constructor that initializes point with given values
float distanceFrom (Point2 p)returns the distance of a point from another point
operator <<outputs the coordinates of the point in the form (x, y)

2. Line (2 points)

Create a class Line2 that describes a straight line segment using two endpoints p1 and p2.

MethodPurpose
Line2 ()default constructor - creates points using Point2 default constructor
Line2 (Point2 np1, Point2 np2)Parameterized constructor – values for class elements are provided
float length ()returns the length of a line (use the point distanceFrom method)
operator <<outputs straight line data in the form (p1 - p2)

3. Circle (4 points)

Create a class Circle2that represents a circle in the plane using the center p1and a non-negative radius r . The non-negative radius must be observed in all methods (constructor, scaling). If the user specifies a negative radius in the constructor, replace it with zero. If the user wants to scale with a negative value, refuse to scale. In the next tutorial, we will look at how to handle such error situations with exceptions, if you wish, you can use the exceptions here as well.

MethodPurpose
Circle2 ()default constructor - creates a Point2 using the default constructor and sets the radius to zero
Circle2 (Point2 np1, float nr)Parameterized constructor – values for class elements are provided
float circumference ()returns the circumference of the circle
float area ()returns the area of ​​the circle
bool contains (Point2 p)returns true if the given point is on or inside the circle, otherwise false (use the point distanceFrom method)
bool contains (Line2 l)returns true if the line is inside a circle (including vertices on the line), otherwise false (use distanceFrom method)
void scale (float factor)multiplies the radius of the circle by the given value
operator <<outputs the circle in the form (p1, r)

4. Makefile and documentation following the directory structure (2 points)

The solution follows the directory structure correctly, and the library, test application, and documentation are created in the correct folders.

Additional task - Three-dimensional geometry (1 additional point)

Write three-dimensional versions of the classes (Point3, Line3, Sphere3).

  1. Add a third coordinate zto the point class and complete the corresponding constructors. The resulting class then has three coordinates.
  2. The line and the sphere must be determined by Point3. Complete the methods accordingly.
  3. For line and sphere implement same methods as in main assignment. Replace Point2 with Point3 where needed.
  4. Add a method float volume() to the sphere class which returns its volume

NB! Keep the circumference method and calculate. Calculate the sphere circumference as a circle circumference.

Tips

  1. The square root function is sqrt.
  2. Information on the use of pi can be found here: http://c-faq.com/fp/mpi.html Libraries beyond this and sqrt should not be necessary
  3. Avoid temptation to inherit Point3 from Point2. This is not practical optimization.
  4. Overloading the operator << should be done inside class (use keyword friend). Then the next lab will be a bit easier.
  5. For creating class constructors and giving values to class elements we recommend using initializer lists. For more information: http://en.cppreference.com/w/cpp/language/initializer_list
  6. make clean command should erase documentation.
  7. In case you want to use less copying in makefile then consult this page http://www.cprogramming.com/tutorial/makefiles_continued.html
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused