Class Designer for C++ in Visual Studio 2008

Let me spoil it for you: it’s not an actually class designer, because it’s only one way. It should have been called Class Viewer, but probably it get’s more publicity if it’s called designer.

Let me explain how to us it. Of course, you need to have some source code for which you want to see the class diagrams. Just to exemplify, let’s consider the following code:

enum TriangleType {Isosceles, Rectangular, Obtuse, Echilateral};

struct Point 
{
	int X;
	int Y; 

	Point(int x = 0, int y = 0): X(x), Y(y)
	{}
};

class Shape 
{
public:
	virtual void Draw() = 0;
};

class Circle : public Shape
{
	int Radius;
	Point Center;
public:
	Circle(int radius, const Point& center):
	  Radius(radius), Center(center)
	  {}
	
	int get_radius() const {return Radius;}
	void set_radius(int radius) {Radius = radius;}

	Point get_center() const {return Center;}
	void set_center(const Point& center) {Center = center;}

	virtual void Draw()
	{
	}
};

class Triangle : public Shape
{
	TriangleType Type;
	Point Vertices[3];
public:
	Triangle(const Point& p1, const Point& p2, const Point& p3)
	{
		Vertices[0] = p1;
		Vertices[1] = p2;
		Vertices[2] = p3;
	}

	TriangleType get_type() const {return Type;}
};

To be able to view a diagram for the classes, structs, and other entities, you must add a Class Diagram file (has the extension .cd). You can either do that from the menu (Project > Add new Item), Solution Explorer or simply by pressing Ctrl + Shift + A. The new file will be opened and you’ll have to drag and drop items from the Class View or the Solution Explorer.

Class View in Visual Studio 2008

When you drop classes that are part of hierarchies the arrows indicating the relationships are automatically shown.

Example Class Diagram for C++ classes

If you double click on a member (function, field, etc.) you will be directed in the code where it is defined. If you right-click on a class, you’ll get a menu with commands, but, as I was saying earlier, there is no possibility to add new elements from the designer.

Context menu in class designer

Class details are shown in an additional pane, and of course, they are also read-only.

Class details for the components of the class diagram

You can also use the Class Designer toolbar for sorting elements, changing the layout or zooming.

I can only hope this is only a first step and that the next version of Visual Studio will feature a real designer for C++ too.

1 thought on “Class Designer for C++ in Visual Studio 2008”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.