9.7. student Class Example

Time: 00:05:27 | Download: Large Small | Streaming

This is a simple, one-file example of a UML class diagram and the corresponding C++ class. The constructor initializes the member variables, and the display function prints the current member variable values to the console.

UML class diagram for class student:
student
---------------
-name : string
-wnum : int
---------------
+student(n : string, i : int)
+display() : void
The student UML class diagram. The UML is a language with an established syntax. We can choose how much information we want to display. We'll include all three sections in the student UML diagram. We put the name in the top section, the attributes in the middle section, and the operations in the bottom section. The "-" symbol denotes private features while the "+" symbol denotes public features.
#include <iostream>
#include <string>
using namespace std;


class student
{
	private:

		string	name;
		int	wnum;

	public:
		student(string n, int i)
		{
			name = n;
			wnum = i;
		}

		void display()
		{
			cout << name << ", " << wnum << endl;
		}
};


int main()
{
	student	sb_pres("Alice", 123);
	sb_pres.display();

	student	class_pres("Dilbert", 987);
	class_pres.display();
	
	return 0;
}
The student example.