Tính trừu tượng trong C++| Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create 3 classes Person, Employee and Customer in 3 following files: Person.cpp, Employee.cpp, Customer.cpp with the following information:

Person class

  • Person is a abstract class used to store general information about human objects.
  • name and address are private attributes that indicates name and address of an object.
  • Person(name: String, address: String) is a constructor with parameter which is used to initialize values for attributes.
  • display() is an abstract method used to display information of the object.

Employee class

  • Employee is the class inheriting from the abstract class Person
  • salary attribute stores information about employees' salary.
  • Employee(name: String, address: String, salary: int) is the constructor used to initialize values for attributes.
  • display() is the overridden method from the abstract class, it is used to display information of employees on the screen. For example, if name = "Trung", address = "HN", salary = 3300,then when we call the display() method, the program produces the following information:
  • Employee name: Trung
    Employee address: HN
    Employee salary: 3300​

Customer class

  • Customer is the class inheriting from the abstract class Person
  • balance is the attribute for storing information about the balance in a customer's account.
  • Customer(name: String, address: String, balance: int) is the constructor that is used to initialize values for attributes.
  • display() is the method overriding from the abstract class. This method is used to display information of employee on the screen. For example, if name = "Linh", address = "BN", balance = 10400, then when we call display() method, the program produces the following result:
  • Customer name: Linh
    Customer address: BN
    Customer balance: 10400

Note: In a class diagram, if a class name is italic, it is an abstract class, if the method name is italic, it is an abstract method.

The program to test the above 3 classes:

#include "Person.cpp"
#include "Employee.cpp"
#include "Customer.cpp"

int main() {
	Person *person1 = new Employee("Trung", "HN", 3300);
	Person *person2 = new Customer("Linh", "BN", 10400);
	person1->display();
	person2->display();
	return 0;
}

When the above code is compiled and executed, it produces the following result:

Employee name: Trung
Employee address: HN
Employee salary: 3300
Customer name: Linh
Customer address: BN
Customer balance: 10400

Theory

In this article, you will learn about the last concept in object oriented programming, which is abstraction.

Abstraction

Abstraction is one of 4 general characterisitics in OOP, it focuses only on the features of the object and hides unnecessary information. It helps you focus more on the features rather than how it is done. Example of abstraction in life:

When you go to draw some money at an ATM, you do not care about how the ATM works or the components in the ATM, the only thing that you care about is the drawing feature. In this case, the unnecessary information of the ATM is hidden, which is abstraction.

Similarly in programming, when using an object you only need to care about the necessary methods and know what those methods are used for, regardless of source code and other methods. To implement abstraction in C++, you can use abstract class.

Abstract class

An abstract class is a class that is declared with the abstract keyword and has the following characteristics:

  1. An abstract class is the class that has at least one abstract method. The abstract methods are virtual methods with the value = 0 as shown in the following example:
    class Animal {
    public:
    	// sound is an abstract method
    	virtual void sound() = 0;
    };
    You can see that the abstract method is a method with only the declaration, without the implementation => when looking at this method, the only thing you see is its functionality (instead of the way it is implemented).
  2. You can not create an instance of abstract class, you only can create an instance of child class. For example: 
    #include <iostream>
    
    using namespace std;
    
    class Animal {
    public:
    	// sound là một phương thức trừu tượng
    	virtual void sound() = 0;
    };
    
    class Cat : public Animal {
    	void sound() {
    		cout << "meow meow";
    	}
    };
    
    int main() {
    	Animal* a = new Cat();
    	a->sound();
    	return 0;
    }
    The above program won't throw any errors and display "meow meow" on the screen. But if you change  Animal* a = new Cat(); to Animal a; then the program will throw error (you can't create an instance of abstract class).
  3. If a class inherits from a abstract class, this class has to override all the methods in abstract class. For example:
    #include <iostream>
    
    using namespace std;
    
    class Animal {
    public:
    	// sound là một phương thức trừu tượng
    	virtual void sound() = 0;
    };
    
    class Cat : public Animal {
    };
    
    int main() {
    	Cat c;
    	return 0;
    }
    This program will throw error because class Cat didn't override the abstract method sound() from class Animal.

In conclusion, when looking at abstract classes, the only thing you can see is abstract methods (the featrures of the class). In case, a class only has abstract methods, that class is a completely abstract class.

Now you have a basic knowledge about abstract class and abstract method, you can return to Task and give it a try or refer to the instruction below.

Instruction

Code sample:

File Person.cpp:

#pragma once
#include<iostream>

using namespace std;

class Person {
private:
	string name;
	string address;

public:
	Person(string name, string address) {
		this->name = name;
		this->address = address;
	}

	virtual void display() = 0;

	string getName() {
		return name;
	}

	void setName(string name) {
		this->name = name;
	}

	string getAddress() {
		return address;
	}

	void setAddress(string address) {
		this->address = address;
	}
};

File Employee.cpp

#include "Person.cpp"

class Employee : public Person {
private:
	int salary;

public:
	Employee(string name, string address, int salary) :Person(name, address) {
		this->salary = salary;
	}

	void display() {
		cout << "Employee name: " << Person::getName() << endl;
		cout << "Employee address: " << Person::getAddress() << endl;
		cout << "Employee salary: " << salary << endl;
	}
};

File Customer.cpp

#include "Person.cpp"
class Customer : public Person {
private:
	int balance;

public:
	Customer(string name, string address, int balance) : Person(name, address) {
		this->balance = balance;
	}

	void display() {
		cout << "Customer name: " << Person::getName() << endl;
		cout << "Customer address: " << Person::getAddress() << endl;
		cout << "Customer balance: " << balance << endl;
	}
};