Lập trình hướng đối tượng trong C++: sơ đồ lớp trong UML | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create a Student class within the Student.cpp file with the following information:

Explanation: The picture above describes the Student class with the following attributes and methods:

  • name and age are attributes with private access modifier.
  • Student() is a no-arg constructor with public access modifier.
  • getInformation() and display() are methods with public access modifier.

In fact, you should separate the classes into separate files, each class corresponds to a file, the code area in this task is the Student.cpp file. In this file, you need to create a Student class with full attributes and methods as described (you only need to declare attributes and methods, no need to write code inside the method).

Notes: You need to create file name as requested, distinguish uppercase and lowercase. For example, file name like student.cppa.cpps.cpp won't be accepted.

Theory

The above illustrated picture about Student class is a class diagram. In the previous lessons, you already got to know this diagram but you will have a deeper understanding about it in this lesson.

Class diagram

As you know, Object Oriented Programming(OOP) is the technique that solves programming problems by objects and its relationship, in a real project can have lots of classes and objects. Class diagram provides us an overview look about description of classes and its relationship in system (you'll learn about relationship between objects in the next lessons)

A class notation will contain 3 parts: class name, attributes and operators. A class in class diagram can be described as below:

You can define access modifier for attributes, methods by putting these characters -, #, +  before attributes and methods

  • Character '-' corresponds to private access modifier.
  • Character '#' corresponds to protected access modifier.
  • Character '+' corresponds to public access modifier.

For example: See the class Student below:

class Student {
	string name;
private:
	int id;
protected:
	int age;
public:
	double gpa;

	Student() {

	}

	void getInformation() {

	}

	void display() {

	}
};

This Student will be described by the following class diagram (noticing about characters -, #, +):

Notes: When a method does not have a return type and its name is the same with class name - this is constructor. In the above diagram +Student() is the constructor of Student class with Public access modifier. 

To make sure you understood about class diagram, let see the next example about class Product:

class Product {
	string description;
private:
	string name;	
protected: 
	double price;
public:
	int id;
	Product() {

	}
	void getInformation() {
	}
	void display() {
	}
}

This class will be described by the following diagram:

Now you can return to the Task above and solve it or refer to the instruction below.

Instruction

Sample Code:

#include<iostream>

using namespace std;

class Student {
private:
	string name;
	int age;
public:
	Student() {

	}

	void getInformation() {
	}

	void display() {
	}
};