搜尋此網誌

2025年10月27日星期一

Chapter Quiz

In C++, a member function is simply a function that belongs to a class (or struct). It operates on objects of that class and has access to the class’s data members (including private ones).

The -> is called the arrow operator in C++. It’s used when you have a pointer to an object (or struct) and you want to access one of its members (a variable or a function).

The dot operator (.) in C++ is called the direct member access operator. It’s used to access data members (variables) and member functions (methods) of an object, structure, or union directly.

In C++, an object is an instance of a class.

Think of it this way:

  • A class is like a blueprint (it defines attributes and behaviors).
  • An object is the real thing created from that blueprint.
Using "this" inside constructor:

ComplexNumber(float real, float im){
 this->real_part = real;
 this->im_part = im;
}

An include guard in C++ is a technique used in header files to prevent the same file from being included multiple times during compilation. Without it, if a header is included more than once (directly or indirectly), you’ll get redefinition errors because classes, functions, or variables would be declared multiple times.

#ifndef HEADER_FILE_NAME   // If not defined yet
#define HEADER_FILE_NAME   // Define it now

// Your header file content goes here
class MyClass {
public:
    void hello();
};

#endif // HEADER_FILE_NAME

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass {
public:
    void greet();
};

#endif

#pragma once is a preprocessor directive in C++ that tells the compiler to include a header file only once per compilation, no matter how many times it’s referenced.

It serves the same purpose as traditional include guards (#ifndef … #define … #endif), but it’s shorter and less error‑prone.

parse something: to divide a sentence into parts and describe the grammar of each word or part

Operator overloading in C++ is a way to give special meaning to operators (like +, -, ==, [], etc.) when they are used with user‑defined types (classes or structs). It’s a form of compile‑time polymorphism that makes objects behave more like built‑in types.

When you define a member function outside the class, you use the scope resolution operator to tell the compiler which class the function belongs to.

A global function is defined outside of any class. If there’s a naming conflict (e.g., a local variable or function with the same name), you can use the global scope resolution operator (::) to explicitly call the global version.

Microsoft Copilot

沒有留言:

發佈留言