Advance Innovation Centre
  • AIC Knowledge @ EEC for All
  • šŸ˜ŽLogical Thinking
    • Karel Robot
    • Code to Flowchart
    • Play with Docker
    • CNX Software
  • MCU & Interfacing with Infineon PSOCā„¢
    • Basic MCU Interfacing
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
      • PSoCā„¢ 6S2 Peripherals Interfacing (GPIO)
        • Hello World and LED Blinking
        • GPIO Principles
        • PSoCā„¢ 6S2 GPIO-HAL LED Blink Lab
        • PSoCā„¢ 6S2 GPIO-PDL LED Blink Lab
        • Button "Bounce" Principles
          • Push/Pull Button to Turn ON/OFF LED via HAL
          • Push/Pull Button to Turn ON/OFF LED via PDL
          • GPIO Button Interrupt via HAL
          • GPIO Button Interrupt via PDL
        • GPIO variables & functions
      • PSoCā„¢ 6S2 Peripherals Interfacing (ADC, PWM)
        • PSoCā„¢ 6S2 SAR ADC
          • ADC Principles
          • PSoCā„¢ 6S2 with ADC Labs
            • Reading potentiometer sensor value via an ADC HAL
            • Reading potentiometer sensor value via an ADC PDL
        • PSoCā„¢ 6S2 PWM & TCPWM
          • PWM Principles
          • PSoCā„¢ 6S2 for PMW Function Labs
            • LED Brightness using PWM via HAL
            • LED Brightness using PWM via PDL
    • Sensor Interfacing and HMI
      • OLED Display
        • OLED Display Principles
        • Calling BDH’s OLED functions
        • Display ADC via Potentiometer on OLED
      • BDH Shell
        • Shell Principles
        • LED Blinking and CAPSENSE via BDH Shell
        • Adding "History" command
        • Adding "Reboot" command
        • CAPSENSE Button and Slider
          • CAPSENSE Button and Slider with Capsense Tuner
          • CAPSENSE Button and Slider using FreeRTOS
    • Serial Communication & Visualization
      • UART, I2C, SPI Communication via Infineon PSoCā„¢6
      • BMX160 Sensor Communication via Infineon PSoCā„¢6
        • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
        • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
        • Motion Sensors GUI Integration via Serial Studio
    • IoT Connectivity & Data Analytics via Node-Red
      • Node-Red Installation
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • Edge AI on PSoCā„¢
      • Machine Learning on PSoCā„¢6 via Edge-Impulse
    • Infineon PSoCā„¢ Troubleshooting
  • IoT Development with Infineon PSOCā„¢ & BDH Platform
    • PSoCā„¢ IoT Development Kit
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
        • Hello World and LED Blinking
    • IoT Connectivity
      • Node-Red Installation
      • Controlling PSoCā„¢ LED using MQTT
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • BDH IoT Connectivity
    • WireLinXā„¢ IoT PLC
    • BDH X-Brain Data Analytics
      • PSoC6 Data Collection to CSV log file
    • Data Visualization
      • สร้าง Dashboard ด้วย Looker Studio
  • šŸ–„ļøOperation Systems
    • Prerequisites
      • Guideline from Ubuntu
        • Ubuntu and VSCode on WSL2
      • ตณดตั้ง WSL 2
      • Run Ubuntu on VirtualBox7
    • Zero to Linux Hero
      • Computer OS Architecture
      • Anatomy of Linux System
        • UNIX/Linux History
        • UNIX/Linux Evolution
        • GNU Project
        • Linux OS Architecture
        • Command Line Interface (CLI)
          • Basic Commands
          • šŸ˜ŽLevel up your Linux Shell
          • File & Dir. Commands
          • Searching Commands
          • šŸ˜ŽChatGPT-based Terminal
          • SysAdmin Commands
          • Network Commands
          • Hacker Commands
        • Busybox
        • Shell Script
          • Awk Script
          • Bash Shell Script
            • Bash Snippets
            • Bash Useful Examples
      • Anatomy of Linux Kernel
        • Linux Kernel Principles
        • Linux Environment for Developer
      • Anatomy of Embedded Linux
        • Embedded Linux
        • Host & Target
        • Cross Toolchains
        • Bootloader
        • Building Embedded Linux
    • Linux OS Dev. Engineer
      • Process Management
        • Process Basic
        • Process State
        • Basic Process Mgmt. Commands
        • Advance Process Mgmt. Commands
        • Process API Programming
      • IPC
        • IPC Anatomy
        • Signal Programming
        • Pipe Programming
        • FIFO Programming
        • Msg. Queue Programming
          • System V
        • Share Memory Programming
          • System V
        • Socket Programming
      • POSIX Threads
        • Multi-tasking Basic
        • POSIX Thread Anatomy
        • Threading Programming
      • Applied IPC
        • Remote Commander
        • Multi-Remote Commanders
      • Process Synchronization
        • Mutex Programming
        • Semaphore Programming
      • Applied IPC with Semaphore
  • ⌚Embedded Systems Development
    • Introduction to ESD
      • Why's ESD?
      • What it use for?
      • How it works?
    • Enbedded System Development via PSoC6
      • Basic MCU Interfacing
        • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
        • Development Environment Preparation
        • PSoCā„¢ 6S2 Peripherals Interfacing (GPIO)
          • Hello World and LED Blinking
          • GPIO Principles
          • PSoCā„¢ 6S2 GPIO-HAL LED Blink Lab
          • PSoCā„¢ 6S2 GPIO-PDL LED Blink Lab
          • Button "Bounce" Principles
            • Push/Pull Button to Turn ON/OFF LED via HAL
            • Push/Pull Button to Turn ON/OFF LED via PDL
            • GPIO Button Interrupt via HAL
            • GPIO Button Interrupt via PDL
          • GPIO variables & functions
        • PSoCā„¢ 6S2 Peripherals Interfacing (ADC, PWM)
          • PSoCā„¢ 6S2 SAR ADC
            • ADC Principles
            • PSoCā„¢ 6S2 with ADC Labs
              • Reading potentiometer sensor value via an ADC HAL
              • Reading potentiometer sensor value via an ADC PDL
          • PSoCā„¢ 6S2 PWM & TCPWM
            • PWM Principles
            • PSoCā„¢ 6S2 for PMW Function Labs
              • LED Brightness using PWM via HAL
              • LED Brightness using PWM via PDL
      • Sensor Interfacing and HMI
        • OLED Display
          • OLED Display Principles
          • Calling BDH’s OLED functions
          • Display ADC via Potentiometer on OLED
        • BDH Shell
          • Shell Principles
          • LED Blinking and CAPSENSE via BDH Shell
          • Adding "History" command
          • Adding "Reboot" command
          • CAPSENSE Button and Slider
            • CAPSENSE Button and Slider with Capsense Tuner
            • CAPSENSE Button and Slider using FreeRTOS
      • Serial Communication & Visualization
        • UART, I2C, SPI Communication via Infineon PSoCā„¢6
        • BMX160 Sensor Communication via Infineon PSoCā„¢6
          • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
          • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
          • Motion Sensors GUI Integration via Serial Studio
    • Edge Computing and IoT Connectivity
    • Cloud-Based Data Analytics and Digital Twin
    • Edge Vision AI
    • Resources
      • Basic Hardware and Firmware
        • Environment Preparation
          • การตณดตั้งโปรแกรด Arduino IDE
            • ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ą¹€ąø£ąø“ą¹ˆąø”ąø•ą¹‰ąø™ą¹ƒąøŠą¹‰ąø‡ąø²ąø™ Arduino IDE
          • ąøąø²ąø£ąø•ąø“ąø”ąø•ąø±ą¹‰ąø‡ą¹‚ąø›ąø£ą¹ąøąø£ąø”ąøŖąø³ąø«ąø£ąø±ąøšą¹ƒąøŠą¹‰ąø‡ąø²ąø™ą¹€ąø„ąø£ąø·ą¹ˆąø­ąø‡ąø”ąø·ąø­ąø§ąø±ąø” NI MyDAQ
            • ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ąø•ąø±ą¹‰ąø‡ąø„ą¹ˆąø²ą¹ƒąøŠą¹‰ Digital Multimeter -NI ELVISmx
            • ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøąø²ąø£ąø•ąø±ą¹‰ąø‡ąø„ą¹ˆąø²ą¹ƒąøŠą¹‰ Oscilloscope-NI ELVISmx
          • ตณดตั้งโปรแกรด KingstVIS
        • Basic measurement
          • Basic Digital and Analog I/O
            • LAB: Basic Digital Input/Output
            • LAB: Basic Analog Input/Output
          • Waveform
            • LAB: Oscilloscope
            • LAB: Oscilloscope and Function Generator
            • LAB: Pulse Width Modulation (PWM)
              • Homework
        • Interfacing and Communication
          • LAB: UART, RS485, RS232 Protocol
          • LAB: I2C Protocol
            • HOMEWORK
          • LAB: SPI Protocol
      • IoT Connectivity
        • Example: IoT with MQTT on Node-red
        • Data logger
        • LAB: Data Visualization
  • šŸ› ļøC/C++ for Embedded Programming
    • Development Environment Preparation
      • ตณดตั้ง WSL 2
      • ตณดตั้ง Ubuntu environment
      • ตณดตั้งโปรแกรด Visual Studio Code
      • ąøąø²ąø£ą¹€ąøŠąø·ą¹ˆąø­ąø”ąø•ą¹ˆąø­ Virtual studio code ą¹€ąø‚ą¹‰ąø²ąøąø±ąøš WSL
      • ตณดตั้ง docker on WSL
    • Principle C/C++ Programming
      • Get started with C++
      • Makefile
        • Makefile Examples
      • Compiling and running
        • How to create a program that you can enter inputs.
          • Lab 1 Exercise
      • Arguments
        • Command line arguments in C and C++
      • signed and unsigned data types
      • Variable and Operator
      • If and If else
      • Loop, Infinite loop, and flag
        • Loop and Flag exercise
      • Array
        • Get to know with arrays
        • Implement example
      • Vector
    • Object Oriented Programming (OOP) in C++
      • Class and Object
      • Encapsulation and Abstraction
      • Polymorphism and Inheritance
    • C/C++ Preprocessing
      • Macro
        • Quiz Macro
      • File Inclusion
      • Conditional Compilation
      • Pragma directive
        • Quiz Pragma
    • String in C++
      • Concatenation
      • Split
    • Type conversions for C/C++
      • Conversion using Cast operator
    • Error handling
    • Data logger
      • ąøąø²ąø£ąøŖąø£ą¹‰ąø²ąø‡ą¹„ąøŸąø„ą¹Œą¹ąø„ąø°ą¹€ąø‚ąøµąø¢ąø™ą¹„ąøŸąø„ą¹Œ
      • ąøąø²ąø£ąø­ą¹ˆąø²ąø™ą¹„ąøŸąø„ą¹Œ
      • ąøąø²ąø£ą¹€ąøą¹‡ąøšąø‚ą¹‰ąø­ąø”ąø¹ąø„ąøąø±ąøšTime stamp
    • High performance programing
      • Multi-task and Multi-thread
        • Multi-threading example
      • Mutex
      • Queue
      • OpenCV
    • C/C++ Techniques
      • Makefile in action
      • Object Oriented Programming (OOP) in C++
        • Class and Object
        • Encapsulation and Abstraction
        • Polymorphism and Inheritance
      • C/C++ Preprocessing
        • Macro
          • Quiz Macro
        • File Inclusion
        • Conditional Compilation
        • Pragma directive
          • Quiz Pragma
      • Binary, Octal and Hexadecimal Numbers
      • Array and properties of an array
        • Get to know with arrays
        • Implement example
      • What's next?
  • šŸ¤–Artificial Intelligence (AI)
    • VAMStack Design House, BUU
    • Data Analytics
      • Data cleansing
      • Data analytics
      • Data analytic exercise
    • Machine Learning
      • Neural Network Layers
      • Machine learning type
      • Dataset
      • Using Edge Impulse for AI Model
    • Basic Image Processing
      • Computer Vision using Python Language
        • Installation
        • Computer Vision Basics
          • Pixel and Color
          • Draw image
          • Basic Image processing
          • Morphology Transformations
          • Gaussian blur
          • Simple Thresholding
          • Contour
          • Canny edge detection
        • Case Study
          • Coin counting
          • Color detection & tracking
        • VAM_CV SDK
  • āš™ļøFPGA Design and Development
    • Verilog HDL via Vivado IDE
      • LAB1: Setting Environment and Create Project
        • Create Vivado Project
      • LAB2: Hardware Description Language Work Flow
        • Simulation code
      • LAB3: Design HDL Project
        • Top Level Design
        • Top-level Simulation
      • LAB4: Asynchronous VS Synchronous Circuit
        • Simulation Synchronous counter
    • C/C++ Programming on Ultra96v2 FPGA Board
      • Application C/C++ on Ultra96v2 Part 1
        • Design Overview
        • Step 1 - Burn the image to SD card
        • Step 2 - Bring up Ultra96v2
        • Step 3 - Installing the Vitis-AI runtime packages
      • Application C/C++ on Ultra96v2 Part 2
        • STEP 1 : Setting auto boot Wifi
        • STEP 2 : How to working on Embedded
        • STEP 3 : How to run the test code
  • šŸ¤–Robotics
    • Dobot Magician
      • Instruction of Dobot
      • Software Download
      • Basically of Program
        • Teaching and Playback
        • Write and Draw
        • LaserEngraving
        • 3D Printer
    • Robotino
      • Software Download
        • Robotino View
        • Robotino SIM
      • Charging
      • Connecting
      • Follow Line example
        • Basic block in Follow Line
    • RaspBlock
      • Get Started with Raspblock
  • 🚩Special Topics
    • Node-Red
      • Set up Raspberry Pi
      • Install node red in Raspberry Pi
      • Get started with Node Red
        • Open node-red
        • Turn off node red
        • Install Dashboard on Node-red
        • Use node red to show message
        • Using Ultrasonic sensor with node-red
    • IoT Cloud
      • Overview
        • How do they work?
          • Basic Knowlege
      • Installations
        • Install Docker
        • Install Mosquitto Broker
        • Install InfluxDB
        • Install Telegraf
        • Install Grafana
      • Get Sensor Value and Send to MQTT
        • Connect ESP3266 to sensor
        • Connect ESP3266 to MQTT
      • Integration
    • Senses IoT
      • SENSES IoT Platform
      • LAB8: MCU send data to IoT platform
    • CrowPi Dev Kit
      • Raspberry Pi with CrowPi
      • Remote to Raspberry Pi
      • Cross-Compile
        • Lab 1: Programming and cross complier
      • Hardware and Interfaces Usage CLI
        • LAB: Usage GPIO via CLI
        • LAB: Scan I2C bus via CLI
      • Python library for Crow Pi
      • wiringPi library (C) for CrowPi
        • Lab2: Crowpi and sensors
    • LVGL Development
      • LVGL - Light and Versatile Embedded Graphics Library
        • Setting program for LVGL Simulator
        • Get started with LVGL simulator
        • Example Library of LVGL
        • Create your own screen
          • Exercise
        • Style
          • Exercise
        • Event
    • Docker OS
      • Docker OS Part 1
        • Part 1 : Installation
        • Part 2 : Basic Docker OS and Linux CLI
      • Docker OS Part 2
        • Part 1 : Docker communication
        • Part 2 : Docker compose
      • Application Gstreamer on devcontainer
        • STEP 1 : Setting gstreamer environment
        • STEP 2 : Create the Gstreamer element on template
        • STEP 3 : Testing and application on your gst element
  • 🤟Recommended by AIC
    • Skill Roadmap
      • Embedded Engineer
      • Developer
    • Hardware Programming
    • Embedded Programming
    • General-propose Programming
    • Algorithmica
    • Thai Expert Knowledge
    • RT-Thread University Program
      • Infineon PSoC6
      • Kernel
        • Kernel Basics
        • Thread Management
        • Clock Management
        • Inter-thread synchronization
        • Inter-thread communication
        • Memory Management
        • Interrupt Management
        • Kernel porting
        • Atomic Operations
        • RT-Thread SMP
        • Kernel API Changelog
      • Tools
      • Devices & Drivers
        • SENSOR Devices
        • Touch Equipment
        • CRYPTO Devices
        • AUDIO Devices
        • Pulse Encoder Devices
      • Components
        • C Library (libc)
        • ISO/ANSI C Standard
        • POSIX Standard
          • FILE (File IO)
          • Pthread
          • Timer
          • IPC Semaphore
          • IPC Message Queues
          • Dynamic Modules
        • Network Components
          • FinSH Console
          • FAL: Flash Abstraction Layer
          • Virtual File System
          • tmpfs: temporary file system
          • ulog log
          • utest testing framework
          • Power Management
          • RT-Link
        • Software Packages
          • Internet of Things
            • MQTT-umqtt
            • Telnet
          • Tools
            • SystemView
            • SEGGER_RTT
          • LVGL Manual
            • Touch Screen Driver
      • Demo
        • Infineon Gateway
        • Handwriting Recognition (MNIST)
        • Object Detection (Darknet)
        • ROS using RT-Thread
        • Control the car using RT-Thread
        • LiDAR via RT-Thread
        • Detection via RT-Thread and ROS
        • Sensor Driver Development Guide
