Anda di halaman 1dari 4

Advanced C++ Programming- Exercises

Exercise 1 Write a class int_stack that will manage a stack of integers. The integers values will be stored in a dynamically allocated array. This class will propose the following member functions : int_stack (int n) int_stack ( ) ~ int_stack ( ) int empty ( ) int full ( ) void operator < (int p) int operator >(int p) constructor that will dynamically allocate n integers, constructor allocating 20 integers, destructor, the return value is 1 if the stack is empty, 0 otherwise, the return value is 1 if the stack is full, 0 otherwise, pushes (add) the p value on the stack, returns (and remove) the value on the top of the stack,

plus a copy constructor and an assignment operator. Exercise 2 Write a list class that will handle linked objects which specific details are unknown. On the class, the following operations are proposed : - add an information in the list (at the front), - initialize the navigation through the list, - go to the next information, - get the current information, - detect if the end the list has been reached. Use the previous class to create a list_of_points. Add a member function in this second class to display the information of each point stored in the list. Exercise 3 On the class point, manage dynamically allocated points in a linked list. When a class user requires a new dynamic point, first check if there is any in the list, and if not invoke the standard new operator. When a class user deletes a dynamic point, this object is inserted in the list. Exercise 4 Write a class mixed_list obtained from the previous list, that will store objects which information may be displayed. Use it to store objects of different classes (point, vector, circle, rectangle) and navigate through the list to display the each object s information.

Exercise 5 Define a base class person that will contain universal information, including name, address, date of birth, and gender. Derive from this class the following classes : student, worker and student_worker. Modify the classes so that each one contains the list of all its instances. Write a program that reads information on persons, and displays a list of employees, a list of students and a list of people that are student and worker. Exercise 6 Add to class point a method that clones an object : it returns a new dynamic object. Add to class list_of_points a method that duplicates (clones) it. Use this list to store points and colored points , and try to clone it. Exercise 7 Write a class that manages naturals (positive or nul integers) : its constructor with one argument generates an ConstErr exception if the value of the argument is negative. Exercise 8 Define a class Array : when constructing an object of this class, mention the lower and upper bounds. Only positive values should be inserted. Provide the way to push and pop information in the array, and an operator to directly access to on element within the bounds. Exceptions should be thrown when trying to store a negative value, accessing, push or popping outside the bounds.

Exercise 9 Derive from class Person a class Employee that contains a reference to the company he works for. When constructing an object of this class, mention the company. Add a method to change the company of an employee. Define a specific kind of employee, a Director. Define a class Company that contains the list of all its employees. You will be able to remove employees. Add a method to class Company to define its director. Modify the classes to handle every possible exception : a company may have only one director, an employee must work for a company and may work for only one company, you cannot remove an employee that does not work for this company, ...

Exercise 10 Define a class Array (of integers) that dynamically allocates memory to store integers. Provide a copy constructor that does not duplicate the dynamic part : a intermediate object will be used that contains a pointer to the dynamic part, and the numbers of references to that area. Limit the number of copies to 2 and handle an exception.

Exercise 11 Add a function exists to class list that returns true if its argument is in the list and throws an exception otherwise.

Exercise 12 Define a new list that serializes (saves) its information on a file.

Exercise 13 On class mixed_list, provide a member function that only displays points. Generalize that function so that it only displays objects of a class that the user chooses.

Exercise 14 Write a function that gives the maximum of an area (variable size). Use it with integers and points.

Exercise 15 Write a function that duplicates an area (variable size). Use it with integers and points. Try to use it with strings (pointers to characters).

Exercise 16 Write a function that sort an area. Use it with integers and points. Correct it in order to use it with an object of class int_arr (dynamic array of integers).

Exercise 17 Write the class template of a stack data structure.

Exercise 18 Rewrite class list as a template.

Anda mungkin juga menyukai