Introduction to C++, arrays, pointers, dynamic memory allocation
Before the topic
At the top of the file, we usually includes package <iostream>, which is related to input and output, contains frequent-used function cin and cout.
#include <iostream> //cin and cout
using namespace std;
Definition of Variables:
There are four ways to define a variable with value:
int main(){
int i ;
int k = 0;
int j{ 5 }; // the newest feature
int w(10);
char c1{'e'};
// primitive data type: int, char, float, double, long, etc.
}
Define the Data Type by User
In many times, we need to define our own data type for use. Then, we usually define a new class beyond the main() function as our new datatype.
// for user-defined class
class ThreeD {
public:
int ht;
int wid;
int dep;
ThreeD(){}
//constructor
ThreeD(int i, int j, int k) {ht = i; wid = j; dep = k;}
};
int main(){
ThreeD t1(3,4,5);
cout << t1.dep << endl;
cin >> t1.ht >> t1.wid >> t1.dep;
cout << t1.ht << " " << t1.wid << " " << t1.dep << endl;
}
Pointer
Pointer is a special character of C++.
int main(){
// pointer
// p1 is a pointer to int object.
// it can also be: int* p1, int * p1.
int *p1;
// p1 has address of j as value
p1 = &j
// * is a FUNCTION which returns the value
// stored in the address carried by p1
cout << *p1 << endl;
// j is now 100
*p1 = 100
}
Array Structure
int A[5] = {1,2,3,4,5};
//Array name without subscript is the address pf the
// first element of array
// i.e., A is &A[0]
cout << *A << endl;
// print A[2]
cout << *(A+2) << endl;
// no error, but it return the undesired result.
// beacuse it takes the address, it will read the whatever on this address
cout << A[7] << endl;
// the same as A[2]
cout << 2[A] << endl;
int * B = &w;
B[2] = 5;
Dynamic Memory Allocation
// new is a FUNCTION taht will ask system to allocate a memory
// space for an object, and return the address of the memory space.
int *p2 = new int(7);
// Remove the object pointed by p2
// Note here, it's remove not delete. It means we can still use p2
// pointer but the object had been removed
delete p2;
// remove the array
int *p3 = new int[5];
delete[] p3;
2D array
// 2D array
int A1[3][4] = { {11,12,13,14}, {15,16,17,18}, {19,20,21,22} };
// Belows are all the same thing using different expression.
cout << A[1][2] << endl;
cout << *(*(A+1) + 2) << endl;
cout << *(A[1] + 2) << endl;
cout << (*(A+1))[2] << endl;
cout << *(&A[0][0] + 4 * 1 + 2) << endl;
Linked List
class node {
public:
int value;
node* next;
node(int i){
value = i;
// nullptr: system keyword, means its value is not defined.
next = nullptr;
}
};
int main(){
node * head = nullptr;
for(int i = 0; i < 5;; i++){
// create a new node
node* p3 = new node(i+1);
// (*p3).next = head;
p3->next = head;
head = p3;
}
// print: 5 4 3 2 1
node* p4 = head;
while(p4){
cout << p4->value << " ";
p4 = p4->next;
}
cout << endl;
}
Complete Codes:
/*
Performance
Low level structure: only c/c++ allowed to access low level structure.
*/
#include <iostream> //cin and cout
using namespace std;
// for user-defined class
class ThreeD {
public:
int ht;
int wid;
int dep;
ThreeD(){}
//constructor
ThreeD(int i, int j, int k) {ht = i; wid = j; dep = k;}
};
class node {
public:
int value;
node* next;
node(int i){
value = i;
// nullptr: system keyword, means its value is not defined.
next = nullptr;
}
};
int main(){
// Part1: Data Type
int i ;
int k = 0;
int j{ 5 }; // the newest feature
int w(10);
char c1{'e'};
// primitive data type: int, char, float, double, long, etc.
ThreeD t1(3,4,5);
cout << t1.dep << endl;
cin >> t1.ht >> t1.wid >> t1.dep;
cout << t1.ht << " " << t1.wid << " " << t1.dep << endl;
// pointer
int *p1; // p1 is a pointer to int object.
// it can also be: int* p1, int * p1.
p1 = &j // p1 has address of j as value
// * is a FUNCTION which returns the value
// stored in the address carried by p1
cout << *p1 << endl;
// j is now 100
*p1 = 100
//array structure
int A[5] = {1,2,3,4,5};
//Array name without subscript is the address pf the
// first element of array
// i.e., A is &A[0]
cout << *A << endl;
// print A[2]
cout << *(A+2) << endl;
// no error, but it return the undesired result.
// beacuse it takes the address, it will read the whatever on this address
cout << A[7] << endl;
// the same as A[2]
cout << 2[A] << endl;
int * B = &w;
B[2] = 5;
// Part2: dynamic memeory allocation
// new is a FUNCTION taht will ask system to allocate a memory
// space for an object, and return the address of the memory space.
int *p2 = new int(7);
// Remove the object pointed by p2
// Note here, it's remove not delete. It means we can still use p2
// pointer but the object had been removed
delete p2;
// remove the array
int *p3 = new int[5];
delete[] p3;
// 2D array
int A1[3][4] = { {11,12,13,14}, {15,16,17,18}, {19,20,21,22} };
// Belows are all the same things using different expression.
cout << A[1][2] << endl;
cout << *(*(A+1) + 2) << endl;
cout << *(A[1] + 2) << endl;
cout << (*(A+1))[2] << endl;
cout << *(&A[0][0] + 4 * 1 + 2) << endl;
// linked list
node * head = nullptr;
for(int i = 0; i < 5;; i++){
// create a new node
node* p3 = new node(i+1);
// (*p3).next = head;
p3->next = head;
head = p3;
}
// print: 5 4 3 2 1
node* p4 = head;
while(p4){
cout << p4->value << " ";
p4 = p4->next;
}
cout << endl;
return 0;
}
Zahb44856
发布了10 篇原创文章 · 获赞 0 · 访问量 93
私信
关注