Powered by GitBook

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University

On this page
  • Polymorphism
  • 1. Function Overloading
  • 2. Operator Overloading
  • Inheritance
  • 1. Single Inheritance
  • 2. Multiple Inheritance
  • 3. Multilevel Inheritance
  • Access Specifiers for Inheritance

Was this helpful?

  1. C/C++ for Embedded Programming
  2. C/C++ Techniques
  3. Object Oriented Programming (OOP) in C++

Polymorphism and Inheritance

Polymorphism

Polymorphism หดายถึง ąøąø²ąø£ąø”ąøµąø«ąø„ąø²ąø¢ąø£ąø¹ąø›ą¹ąøšąøš หรือก็คือ ą¹ƒąø™ 1 ąøąø£ąø°ąøšąø§ąø™ąøąø²ąø£ąø«ąø£ąø·ąø­ąøąø²ąø£ąøąø£ąø°ąø—ąø³ąøŖąø²ąø”ąø²ąø£ąø–ą¹ąøŖąø”ąø‡ąøœąø„ąø„ąø±ąøžąø˜ą¹Œąø«ąø£ąø·ąø­ą¹ąøŖąø”ąø‡ąøœąø„ąøąø²ąø£ąøąø£ąø°ąø—ąø³ą¹„ąø”ą¹‰ąø”ąø²ąøąøąø§ą¹ˆąø²ąø«ąø™ąø¶ą¹ˆąø‡ąø£ąø¹ąø›ą¹ąøšąøš ą¹ąøšą¹ˆąø‡ąø­ąø­ąøą¹€ąø›ą¹‡ąø™ 2 ประเภท

