C++
I’ve already talked about why we might want to learn C++ earlier. Here, we’ll do a crash course.
This Lesson Will Cover
- 🖥️ Writing a Hello World program in C++
- 📊 Understanding variables and data types
- 🔁 Using loops and control flow
- 🛠️ Writing functions
- 🏗️ Creating classes and objects
- 📚 Using external libraries (OpenCV example)
Hello World
The classic starting point:
#include <iostream> // Include input-output library
int main() {
std::cout << "Hello, World!" << std::endl; // Print to console
return 0; // Indicate successful program execution
}
Here:
#include <iostream>brings in functionality for input/output.std::coutoutputs text.main()is the entry point of any C++ program.
To run this code:
- Save it in a file with a
.cppextension (e.g.,hello.cpp). - Compile the code:
g++ hello.cpp -o hello - Run the program:
- On Windows:
hello - On Linux/macOS:
./hello
- On Windows:
Variables and Data Types
C++ is statically typed, meaning we must declare a variable’s type explicitly. Common types include:
int a = 10; // Integer
float b = 3.14; // Floating-point number
char c = 'A'; // Character
bool d = true; // Boolean
For dynamic memory allocation (e.g., for large datasets), we can use pointers:
int* ptr = new int(5); // Allocate memory for an integer
delete ptr; // Free memory to prevent leaks
This raw-pointer pattern makes us responsible for matching new with delete (and new[] with delete[]). Modern C++ usually ties ownership to object lifetime through RAII: containers such as std::vector and smart pointers such as std::unique_ptr release their owned memory automatically. Explicit allocation and cleanup are available, rather than required in every C++ program.
Loops and Control Flow
Control structures like loops and conditional statements are essential:
for (int i = 0; i < 5; i++) {
std::cout << "Iteration: " << i << std::endl;
}
if (a > b) {
std::cout << "a is greater than b" << std::endl;
}
Functions
Functions help modularize our code:
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(5, 7);
std::cout << "Sum: " << result << std::endl;
return 0;
}
Classes and Objects
C++ supports object-oriented programming (OOP):
class Dataset {
public:
std::string name;
int size;
void load() {
std::cout << "Loading dataset: " << name << std::endl;
}
};
int main() {
Dataset dataset;
dataset.name = "MNIST";
dataset.size = 60000;
dataset.load();
return 0;
}
Using External Libraries (OpenCV Example)
Here’s how to use the OpenCV library for image processing:
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Mat image = cv::imread("image.jpg");
if (image.empty()) {
std::cout << "Error: Could not load image" << std::endl;
return -1;
}
cv::imshow("Image", image);
cv::waitKey(0);
return 0;
}
To run this code:
- Save it in a file with a
.cppextension (e.g.,image.cpp). - Install OpenCV on our system:
- On Ubuntu/Debian:
sudo apt-get install libopencv-dev - On macOS:
brew install opencv
- On Ubuntu/Debian:
- Compile the code:
- On Linux/macOS:
g++ -std=c++11 image.cpp -o image $(pkg-config --cflags --libs opencv4)- The
-std=c++11flag resolves an error on my system. You might need to remove this.
- The
- On Linux/macOS:
- Run the program:
- On Windows:
image - On Linux/macOS:
./image
- On Windows:
Conclusion
Working with C++ really makes me appreciate high-level languages!
Key Takeaways
📝 C++ requires explicit type declarations for variables.
🔄 Loops and conditional statements control program flow.
🛠️ Functions help modularize our code.
🏗️ Classes and objects enable object-oriented programming.
📚 External libraries, like OpenCV, expand functionality.
Next: Java
Let’s keep going and do a similar lesson with Java. Then, we won’t have to deal with low-level programming for the rest of the course!
Well… maybe just a bit. But we’ll mostly be seeing Python after this point.
Comments (0)
No comments yet.