0% found this document useful (0 votes)
11 views10 pages

Codes

The document provides an introduction to C++, highlighting its uses in games, applications, and microcontrollers like ESP32. It includes examples of simple C++ programs, demonstrating basic syntax and functionality such as input/output and arithmetic operations. Additionally, it encourages further learning about loops, conditions, and microcontroller programming.

Uploaded by

mogobenicole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Codes

The document provides an introduction to C++, highlighting its uses in games, applications, and microcontrollers like ESP32. It includes examples of simple C++ programs, demonstrating basic syntax and functionality such as input/output and arithmetic operations. Additionally, it encourages further learning about loops, conditions, and microcontroller programming.

Uploaded by

mogobenicole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <U8g2lib.

h>

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 18, /*


data=*/ 23, /* CS=*/ 5, /* reset=*/ 4);

void setup() {

u8g2.begin();

void loop() {

u8g2.clearBuffer();

u8g2.setFont(u8g2_font_ncenB14_tr);

u8g2.drawStr(10, 30, "Hello!");

u8g2.sendBuffer();

delay(1000);

Another one

#include <U8g2lib.h>

// Initialize LCD using SPI mode

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 18, /*


data=*/ 23, /* CS=*/ 5, /* reset=*/ 4);

void setup() {

u8g2.begin();

}
void loop() {

u8g2.clearBuffer();

u8g2.setFont(u8g2_font_ncenB14_tr);

u8g2.drawStr(10, 30, "Hello, Michelle!");

u8g2.sendBuffer();

delay(2000);

Graphic#include <U8g2lib.h>

#include <Wire.h>

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 18, /*


data=*/ 23, /* CS=*/ 5, /* reset=*/ 4);

bool eyesOpen = true;

void drawSmileyFace(bool eyesOpen) {

u8g2.drawCircle(64, 32, 15, U8G2_DRAW_ALL); // Face

if (eyesOpen) {

u8g2.drawCircle(58, 26, 2, U8G2_DRAW_ALL); // Left Eye Open

u8g2.drawCircle(70, 26, 2, U8G2_DRAW_ALL); // Right Eye Open

} else {

u8g2.drawLine(55, 26, 61, 26); // Left Eye Closed

u8g2.drawLine(67, 26, 73, 26); // Right Eye Closed

u8g2.drawArc(64, 32, 8, 0, 180, U8G2_DRAW_ALL); // Smile

void setup() {

u8g2.begin();
}

void loop() {

u8g2.clearBuffer();

drawSmileyFace(eyesOpen);

eyesOpen = !eyesOpen; // Toggle eye state

u8g2.sendBuffer();

delay(1000);

Simple coding
#include <iostream> // Adds the ability to display text

using namespace std; // Allows us to use standard commands

int main() { // The starting point of the program

cout << "Hello, Michelle! Welcome to coding!" << endl; // Prints text
on the screen

return 0; // Ends the program

}#include <iostream> // Allows input and output

using namespace std; // Simplifies code writing


int main() {

string name; // Creates a variable to store the name

cout << "What is your name? "; // Asks for the name

cin >> name; // Takes input from the user

cout << "Hello, " << name << "! Welcome to coding!" << endl; //
Displays a personalized message

return 0;

Asks your name

C++
## **📌 Introduction to C++ for Beginners**

### **🔹 What is C++?**


C++ is a **powerful programming language**
used for **games, applications, software,
robotics, and microcontrollers** like ESP32. It is
one of the most widely used programming
languages in the world.

🔹 **Created by:** Bjarne Stroustrup in 1983


🔹 **Used for:** Games, AI, Operating Systems,
Embedded Systems (like ESP32)
🔹 **Popular in:** Microsoft, Google, Game
Development, Robotics

---

## **📌 Why Learn C++?**


✅ **Fast & Powerful** – Used for high-
performance applications
✅ **Used in Microcontrollers** – Helps program
ESP32, Arduino, and robotics
✅ **Game Development** – Used in Unreal Engine
& game programming
✅ **Foundation for Other Languages** – Learning
C++ helps you learn Python, Java, and more!

---

## **📌 Basic Structure of a C++ Program**


Every C++ program has **three main parts**:
1️⃣ **Preprocessor Directives** (`#include`) –
Adds built-in features
2️⃣ **Main Function** (`int main()`) – The starting
point of the program
3️⃣ **Statements** (`cout`, `cin`) – Instructions
for the computer
---

### **📌 Example 1: Your First C++ Program**


Let’s write a simple **"Hello, World!"** program:

```cpp
#include <iostream> // Adds input and output
functions

using namespace std; // Simplifies code writing

int main() { // The main function (starting point)


cout << "Hello, Michelle! Welcome to C++!"
<< endl; // Prints text on the screen
return 0; // Ends the program
}
```

### **🔹 Explanation**


- `#include <iostream>` → Allows us to use
**input & output** commands.
- `using namespace std;` → Makes coding
**easier** by removing the need for `std::`.
- `int main() { ... }` → This is the **main
function** (where the program starts).
- `cout << "Hello, Michelle!";` → Displays text on
the screen.
- `return 0;` → Ends the program.

---

### **📌 Example 2: Asking for Your Name**


```cpp
#include <iostream>

using namespace std;

int main() {
string name; // Creates a variable to store the
name

cout << "What is your name? ";


cin >> name; // Takes user input

cout << "Nice to meet you, " << name << "!"
<< endl; // Outputs the name

return 0;
}
```

### **🔹 What’s New?**


- `string name;` → Stores your name.
- `cin >> name;` → Allows user **input** (you
type your name).
- `cout << "Nice to meet you!";` → Displays the
greeting.

---

### **📌 Example 3: Simple Math (Addition)**


```cpp
#include <iostream>

using namespace std;

int main() {
int num1, num2, sum; // Creates variables

cout << "Enter first number: ";


cin >> num1; // Takes first number

cout << "Enter second number: ";


cin >> num2; // Takes second number

sum = num1 + num2; // Adds the numbers

cout << "The sum is: " << sum << endl; //
Displays result

return 0;
}
```

### **🔹 What’s Happening?**


- `int num1, num2, sum;` → Creates **integer
variables**.
- `cin >> num1;` → Takes a number from the user.
- `sum = num1 + num2;` → Adds the numbers.
- `cout << sum;` → Prints the result.

---

## **📌 What’s Next?**


Would you like to learn **loops, conditions, or
more about microcontrollers (ESP32)?** 🚀😊

You might also like