1. Function Overloading

ąøąø²ąø£ą¹ƒąøŠą¹‰ąøŠąø·ą¹ˆąø­ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ą¹€ąø«ąø”ąø·ąø­ąø™ąøąø±ąø™ ą¹ąø•ą¹ˆąø”ąøµąø„ąø§ąø²ąø”ąøŖąø²ąø”ąø²ąø£ąø–ąø«ąø£ąø·ąø­ąø„ąøøąø“ąø„ąø±ąøąø©ąø“ąø°ąø—ąøµą¹ˆą¹ąø•ąøąø•ą¹ˆąø²ąø‡ąøąø±ąø™ ąø«ąø”ąø²ąø¢ąø„ąø§ąø²ąø”ąø§ą¹ˆąø² ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ąøŖąø²ąø”ąø²ąø£ąø–ą¹€ąø›ąø„ąøµą¹ˆąø¢ąø™ąøˆąø³ąø™ąø§ąøąø²ąø£ąø£ąø±ąøšąøžąø²ąø£ąø²ąø”ąø“ą¹€ąø•ąø­ąø£ą¹Œąø«ąø£ąø·ąø­ą¹€ąø›ąø„ąøµą¹ˆąø¢ąø™ąø›ąø£ąø°ą¹€ąø ąø—ąø•ąø±ąø§ą¹ąø›ąø£ąø‚ąø­ąø‡ąøžąø²ąø£ąø²ąø”ąø“ą¹€ąø•ąø­ąø£ą¹Œ

