Essential C++ # 01. Basic C++ Programming

C++

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:
  • constructor syntax:

 

📌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️⃣:

2️⃣:

 

📌Initialize a vector

1️⃣:

2️⃣:

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?

 

📌Different Style of Pointer

The preceding are both correct. If you prefer the first one, please take into account the following:

pi is a pointer, pi2 is just an integer. If you want both of them are pointer, you should:

 

📌Dereference a pointer

You can take the value of the pointer points to by using *.

 

📌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!

Therefore, to guard against dereferencing a null pointer, we must assure if its address value is zero before using...

The if (pi && ...) assure the pointer pi is not empty.

 

📌Complex Example of Pointers

We have 6 vector sequence objects:

Suppose we have a pointer points to fibonacci:

Or more than that, an array of pointers whose type is vector<int>*...

 

 

📌Function using ->() and .()

In C#, a function can be called like this:

But in C++, you may see 2 ways of invoking a function:

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();

 

Previous
Previous

Essential C++ # 02. Procedural Programming

Next
Next

Agile Software Development# 10.LSP: The Liskov Substitution Principle