Learning C++ Sources
I will follow the Udemy Course “The C++20 Masterclass From Fundamentals to Advanced” which is around 112 hours.
Learn by Numerical calculation:
Learn by Making Game
- Sử dụng Library SFML: First lesson SFML with Youtube Pong-SFML-Game Game CutTree
- Sử dụng Library Raylib: Learn C++ with Raylib
- Sử dụng SDL: Learn SDL 2
- Sử dụng Game Engine Book-Esenthel-2D-Basics Esenthel-First-Try Esenthel-Book-Tetris Book-Esenthel-Basics
1 Learn from Udemy course¶
Backlink | Photo by Lewis Keegan on Unsplash
I would like to learn this course in 20 days, I need to watch around 120h/20 days= 6h/day. I maybe too much, but I’m sure I will skip some videos, or skip some easy parts. There are 49 sections in total. I will try learning 3 sections per day (around 30-50 videos)
2 Print-out¶
#include <iostream> // libary to print
int addNumber (int first_parameter, int second_parameter){
int result = first_parameter + second_parameter;
return result;
}
// Tạo 1 function để sum hai giá trị, sử dụng int để khai báo dạng dữ liệu, trong dấu {} là các bước thực hiện
int main(){
std::cout << "Hello World"; // Dùng << để print out
std::cout << "My Name is An" << std::endl; // Dùng thêm << để tiếp tục print out, endl sẽ xuống hàng
std::cout << "This is my first C++ print" << std::endl;
int value = 7; // gán biến , tất cả các biến trong C++ đều phải được khai báo dạng số thực, nguyên hay là string trước khi sử dụng
std::cout << "Value : " << value << std::endl;
int first_number {3}; // Khai báo dữ liệu có thể dùng {value}
int second_number {10};
int sum = first_number + second_number; // Đây là 1 function nhỏ có trong main function, tuy nhiên function này nên được nằm tách biệt, check line 3-5
// Có thể sử dụng addNumber function tại Line 3-5
int sum2 = addNumber(first_number, second_number);
std::cout << "Sum:" << sum << std::endl;
std::cout << "Sum from function " << sum2 <<std::endl;
return 0;
}
3 Variables¶
3.1 Store values in C++¶
There are several ways to store the data in C++.
- Decimal: 15
- Octal: 017
- Hexadecimal: 0x0f
- Binary: 0b00001111
#include <iostream>
int main(){
int number1=15;
int number2=017; //using the Octal
std::cout << "Number1: " << number1 << std::endl;
std::cout << "Number2: " << number2 << std::endl;
// Kết quả cả hai dữ liệu đều xuất 15
}
3.2 Variables initialization¶
Khi khai báo dữ liệu, có thể sử dụng {} để khai báo value của dữ liệu đó
int là dạng dữ liệu số nguyên, vì vậy không thể gán dữ liệu là 15.1 được
#include <iostream>
int main(){
int number1=15;
int number2=017; //using the Octal
std::cout << "Number1: " << number1 << std::endl;
std::cout << "Number2: " << number2 << std::endl;
// Kết quả cả hai dữ liệu đều xuất 15
// Bây giờ sẽ sử dụng initialization khi khai báo dữ liệu
int elephant_count;
int lion_count {}; // nếu ko khai báo thì mặc định là 0
int dog_count {10};
int sum_count {dog_count + lion_count};
std::cout << "Sum of Dog and lion: " << sum_count;
return 0;
}
3.3 Signed and unsigned¶
3.4 Fractional number¶
Để sử dụng dạng thập phân thì có ba dạng
- float: 7 số
- double: 15 số sau dấy phẩy
- long double
3.39e-11
tức là 3.39 nhân 10 mũ 11
#include <iostream>
int main()
{
int number1 = 15;
int number2 = 017; //using the Octal
std::cout << "Number1: " << number1 << std::endl;
std::cout << "Number2: " << number2 << std::endl;
// Kết quả cả hai dữ liệu đều xuất 15
// Bây giờ sẽ sử dụng initialization khi khai báo dữ liệu
int elephant_count;
int lion_count{10}; // nếu ko khai báo thì mặc định là 0
int dog_count{};
std::cout << "Write number of dog: ";
std::cin >> dog_count;
int sum_count{dog_count + lion_count};
std::cout << "Sum of Dog and lion: " << sum_count;
float number3{1.1231232134125341};
double number4{1.1231232134125341};
return 0;
}
4 Postfix¶
Sử dụng prefix để tăng hoặc giảm 1 giá trị
value = value + 1
tương đương với value++
value = value - 1
tương đương với value--
#include <iostream>
#include <cmath>
using std::cout; //cho phép viết tắt cout thay vì viết std::cout
using std::cin;
using std::endl;
int main(){
int value {5};
// Postfix
cout << "Using postfix for value of 5 : " << value++ << endl; // at this step, the value is still 5 because it print 5 first then value + 1
cout << value << endl;; // now values = 6
// Prefix
value = 5 ;// reset it
--value;
cout << "Using postfix for value of 5 : " << value++ << endl; // at this step,
// Postfix with specific value
value = 5;
value += 5;
cout << value <<endl; // giá trị bây giờ là 5 +5
value -= 5; // value = value - 5
cout << value <<endl; // giá trị bây giờ là 10 -5
// Sử dụng boolean, true or false
std:cout << std::boolalpha ; // xuất text là true or false thay vì 1 hoặc 0
int value2 {1};
int value3 {10};
cout << (value2 < value3) <<endl;
// and operator
return 0;
}
5 Math functions¶
Sử dụng library cmath
, check cppreference.com for more details
std::floor
std::ceil
std::exp(a)
tức là \(e^a\)
std::pow(x,a)
tức là \(x^a\)
std::log(a)
tức là \(ln(a)\)
std::log10(a)
tức là \(log10(a)\)
6 Control-Flow¶
7 if statement¶
#include <iostream>
/*
Bây giờ sẽ tạo một program điều khiển giao thông
*/
int main(){
std::cout << "Please see the stoplight \nRed then stop, Green then Go now \nBut if the police ask stop, you have to stop!\n";
// Khai báo các biến, sử dụng boolean
bool red {false};
bool green {true};
bool yellow {true};
bool police_stop {true};
if(green){
if(police_stop){
std::cout << "Stop";
}
else{
std::cout << "Go now";
}
}else if(yellow){
std::cout << "Please slow down";
}else{
std::cout << "Stop";}
}
8 Ternary Expression¶
result = (condition) ? open1:open2
9 Loops¶
9.1 For¶
For function cần khai báo như sau for(biến chạy; điều kiện;bước nhảy)
#include <iostream>
int main(){
for(int i{}; i < 10; ++i){ // Sử dụng initializer cho i{}, mặc định là 0
std::cout << "I learn C++ " <<i <<std::endl;
}
// Please note that i{} cannot use outside the for function{}
// Nếu muốn sử dụng i phải khai báo phía trên của for loop
// For function cần khai báo như sau for(biến chạy; điều kiện;bước nhảy)
}
9.2 While loop¶
// While loop
const int count{10};
unsigned int i{};
while (i< count){
std::cout << "I learn more C++ " <<i<<std::endl;
++i; // i need to increase, otherwise it runs forever
}
10 Arrays¶
#include <iostream>
int main(){
int i{1};
std::cout << " We will the array in this tutorial\n" <<++i <<std::endl;
// Tạo array 10x10
int scores[5];// index starts from 0, ends at 9, nhưng chưa có giá trị
double salaries[5]{12.123,4.21,100.124,123.25,456.12};
int my_class[]{1,5,123,3456,1}; // sử dụng [] thì k cần khai báo số lượng element
//Có 2 cách sử dụng For để chạy hết tất cả element trong arrays
//Sử dụng biến chạy i và prefix ++i, dựa theo index của arrays
//Hoặc sử dụng auto value : arrays, tức là dò từng element
// Sử dụng index
for (size_t i = 0; i < 5; ++i)
{
std::cout <<"Values of scores [" << i << "]: "<<scores[i] << std::endl;
// Sẽ không có giá trị
// Bây giờ sẽ gán giá trị
scores[i] = i*2; //lưu ý scores được khai báo là int
std::cout <<"Values of salaries [" << i << "]: "<<salaries[i] << std::endl;
// Sẽ xuất giá trị của salaries theo thứ tự
}
//Cách 2, sử dụng giá trị của từng element
for (auto value : my_class) // sử dụng định dạng auto cho value
{
std::cout<< value << std::endl;
}
// Size of array
std::cout <<"Size of my_class array using sizeof: " <<sizeof(my_class) <<std::endl; //print ra 20, đây là size theo dung lượng (nặng hay nhẹ) chứ không phải số lượng element
// Cần phải chia cho dung lượng của 1 element để ra được số lượng
std::cout <<"Size of my_class array using sizeof/sizeof[0]: " <<sizeof(my_class)/sizeof(my_class[0]) <<std::endl;
std::cout <<"Size of my_class array: " <<std::size(my_class) <<std::endl;// only from c++17
//Multi Dimensional Arrays
//Tạo mảng từ 2D thì sử dụng [][], nếu 3D thì [][][]
//Khi gán biến sử dụng {i1,i2,i3}
int twoDimension [4][3] {//3 tức là có 3 cột, vì vậy phải khai báo {i1,i2,i3}
// còn để trống [] thì ko xác định được số dòng, phù hợp nếu chúng ta chưa rõ
{1,2,3},
{23,12,34}, // chú ý, giữa các dòng phải cách nhau dấu phẩy ,
{123,125,123},
{1156,115,772}
};
std::cout <<"Cannot print the array like Python " <<twoDimension<<std::endl;
// Xuât tất cả giá trị array
for (size_t i=0; i< std::size(twoDimension);++i){
for (size_t j = 0; j < std::size(twoDimension[i]); ++j)
{
std::cout << " Item [" <<i<<"]["<<j<<"] "<<twoDimension[i][j]<<std::endl;
}
}
for (size_t i=0; i< std::size(twoDimension);++i){
for (size_t j = 0; j < std::size(twoDimension[i]); ++j)
{
std::cout << twoDimension[i][j] << " ";
} ;
std::cout<<std::endl;
}
}
11 Pointers¶
Sử dụng dấu *
để khai báo pointers
int * number p{};
Pointers của int
và các loại khác đều có same size vì chúng có thể chuyển hoá lẫn nhau
&
là adress của operator
Tóm lại pointer sẽ cho adress
Tại sao phải dùng pointer
Sử dụng pointer trong array sẽ tiết kiệm thời gian và dung lượng thay vì phải khai báo biến. Chỉ cần adress của values đó là được
Đặc biệt trong trường hợp sử dụng các biến trung gian cho matrix. Sử dụng pointer sẽ tiết kiệm được rất nhiều dung lượng và thời gian, vì nó chỉ cần adress của matrix, chứ không cần phải lưu dữ liệu
#include <iostream>
// How to use pointer
// Pointer tức là adress của từng variable
int main(){
int* p_int{}; // sử dụng * để khai báo đây là pointer, có để * trước p_int, hoặc sau int
int data_test{20};
p_int = &data_test; // sử dụng & để lấy adress của data_test trong memory
// Điều này tương tự khi khai báo dữ liệu int*
std::cout<<"Here is the adress in memory of data_test " << p_int << std::endl;
std::cout<<"Here is the value of data_test " << *p_int << std::endl;
// Nếu sử dụng dấu *trước p_int sẽ chuyển tỪ Adress sang dữ liệu
// It called dereferencing a pointer
// Chú ý, pointer không sử dụng được cho text, vì chỉ lấy adress của ký tự đầu tiên
// Để sử dụng pointer cho text thì phải khai báo dạng array
// VD char* message[] {"Hello guys!"}
const char* message2[]{"Hello guys!"}; // Chú ý phải dùng const
std::cout << "Using array text with pointer: " << message2 << std::endl;
std::cout << "Using array text with pointer: " << *message2 << std::endl;
// Bây giờ sẽ tạo một máy nói tự động
// Tạo list các câu nói, sử dụng pointer dạng char array để chứa các sentences
const char* predictions[]{
"Good morning sir!",
"I hope you have a good day",
"I am working on the C++ project",
"Do you like making video games?",
"There are many things to learn!!!",
"Time management is really important",
"Try to focus on one thing then keep doing",
"Remember, family is always the most important"
};
bool end_speaker{false}; // Sử dụng boolean, xác định kết thúc trò chuyện
const int max_length{30};
char name[max_length]{}; // Khai báo biến name dài 15 ký tự
std::cout <<"What's your name : ";
std::cin.getline(name,max_length); // Sử dụng getline để lấy một sentence, nếu không chỉ lấy được một giá trị hay chỉ 1 char
// Tạo một loop, đưa các câu hỏi cho đến khi người dùng muốn end
while (!end_speaker)
{
std::cout<<"Hello Mr. " <<name <<std::endl;
size_t rand_num = ((std::rand()%std::size(predictions)));
std::cout <<"Sentence "<<rand_num<< " : "<<predictions[rand_num] <<std::endl;
std::cout << "Do you want to continue? (Y | N)? ";
char go_on{};
std::cin >> go_on;
end_speaker = ((go_on=='Y') || (go_on=='y')) ? false: true; // Chú ý sử dụng dấu == để so sánh, không sử dụng dấu =, vì đây là gán variable
if (end_speaker) {
std::cout<<"Thanks for your answer, have a good day!";
}else{
continue;
}
}
return 0;
}
12 References¶
Reference tương tự như pointer.
Reference sử dụng &
13 Functions¶
Trong file code.cpp
có thể tạo ra những function (a,b) để sử dụng nhiều lần
Các functions được tóm tắt lại trong các header.h
để khai báo những functions và để làm documentation
header
là tổng hợp của các functions sẽ sử dụng trong main code.cpp
- Chú ý: Các variables được khai báo trong function {} sẽ không thể đọc được trong các functions khác
**
là pointer to pointer, sử dụng trong mảng
14 Related¶
Backlinks¶
No other pages link to this page.
Created : Jan 19, 2022
Recent Posts
- 2024-11-02: 🔐BUỔI 10 - Phân tích thị trường
- 2024-11-02: 🔐BUỔI 11 - Phân tích thị trường
- 2024-11-02: 🔐BUỔI 12 - Phân tích sóng tăng
- 2024-11-02: 🔐BUỔI 13 - Phân tích hỏi đáp
- 2024-11-02: 🔐BUỔI 14 - Yếu tố kiểm soát
- 2024-11-02: 🔐BUỔI 15 - Hỏi đáp
- 2024-11-01: 🔐BUỔI 6 - Ôn lại và bổ sung
- 2024-11-01: 🔐BUỔI 7 - Chiến thuật Trend
- 2024-11-01: 🔐BUỔI 8 - Công thức điểm vào lệnh
- 2024-11-01: 🔐K2023 - BUỔI 9 - Quy trình vào lệnh