C++ course / PDF

C++ course / PDF


C++ course / PDF








Part 1: Mechanics
Part 2: Basics
Part 3: References
Part 4: Const
Part 5: Inheritance
Part 6: Libraries
Part 7: Conclusion



*********




Part 1: Mechanics

C++ is a superset of C

•New Features include
–Classes (Object Oriented)
–Templates (Standard Template Library)
–Operator Overloading

–Slightly cleaner memory operations

Header Guards
#ifndef __SEGMENT_HEADER__
#define __SEGMENT_HEADER__
// contents of Segment.h
//...
#endif

•To ensure it is safe to include a file more than once.

Part 2: Basics

Allocating memory using new
Point *p = new Point(5, 5);
•new can be thought of a function with slightly strange syntax
•new allocates space to hold the object.
•new calls the object’s constructor.

•new returns a pointer to that object

Deallocating memory using delete

// allocate memory
Point *p = new Point(5, 5);
...
// free the memory
delete p;
For every call to new, there must be
exactly one call to delete.

Using new with arrays
int x = 10;
int* nums1 = new int[10]; // ok
int* nums2 = new int[x]; // ok
•Initializes an array of 10 integers on the heap.
•C++ equivalent of the following C code
int* nums = (int*)malloc(x * sizeof(int));

Using new with multidimensional arrays
int x = 3, y = 4;
int** nums3 = new int[x][4];// ok
int** nums4 = new int[x][y];// BAD!
•Initializes a multidimensional array
•Only the first dimension can be a variable. The rest must be constants.
•Use single dimension arrays to fake multidimensional ones

Using delete on arrays
// allocate memory
int* nums1 = new int[10];
int* nums3 = new int[x][4][5];
...
// free the memory
delete[] nums1;
delete[] nums3;
•Have to use delete[].

Destructors
•delete calls the object’s destructor.
•delete frees space occupied by the object.
•A destructor cleans up after the object.
•Releases resources such as memory.
















C++ course / PDF

2 commentaires:

  1. It is amazing to see how IT grows these days. Companies (like this one: https://pro4people.com/health-software/pre-development-initiating-software-as-a-medical-device-samd-project/) create more usfeul and amazing stuff. Keep doing the awesome job!

    RépondreSupprimer