Jul 13, 2024
hello_world.cpp
) in VS Code.gcc -v
sudo apt-get install build-essential
xcode-select --install
main
function:
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
iostream
for basic input and output operations.std::cout
for output to the console.\n
or std::endl
for new lines.//
/* */
datatype variableName = value;
int x = 5;
int
: integersdouble
: floating-point numberschar
: single charactersbool
: boolean values (true/false)string
: sequences of text (requires #include <string>
)else if
statementsreturnType functionName(parameters) {
// code
}
void printHello() {
std::cout << "Hello!" << std::endl;
}
main
function.&
).const
to make parameters read-only.datatype arrayName[arraySize];
int numbers[5] = {1, 2, 3, 4, 5};
arrayName[index]
starting from 0.int matrix[3][3];
datatype* pointerName;
int x = 5;
int* p = &x; // p points to x
*
.new
to allocate memory in the heap:
int* p = new int;
*p = 10;
delete
:
delete p;
int* arr = new int[10];
delete[] arr;
class ClassName {
public:
datatype attribute;
returnType methodName(parameters);
};
class Dog {
public:
int age;
void bark() {
std::cout << "Woof!" << std::endl;
}
};