// oop_p3_1.cpp
#include <iostream>
using namespace std;

class function_overlasding 
{ 
    public: 
      
    // function with 1 int parameter 
    int func(int x) { 
        return x + 5; 
    } 
      
    // function with same name but 1 double parameter 
    void func(double x) { 
        cout << "value of x is " << x << endl; 
    } 
      
    // function with same name and 2 int parameters 
    void func(int x, double y) { 
        cout << "value of x and y is " << x << ", " << y << endl; 
    } 
}; 
  
int main() { 
      
    function_overlasding obj1; 
      
    // Which function is called will depend on the parameters passed 
    // The first 'func' is called  
    int a = obj1.func(7);
    cout << a  << endl; 
      
    // The second 'func' is called 
    obj1.func(9.132); 
      
    // The third 'func' is called 
    obj1.func(85,9.132); 
    return 0; 
}  
12
value of x is 9.132
value of x and y is 85, 9.132

ąøˆąø²ąøąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøˆąø°ą¹€ąø«ą¹‡ąø™ą¹„ąø”ą¹‰ąø§ą¹ˆąø²ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ąø—ąøµą¹ˆąøŠąø·ą¹ˆąø­ func ąø—ąø³ąø«ąø™ą¹‰ąø²ąø—ąøµą¹ˆą¹ąø•ąøąø•ą¹ˆąø²ąø‡ąøąø±ąø™ą¹ƒąø™ 3 ąøŖąø–ąø²ąø™ąøąø²ąø£ąø“ą¹Œ (ąøąø²ąø£ąø£ąø±ąøšąøžąø²ąø£ąø²ąø”ąø“ą¹€ąø•ąø­ąø£ą¹Œ) ąø—ąøµą¹ˆą¹ąø•ąøąø•ą¹ˆąø²ąø‡ąøąø±ąø™ ąø‹ąø¶ą¹ˆąø‡ą¹€ąø›ą¹‡ąø™ąø„ąøøąø“ąøŖąø”ąøšąø±ąø•ąø“ąø‚ąø­ąø‡ Polymorphism

