You are on page 1of 2

Assignments

Concept: Classes and objects


Objective: At the end of the assignments, participants will be able to cerate classes and
write programs using objects of those classes.

1. Create a class rectangle. The class has attributes length and width, each of which
defaults to 1. It should have member functions that calculate the perimeter and area of
the rectangle. It should have set and get functions for both length and width. The set
functions should verify that length and width are each floating-point numbers larger
than 0.0 and less than 20.0

2. Modify the above class to include a draw function that displays a filled rectangle
using * as the default fill character. Include a setfillcharacter function to specify the
character out of which the body of the rectangle will be drawn. Include a set
setperimietercharacter function to specify the character that will be used to draw the
border of the rectangle.

3. Create a class called complex for performing arithmetic with complex numbers. Write
a driver program to test your class. Use floating point variables to represent the
private data of the class. Provide a constructor function that enables an object of this
class to be initialized when it is declared. The constructor should contain default
values in case no initializers are provided. Provide public member functions for each
of the following

• Addition of two complex numbers: the real parts are added together and
the imaginary parts are added together

• Subtraction of two complex numbers: The real part of the right operand is
subtracted from the real part of the left operand and the imaginary part of the
left operand is subtracted from the imaginary part of the left operand

• Let the user input complex values in the form a+bi.

• Printing complex numbers in the form (a, b) where a is the real part and b
is the imaginary part

4. Create a class called rational for performing arithmetic with fractions. Write a driver
program to test your class. Use integer variables to represent the private data values
of the class- the numerator and denominator. Provide a constructor function that
enables an object of this class to be initialized when it is declared. The constructor
should contain default values in case no initializers are provided and should store the
fraction in reduced form (i.e. the fraction 2/4 would be stored in the object as 1 in the
numerator 2 in the denominator). Provide public member functions for each of the
following
• Addition of two rational numbers. The result should be stored in the
reduced form

• Subtraction of two rational numbers. The result should be stored in the


reduced form

• Multiplication of two rational numbers. The result should be stored in the


reduced form

• Division of two rational numbers. The result should be stored in the


reduced form

• Let the user input rational numbers in the form a/b.

• Printing rational numbers in the form a/b.

5. Create a class date for manipulating dates. Provide a constructor that enables an
object of this class to be initialized when it is declared (You can select any default
values for the day, month & year, e.g. your birth date). Provide the necessary
functionality to perform error checking on the initializer values for data members day,
month, and year. Also, provide a member function to add an integer in a date to
obtain a new date.

You might also like