Skip to main content

[C++] Apply the primary principle of C++ for preparing a meal

 

Let's apply the principle of C++ (object-based programming) to the task of preparing a meal. In object-based programming, we break down tasks into objects that represent real-world entities. These objects have attributes (data) and methods (actions they can perform).


Example: Preparing a Meal in C++ Style

Imagine you're preparing a meal with different objects, each responsible for a specific part of the process. For simplicity, let's define three main objects: Chef, Ingredients, and KitchenTools.

1. Chef Object:

  • Attributes: Name, experience level.
  • Methods: prepareMeal(), useTool(), mixIngredients().

2. Ingredients Object:

  • Attributes: Type (vegetables, meat, spices), quantity, freshness.
  • Methods: chop(), cook(), serve().

3. KitchenTools Object:

  • Attributes: Tool type (knife, pan, oven), condition (new, used).
  • Methods: cut(), heat(), stir().

C++-Style Meal Preparation Example:

Here’s how we would code a meal preparation process in C++:






Breakdown:

  • The Chef object coordinates the meal preparation by using KitchenTools and Ingredients.
  • The Ingredients object holds information about the food (type, quantity) and has methods like chop(), cook(), and serve().
  • The KitchenTools object has methods like useTool() that represent the actions the tools perform, like cutting or heating.

How C++ Principles are Applied:

  • Encapsulation: Each part of meal preparation (chef, ingredients, tools) is represented as a separate object. These objects encapsulate their data and functions.
  • Reusability: You can create other ingredient or tool objects (like meat or spices) and use them with the chef object, making the code flexible and reusable.
  • Abstraction: The details of how chopping or cooking is done are hidden inside the objects, allowing the main program to focus on the overall process.

Real-Life Analogy:

In real life, preparing a meal involves using tools (knife, stove), ingredients (vegetables, meat), and a chef who coordinates the process. In C++ terms, these become objects with their own abilities, working together to complete the meal!

This approach not only makes the process modular but also allows you to easily change or add components, just like in object-based programming!





Comments

Popular posts from this blog

How to test frame buffer in linux

The intended goal of this article is to provide the user with enough information so they can display an image onto a screen of their choosing. How to re-build frame buffer testing application. Base knowledge http://trac.gateworks.com/wiki/OpenEmbedded/Video_Out Frame buffer test application - rebuild fb-test-app step 1: Download soure code ( git installed on your machine and account in git-hub is required) https://github.com/prpplague/fb-test-app Command in git: git init git clone https://github.com/prpplague/fb-test-app.git step 2: Change makefile vi Makefile //open Makefile by vim editor :set nu //show line number replace from line 6 to line 12 by folow code: CC=arm-linux-gnueabi-gcc CFLAGS=-02 -Wall Save change command :wq step 3: Rebuid Type “make” command step 4: Copy execute file to target system by filezilla or something like that step 5: Remote target by root account, change mode of execute file (what copied from host)

[Git] Handle trailing space when patching with git

After complete coding one module in development branch. Next phase is merging. Obviously, you can merge source code automatically without no errors happen. However, life is not dream. Create a patch git diff HEAD > newTariff.patch Apply patch git apply newTariff.patch then problem happens Problem git apply newTariff.patch:106: trailing whitespace. patch does not apply Route cause: Nguyên nhân: Do trong source code có những dấu space thừa (ô vuông màu đỏ hình dưới) Some whitespaces is existed in your patch. (red area in below pictures) Fix Clean white space and patch again. Cách khắc phục Xóa những dấu space này đi và thực hiện patch lại Make up after complete coding. Find and clean whitespace before create patch file. Sửa sau khi code xong Kiểm tra sau khi code xong có lỗi này không git diff HEAD --check Prevent whitespace by manually when typing source code Sửa ngay khi đang code =>Bật chức năng hiển thị các dấu whitespace, hoặc remove trailing

The best 12 AI Tools in 2023

The new life challenges require new tools.  For decades, AI technology has been a fuzzy field for unprofessional users. However, it is not rocket science anymore at this time.  There are the best 12 AI tools in 2023 that will keep you up and take some advantages in your business. Here, I put everything on your table. Enjoy Solves anything - ChatGPT ( https://chat.openai.com/auth/login ) Write anything - Writesonic ( https://writesonic.com/ ) Generates Art - Midjourney ( https://www.midjourney.com/explore ) Generates Code - Replit ( https://replit.com /) Generates Video - Synthesia ( https://www.synthesia.io/ ) Generates Music - Soundraw ( https://soundraw.io/ ) Generates Tiktoks - Fliki ( https://fliki.ai/ ) Generates Avatars - Starrytars ( https://starryai.com/en/starrytars ) Generates PPTs - SlidesAI ( https://www.slidesai.io/ ) Edit Pictures - Remini ( https://remini.ai/ ) Edit Video - Pictory ( https://pictory.ai/ ) Summarize Notes - Wordtune( https://www.wordtune.com /)