9.3. Test Yourself: Translating Basic UML to C++

It's human nature to imagine that we understand a concept but struggle to use it. Applying our understanding to increasingly complex problems helps identify where our understanding is incomplete and reinforces it as we continue learning, making it more memorable.

Car
class Car
{
	private:

	public:
};
 -model : string
private:
	string model;
 -wheels : int
private:
	int wheels;
 -engine_size : double
private:
	double engine_size;
 +vin : string
public:
	string vin;
 +Car(a_name : string, a_wheels : int, a_engine_size : double)
public:
	Car(string a_name, int a_wheels, double a_engin_size);
 +get_model() : string
public:
	string get_model();
 +is_running() : bool
public:
	bool is_running();
 +set_wheels(num_wheel : int) : void
public:
	void set_wheels(int num_wheel);
 +accelerate(fuel_rate : int) : double
public:
	double accelerate(int fuel_rate);
 -diagnostics(system : char) : int
private:
	int diagnostics(char system);
Translating a UML class diagram to C++. Try converting each line of the UML class into C++. As you convert each part of the UML diagram, check yourself by hovering the mouse pointer over the text to see the corresponding C++ code. Recall that C++ is a case-sensitive language.

The tooltips display each member in its own "public" or "private" section, but programmers typically group them into just two or three sections. Constructor functions often initialize data members, so programmers typically name the function's parameters similarly to the members. Prefixing each parameter with an 'a' (for argument) is common. Press "Next" below to see two versions of the complete C++ class.