Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

12: Class Design

Define a class B1 with a virtual function vf() and a non-virtual function f(). Define both of these functions within class B1. Implement each function to output its name (e.g., B1::vf()). Make the functions public. Make a B1 object and call each function.

Derive a class D1 from B1 and override vf(). Make a D1 object and call vf() and f() for it.

Define a reference to B1 (a B1&) and initialize that to the D1 object you just defined. Call vf() and f() for that reference.

Now define a function called f() for D1 and repeat 1–3. Explain the results.

Add a pure virtual function called pvf() to B1 and try to repeat 1–4. Explain the result.

Define a class D2 derived from D1 and override pvf() in D2. Make an object of class D2 and invoke f(), vf(), and pvf() for it.

Define a class B2 with a pure virtual function pvf(). Define a class D21 with a string data member and a member function that overrides pvf(); D21::pvf() should output the value of the string. Define a class D22 that is just like D21 except that its data member is an int. Define a function f() that takes a B2& argument and calls pvf() for its argument. Call f() with a D21 and a D22.

What is an application domain?

What are ideals for naming?

What can we name?

What services does a Shape offer?

How does an abstract class differ from a class that is not abstract?

How can you make a class abstract?

What is controlled by access control?

What good can it do to make a data member private?

What is a virtual function and how does it differ from a non-virtual function?

What is a base class?

What makes a class derived?

What do we mean by object layout?

What can you do to make a class easier to test?

What is an inheritance diagram?

What is the difference between a protected member and a private one?

What members of a class can be accessed from a class derived from it?

How does a pure virtual function differ from other virtual functions?

Why would you make a member function virtual?

Why would you not make a member function virtual?

Why would you make a virtual member pure?

What does overriding mean?

Why should you always suppress copy operations for a class in a class hierarchy?

How does interface inheritance differ from implementation inheritance?

What is object-oriented programming?

Define two classes Smiley and Frowny, which are both derived from class Circle and have two eyes and a mouth. Next, derive classes from Smiley and Frowny which add an appropriate hat to each.

Try to copy a Shape. What happens?

Define an abstract class and try to define an object of that type. What happens?

Define a class Immobile_Circle, which is just like Circle but can't be moved.

Define a Striped_rectangle where instead of fill, the rectangle is "filled" by drawing one-pixel-wide horizontal lines across the inside of the rectangle (say, draw every second line like that). You may have to play with the width of lines and the line spacing to get a pattern you like.

Define a Striped_circle using the technique from Striped_rectangle.

Define a Striped_closed_polyline using the technique from Striped_rectangle (this requires some algorithmic inventiveness).

Define a class Octagon to be a regular octagon. Write a test that exercises all of its functions (as defined by you or inherited from Shape).

Define a class Rounded that is like a Rectangle, except that it has rounded corners. Use class Arc that you can find in the PPP support code on www.stroustrup.com/programming.html. Test it.

Define a class Box that is a closed shape like a Rectangle (so it has fill color), except that it has rounded corners. Use class Pie that you can find in the PPP support code on www.stroustrup.com/programming.html.

Define a Group to be a container of Shapes with suitable operations applied to the various members of the Group. Hint: Vector_ref. Use a Group to define a checkers (draughts) boardwhere pieces can be moved under program control.

Define a class Pseudo_window that looks as much like a Window as you can make it without heroic efforts. It should have rounded corners, a label, and control icons. Maybe you could add some fake "contents", such as an image. It need not actually do anything. It is acceptable (and indeed recommended) to have it appear within a Simple_window.

Define a Binary_tree class derived from Shape. Give the number of levels as a parameter (levels==0 means no nodes, levels==1 means one top node with two sub-nodes each with two sub-nodes, etc.). Let a node be represented by a small circle. Connect the nodes by lines (as is conventional). P.S. In computer science, trees conventionally grow downward from a top node (amusingly, but logically, often called the root).

Modify Binary_tree to draw its nodes using a virtual function. Then, derive a new class from Binary_tree that overrides that virtual function to use a different representation for a node (e.g., a triangle).

Modify Binary_tree to take a parameter (or parameters) to indicate what kind of line to use to connect the nodes (e.g., an arrow pointing down or a red arrow pointing up). Note how this exercise and the last use two alternative ways of making a class hierarchy more flexible and useful.

Add an operation to Binary_tree that adds text to a node. You may have to modify the design of Binary_tree to implement this elegantly. Choose a way to identify a node; for example, you might give a string "lrrlr" for navigating left, right, right, left, and right down a binary tree (the root node would match both an initial l and an initial r).

Define a class Controller with four virtual functions on(), off(), set_level(int), and show(). Derive at least two classes from Controller. One should be a simple test class where show() prints out whether the class is set to on or off and what is the current level. The second derived class should somehow control the line color of a Shape; the exact meaning of "level" is up to you. Try to find a third "thing" to control with such a Controller class.

The exceptions defined in the C++ standard library, such as exception, runtime_error, and out_of_range (§4.6.3), are organized into a class hierarchy (with a virtual function what() returning a string supposedly explaining what went wrong). Search your information sources for the C++ standard exception class hierarchy and draw a class hierarchy diagram of it.