2. Operator Overloading

operator ąø—ąøµą¹ˆąø—ąø³ąø‡ąø²ąø™ą¹„ąø”ą¹‰ąø«ąø„ąø²ąø¢ą¹ąøšąøš ą¹ƒąø™ąø«ąø„ąø±ąøąøąø²ąø£ą¹€ąøŠąø“ąø‡ąø§ąø±ąø•ąø–ąøøąøˆąø°ąøŠą¹ˆąø§ąø¢ą¹ƒąø«ą¹‰ąøŖąø²ąø”ąø²ąø£ąø–ą¹€ąø£ąøµąø¢ąøą¹ƒąøŠą¹‰ą¹€ąø„ąø£ąø·ą¹ˆąø­ąø‡ąø«ąø”ąø²ąø¢ą¹€ąøŠą¹ˆąø™ +, -, *, / ą¹ąø—ąø™ąøąø²ąø£ą¹€ąø£ąøµąø¢ąøą¹ƒąøŠą¹‰ą¹€ąø”ąø”ąø˜ąø­ąø”ąø«ąø£ąø·ąø­ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ąø‚ąø­ąø‡ object

// oop_p3_2.cpp
#include <iostream>
using namespace std;
   
class Complex { 
    private: 
        int real, imag; 
    public: 
        // first section code
        Complex(int r = 0, int i =0) {
            real = r;   imag = i;
        } 
        
        // second section code
        // This is automatically called when '+' is used with 
        // between two Complex objects 
        Complex operator + (Complex const &obj) { 
            Complex res; 
            res.real = real + obj.real; 
            res.imag = imag + obj.imag; 
            return res; 
        } 
        void print() { 
            cout << real << " + i" << imag << endl; 
        } 
}; 
   
int main() 
{ 
    Complex c1(10, 5), c2(2, 4); 
    Complex c3 = c1 + c2; // An example call to "operator+" 
    c3.print(); 
} 
12 + i9

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ą¹ƒąø«ą¹‰ąøØąø¶ąøąø©ąø²ą¹€ąøžąø“ą¹ˆąø”ą¹€ąø•ąø“ąø” :: การ overload operator ą¹ƒąø™ C++

Inheritance

Inheritance ąø„ąø·ąø­ ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø„ąø§ąø²ąø”ąøŖąø²ąø”ąø²ąø£ąø–ąø‚ąø­ąø‡ class ą¹ƒąø™ąøąø²ąø£ąø£ąø±ąøšąø„ąøøąø“ąøŖąø”ąøšąø±ąø•ąø“ą¹ąø„ąø°ąø„ąø±ąøąø©ąø“ąø°ąøˆąø²ąø class ąø­ąø·ą¹ˆąø™

ąøØąø±ąøžąø¢ą¹Œąø—ąøµą¹ˆąø„ąø§ąø£ąø£ąø¹ą¹‰ ::

Sub Class ąø„ąø·ąø­ ąø„ąø„ąø²ąøŖąø—ąøµą¹ˆąøŖąø·ąøšąø—ąø­ąø”ąø„ąøøąø“ąøŖąø”ąøšąø±ąø•ąø“ąøˆąø²ąøąø„ąø„ąø²ąøŖąø­ąø·ą¹ˆąø™ ą¹€ąø£ąøµąø¢ąøąø§ą¹ˆąø² ąø„ąø„ąø²ąøŖąø—ąøµą¹ˆą¹„ąø”ą¹‰ąø£ąø±ąøš (Derived Class)

