7. Constructor with Parameters

#include <iostream>
using namespace std;

class UART {
public:
    UART(int baudRate, char parity) {
        cout << "UART initialized with baud=" << baudRate
             << ", parity=" << parity << "\n";
    }
};

int main() {
    UART u(9600, 'N');
    return 0;
}

Solution Explanation

  • The constructor UART(int baudRate, char parity) takes parameters at creation.
  • UART u(9600, 'N'); passes these values, and the constructor prints them.
     

Layman’s Terms

It’s like buying a phone: when you set it up, you pick a language and Wi-Fi. The constructor is the setup wizard that takes your choices.

 

Significance for Embedded Developers

Firmware often needs configurable initialization (baud rate, parity, SPI mode, timer period, etc.).

  • Constructors with parameters make this neat and error-free compared to setting fields manually later.

     
Loading...

Input

Expected Output

UART initialized with baud=9600, parity=N