Essential C++ # 01. Basic C++ Programming
Chapter 1. Basic C++ Programming
📌Composition of a C++ Class
- A header file that provides a declaration of the operation supported by the class
- A program text file that contains the implementation of those operations
📌Common Initialization Syntax VS Constructor Syntax
- common:
xxxxxxxxxx
int number = 0;
- constructor syntax:
xxxxxxxxxx
int number(10);
📌Constructor Syntax
The reason behind this syntax refers to multiple parameter initialization.(very common in Class)
But the primitive type can also be initialized like so.
📌Initialize an array
1️⃣:
xxxxxxxxxx
int seq_size = 6;
int elem_seq[seq_size] = {
1, 2, 3,
4, 5, 6
};
2️⃣:
xxxxxxxxxx
int elem_seq[] = {
1, 2, 3,
4, 5, 6
};
📌Initialize a vector
1️⃣:
xxxxxxxxxx
int seq_size = 3;
vector<int> elem_seq(seq_size);
elem_seq[0] = 1;
elem_seq[1] = 2;
elem_seq[2] = 3;
2️⃣:
xxxxxxxxxx
int seq_size = 3;
int elem_val[seq_size] = {1, 2, 3};
vector<int> elem_seq(elem_val, elem_val + seq_size);
elem_val
is actually the address passed in the vector. The + seq_size
indicates how long the array should be.
📌Why pointer?
A pointer introduces a level of indirection to a program. Rather than manipulate an object directly😑, we manipulate a pointer that holds the address of an object😍.
📌How to use a pointer?
xxxxxxxxxx
int ival = 1024;
int* pi; // pi is a pointer to an object of type int
&ival; // evaluates to the address of ival by using `&`
pi = &ival; // pass the address of ival to pi
📌Different Style of Pointer
xxxxxxxxxx
int* pi = &ival;
int *pi = &ival;
The preceding are both correct. If you prefer the first one, please take into account the following:
xxxxxxxxxx
int* pi, pi2;
pi
is a pointer, pi2
is just an integer. If you want both of them are pointer, you should:
xxxxxxxxxx
int* pi, * pi2;
📌Dereference a pointer
You can take the value of the pointer points to by using *
.
xxxxxxxxxx
if (*pi != 1024)
{
*pi = 1024;
}
📌Be patient when you are learning pointer
The initial complexity of using a pointer comes from its confusing syntax.🤣
📌Caution using pointer!⚠
A pointer can possibly points to no object!
xxxxxxxxxx
// initialize a pointer to address no object
int *pi = 0;
Therefore, to guard against dereferencing a null pointer, we must assure if its address value is zero before using...
xxxxxxxxxx
// safe check
if (pi && *pi!=1024)
{
*pi = 1024;
}
The if (pi && ...)
assure the pointer pi
is not empty.
📌Complex Example of Pointers
We have 6 vector sequence objects:
xxxxxxxxxx
vector<int> fibonacci, lucas, pell, triangular, square, pentagonal;
Suppose we have a pointer points to fibonacci
:
xxxxxxxxxx
vector<int> *fib_ptr = &fibonacci;
Or more than that, an array of pointers whose type is vector<int>*
...
vector<int>* ptrs[6] = {
&fibonacci, &lucas, &pell, &triangular, &square, &pentagonal
};
📌Function using ->()
and .()
In C#, a function can be called like this:
xxxxxxxxxx
number.ToString();
But in C++, you may see 2 ways of invoking a function:
xxxxxxxxxx
xxx.DoSomething();
xxx->DoSomething();
The difference is that:
When xxx
is an object of a class, it uses xxx.Function();
When xxx
is a pointer to an object, it uses xxx->Function();