Super Class ąø„ąø·ąø­ ąø„ąø„ąø²ąøŖąø—ąøµą¹ˆąø”ąøµąø„ąøøąø“ąøŖąø”ąøšąø±ąø•ąø“ąøŖąø·ąøšąø—ąø­ąø”ąø«ąø£ąø·ąø­ąø—ąøµą¹ˆąø£ąø¹ą¹‰ąøˆąø±ąøąøąø±ąø™ąø§ą¹ˆąø²ą¹€ąø›ą¹‡ąø™ąø„ąø„ąø²ąøŖąø•ąø±ą¹‰ąø‡ąø•ą¹‰ąø™ (Base Class)

ąøˆąø²ąøąø ąø²ąøž ąø«ąø”ąø²ąø¢ąø„ąø§ąø²ąø”ąø§ą¹ˆąø² Animal class ąø–ąø·ąø­ąø§ą¹ˆąø²ą¹€ąø›ą¹‡ąø™ Base Class ąøŖą¹ˆąø§ąø™ dog class, Cat class แคะ Cow class ąøˆąø°ą¹€ąø›ą¹‡ąø™ą¹€ąøžąøµąø¢ąø‡ Derived Class ąø‚ąø­ąø‡ Animal class ąø‹ąø¶ą¹ˆąø‡ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø„ąø§ąø²ąø”ąøŖąø²ąø”ąø²ąø£ąø–ąøˆąø°ąø”ąøµąø«ąø„ąø²ąø¢ą¹ąøšąøš ą¹ąø•ą¹ˆą¹ƒąø™ąø«ąø±ąø§ąø‚ą¹‰ąø­ąø™ąøµą¹‰ąøˆąø°ąøąø„ą¹ˆąø²ąø§ąø–ąø¶ąø‡ą¹€ąøžąøµąø¢ąø‡ą¹ąø„ą¹ˆ 3 แบบ ąø„ąø·ąø­ ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø—ąø²ąø‡ą¹€ąø”ąøµąø¢ąø§ (Single Inheritance) ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø«ąø„ąø²ąø¢ąø£ąø²ąø¢ąøąø²ąø£ (Multiple Inheritance) ą¹ąø„ąø°ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø«ąø„ąø²ąø¢ąø™ąø°ąø”ąø±ąøš (Multilevel Inheritance)

1. Single Inheritance

ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø—ąø²ąø‡ą¹€ąø”ąøµąø¢ąø§ ąø«ąø”ąø²ąø¢ąø„ąø§ąø²ąø”ąø§ą¹ˆąø² 1 Derived Class ąøŖąø²ąø”ąø²ąø£ąø–ąø£ąø±ąøšąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ą¹„ąø”ą¹‰ 1 Base Class ą¹€ąø—ą¹ˆąø²ąø™ąø±ą¹‰ąø™

// oop_p3_3.cpp
#include <iostream>
#include <string>
using namespace std;

// Base class
class Vehicle {
  public: 
    string brand = "Ford";
    void honk() {
      cout << "Tuut, tuut! \n" ;
    }
};

// Derived class
class Car: public Vehicle {
  public: 
    string model = "Mustang";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model << endl;
  return 0;
}
Tuut, tuut! 
Ford Mustang

2. Multiple Inheritance

ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø«ąø„ąø²ąø¢ąø«ąø„ąø²ąø¢ąøąø²ąø£ ąø«ąø”ąø²ąø¢ąø„ąø§ąø²ąø”ąø§ą¹ˆąø²ą¹ƒąø™ 1 Derived Class ąøŖąø²ąø”ąø²ąø£ąø–ąøŖąø·ąøšąø—ąø­ąø”ą¹„ąø”ą¹‰ąø”ąø²ąøąø§ą¹ˆąø² 1 Base Class

// oop_p3_4.cpp
#include <iostream> 
using namespace std; 
  
// first base class 
class Vehicle { 
  public: 
    Vehicle() 
    { 
      cout << "This is a Vehicle" << endl; 
    } 
}; 
  
// second base class 
class FourWheeler { 
  public: 
    FourWheeler() 
    { 
      cout << "This is a 4 wheeler Vehicle" << endl; 
    } 
}; 
  
// sub class derived from two base classes 
class Car: public Vehicle, public FourWheeler { 
  
}; 
  
// main function 
int main() 
{    
    // creating object of sub class will 
    // invoke the constructor of base classes 
    Car obj; 
    return 0; 
} 
This is a Vehicle
This is a 4 wheeler Vehicle

3. Multilevel Inheritance

ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø«ąø„ąø²ąø¢ąø™ąø°ąø”ąø±ąøš ąø«ąø”ąø²ąø¢ąø„ąø§ąø²ąø”ąø§ą¹ˆąø² Derived Class ąøŖąø²ąø”ąø²ąø£ąø–ąø£ąø±ąøšąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø” Base Class ąø—ąøµą¹ˆąøŖąø·ąøšąø—ąø­ąø”ąøąø±ąø™ąø”ąø²ąøą¹ˆąø­ąø™ą¹„ąø”ą¹‰

ąøˆąø²ąøąø ąø²ąøžąøˆąø°ą¹€ąø«ą¹‡ąø™ą¹„ąø”ą¹‰ąø§ą¹ˆąø² Class C ąøˆąø°ą¹€ąø›ą¹‡ąø™ Base Class ąø«ąø„ąø±ąøąø‚ąø­ąø‡ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø” ąøŖą¹ˆąø§ąø™ Class B ąøˆąø°ą¹€ąø›ą¹‡ąø™ąø—ąø±ą¹‰ąø‡ Derived Class ąø‚ąø­ąø‡ Class C แคะเป็น Base Class ąø‚ąø­ąø‡ Class A ąø­ąøµąøąø—ąøµąø«ąø™ąø¶ą¹ˆąø‡ ąø—ąø³ą¹ƒąø«ą¹‰ Class A ąø—ąøµą¹ˆą¹€ąø›ą¹‡ąø™ Derived Class ąøŖąøøąø”ąø—ą¹‰ąø²ąø¢ąøŖąø²ąø”ąø²ąø£ąø–ąøŖąø·ąøšąø—ąø­ąø”ą¹„ąø”ą¹‰ąø—ąø±ą¹‰ąø‡ąø„ąøøąø“ąøŖąø”ąøšąø±ąø•ąø“ąø‚ąø­ąø‡ Class B แคะ Class C

// oop_p3_5.cpp
#include <iostream> 
using namespace std; 
  
// base class 
class Vehicle  
{ 
  public: 
    Vehicle() 
    { 
      cout << "This is a Vehicle" << endl; 
    } 
}; 
class fourWheeler: public Vehicle 
{  public: 
    fourWheeler() 
    { 
      cout<<"Objects with 4 wheels are vehicles"<<endl; 
    } 
}; 
// sub class derived from two base classes 
class Car: public fourWheeler{ 
   public: 
     Car() 
     { 
       cout<<"Car has 4 Wheels"<<endl; 
     } 
}; 
  
// main function 
int main() 
{    
    //creating object of sub class will 
    //invoke the constructor of base classes 
    Car obj; 
    return 0; 
} 
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels

ąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ą¹ƒąø«ą¹‰ąøØąø¶ąøąø©ąø²ą¹€ąøžąø“ą¹ˆąø”ą¹€ąø•ąø“ąø” :: Inheritance in C++

Access Specifiers for Inheritance

ąø­ąø¢ą¹ˆąø²ąø‡ąø—ąøµą¹ˆąø—ąø£ąø²ąøšąøąø±ąø™ąø§ą¹ˆąø² Access Specifiers ąøˆąø°ąø”ąøµąø—ąø±ą¹‰ąø‡ąø«ąø”ąø” 3 แบบ ąø„ąø·ąø­ public, private แคะสุดท้ายคือ protected ąø—ąøµą¹ˆąøˆąø°ąøąø„ą¹ˆąø²ąø§ąø–ąø¶ąø‡ą¹ƒąø™ąø«ąø±ąø§ąø‚ą¹‰ąø­ąø™ąøµą¹‰ ąø‹ąø¶ą¹ˆąø‡ protected ąøˆąø°ą¹€ąø›ą¹‡ąø™ąøąø²ąø£ąø­ąø™ąøøąøąø²ąø•ąø“ąøąø²ąø£ą¹€ąø‚ą¹‰ąø²ąø–ąø¶ąø‡ąø‚ą¹‰ąø­ąø”ąø¹ąø„ą¹€ąø‰ąøžąø²ąø° class ąø—ąøµą¹ˆą¹€ąø›ą¹‡ąø™ Derived class ą¹€ąø—ą¹ˆąø²ąø™ąø±ą¹‰ąø™

ąø–ąø¶ąø‡ą¹ąø”ą¹‰ąø§ą¹ˆąø²ą¹ƒąø™ Inheritance ąøˆąø°ą¹€ąø›ą¹‡ąø™ąøąø²ąø£ąøŖąø·ąøšąø—ąø­ąø”ąø„ąø§ąø²ąø”ąøŖąø²ąø”ąø²ąø£ąø–ąø‚ąø­ąø‡ class ą¹ąø•ą¹ˆą¹„ąø”ą¹ˆąøŖąø²ąø”ąø²ąø£ąø–ąøŖąø·ąøšąø—ąø­ąø”ąø„ąø§ąø²ąø”ąøŖąø²ąø”ąø²ąø£ąø–ąø—ąøµą¹ˆąø–ąø¹ąøąøˆąø³ąøąø±ąø”ąøąø²ąø£ą¹€ąø‚ą¹‰ąø²ąø–ąø¶ąø‡ą¹ąøšąøš private ąø—ąøµą¹ˆąø­ąø¢ąø¹ą¹ˆą¹ƒąø™ Base class ąø«ąø”ąø²ąø¢ąø„ąø§ąø²ąø”ąø§ą¹ˆąø² ą¹„ąø”ą¹ˆąøŖąø²ąø”ąø²ąø£ąø–ąøŖąø£ą¹‰ąø²ąø‡ą¹€ąø”ąø”ąø˜ąø­ąø”ąø«ąø£ąø·ąø­ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ąø—ąøµą¹ˆąøˆąø°ą¹€ąø‚ą¹‰ąø²ąø–ąø¶ąø‡ąø‚ą¹‰ąø­ąø”ąø¹ąø„ąø—ąøµą¹ˆą¹€ąø›ą¹‡ąø™ private ąø‚ąø­ąø‡ Base class ą¹€ąø™ąø·ą¹ˆąø­ąø‡ąøˆąø²ąøą¹€ąø”ąø”ąø˜ąø­ąø”ąø«ąø£ąø·ąø­ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ąø—ąøµą¹ˆąøŖąø£ą¹‰ąø²ąø‡ąø­ąø¢ąø¹ą¹ˆąø™ąø­ąø class ąø—ąøµą¹ˆąø”ąøµąø‚ą¹‰ąø­ąø”ąø¹ąø„ąø—ąøµą¹ˆąø–ąø¹ąøąøąø³ąø«ąø™ąø”ą¹€ąø›ą¹‡ąø™ private

// oop_p3_6.cpp
#include <iostream>
using namespace std;

// Base class
class Employee  {
  protected:  // Protected access specifier
    int salary;
};

// Derived class
class Programmer: public Employee {
  public:
    int bonus;
    void setSalary(int s) {
      salary = s;
    }
    int getSalary() {
      return salary;
    }
};

int main() {
  Programmer myObj;
  myObj.setSalary(50000);
  myObj.bonus = 15000;
  cout << "Salary: " << myObj.getSalary() << "\n";
  cout << "Bonus: " << myObj.bonus << "\n";
  return 0;
}
// oop_p3_7.cpp
#include <iostream>
using namespace std;

// Base class
class Employee  {
  private:  // Private access specifier
    int salary;
  public:
    void set(int c){
      salary = c;
    }
    int get(){
      return salary;
    }
    
};

// Derived class
class Programmer: public Employee {
  public:
    int bonus;
    void setSalary(int s) {
      set(s);
    }
    int getSalary() {
        int ss = get();
      return ss;
    }
};

int main() {
  Programmer myObj;
  myObj.setSalary(50000);
  myObj.bonus = 15000;
  cout << "Salary: " << myObj.getSalary() << "\n";
  cout << "Bonus: " << myObj.bonus << "\n";
  return 0;
}
Salary: 50000
Bonus: 15000

ą¹ąø«ąø„ą¹ˆąø‡ąø­ą¹‰ąø²ąø‡ąø­ąø“ąø‡ ::

  • https://www.geeksforgeeks.org/polymorphism-in-c/?ref=lbp

  • https://www.geeksforgeeks.org/operator-overloading-c/

  • https://www.w3schools.com/cpp/cpp_inheritance_access.asp

คำถาด สาดารถนำ Polymorphism ąø”ąø²ą¹ƒąøŠą¹‰ąøąø±ąøš Inheritance ą¹„ąø”ą¹‰ąø«ąø£ąø·ąø­ą¹„ąø”ą¹ˆ

ąø„ąø³ąø•ąø­ąøš สาดารถทำได้

// oop_p3_8.cpp
#include <iostream> 
using namespace std; 

// Base class
class Animal {
  public:
    void animalSound() {
    cout << "The animal makes a sound \n" ;
  }
};

// Derived class
class Pig : public Animal {
  public:
    void animalSound() {
    cout << "The pig says: wee wee \n" ;
   }
};

// Derived class
class Dog : public Animal {
  public:
    void animalSound() {
    cout << "The dog says: bow wow \n" ;
  }
};

int main() {
  Animal myAnimal;
  Pig myPig;
  Dog myDog;

  myAnimal.animalSound();
  myPig.animalSound();
  myDog.animalSound();
  return 0;
}
The animal makes a sound 
The pig says: wee wee 
The dog says: bow wow 

ąøˆąø²ąøąø•ąø±ąø§ąø­ąø¢ą¹ˆąø²ąø‡ąøˆąø°ą¹€ąø«ą¹‡ąø™ą¹„ąø”ą¹‰ąø§ą¹ˆąø²ąøŖąø²ąø”ąø²ąø£ąø–ą¹ƒąøŠą¹‰ąøŠąø·ą¹ˆąø­ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ąø«ąø£ąø·ąø­ą¹€ąø”ąø”ąø˜ąø­ąø”ąø‹ą¹‰ąø³ąøąø±ąø™ą¹„ąø”ą¹‰ąø•ąø²ąø”ąø«ąø„ąø±ąøąøąø²ąø£ąø‚ąø­ąø‡ Polymorphism ą¹ąø•ą¹ˆąøˆąø°ąøąø²ąø£ąø—ąøµą¹ˆąøˆąø°ą¹ąøŖąø”ąø‡ąøœąø„ąø„ąø±ąøžąø˜ą¹Œąø—ąøµą¹ˆą¹ąø•ąøąø•ą¹ˆąø²ąø‡ąøąø±ąø™ąø•ą¹‰ąø­ąø‡ą¹ƒąø«ą¹‰ object ąø›ąø£ąø°ąøąø²ąøØą¹€ąø£ąøµąø¢ąøą¹ƒąøŠą¹‰ class ą¹€ąøžąø·ą¹ˆąø­ą¹€ąø‚ą¹‰ąø²ąø–ąø¶ąø‡ąøŸąø±ąø‡ąøą¹ŒąøŠąø±ąø™ąø«ąø£ąø·ąø­ą¹€ąø”ąø”ąø˜ąø­ąø”ąø—ąøµą¹ˆąø”ąøµąøœąø„ąø„ąø±ąøžąø˜ą¹Œąø•ąø²ąø”ąø—ąøµą¹ˆąø•ą¹‰ąø­ąø‡ąøąø²ąø£

PreviousEncapsulation and AbstractionNextC/C++ Preprocessing

Last updated 2 years ago

Was this helpful?

šŸ› ļø