搜尋此網誌

2025年9月18日星期四

Classes and Objects in C++

In C++, classes and objects are the foundation of object-oriented programming. They help you model real-world entities by bundling data and behavior together.

A class is a blueprint or template for creating objects. It defines data members member functions.

An object is an instance of a class. It has actual values and can perform actions defined by the class.

Operator overloading in C++ lets you redefine how operators behave for user-defined types like classes and structs. It’s a powerful feature that makes your custom types feel more natural to use—like built-in types.

- To make code involving custom types more intuitive.

- To enable operations like +, -, ==, etc., on objects of your class.

Operator overloading is the process of giving special meaning to an existing operator (like +, -, ==, etc.) when used with objects of a class. For example, if you have a Complex class, you can define how + works between two Complex objects.

Inheritance and polymorphism are the keys to writing flexible, reusable, and scalable code.

Inheritance allows a class (called a derived class) to inherit properties and behaviors from another class (called a base class). It promotes code reuse and establishes a hierarchical relationship.

Polymorphism: One Interface, Many Forms

Polymorphism means “many forms.” It allows you to use a single interface to represent different underlying forms (data types or behaviors).

Inheritance gives you structure. Polymorphism gives you flexibility.

Encapsulation is one of the pillars of object-oriented programming in C++, and it’s all about bundling data and the methods that operate on that data into a single unit—usually a class. Think of it like putting valuables in a safe: you control who gets access and how.

Constructors and destructors are the backbone of object lifecycle management in C++.

A constructor is a special member function that’s automatically called when an object is created. Its job? Initialize the object.

A destructor is called automatically when an object goes out of scope or is deleted. It’s used to release resources like memory or file handles.

A memory leak in C++ happens when your program allocates memory on the heap but fails to release it after it's no longer needed. This leftover memory remains occupied, even though your program can't use it anymore—leading to wasted resources and potentially serious performance issues over time.

In C++, a parameter is a variable used in a function definition to accept values passed during a function call. These values are called arguments. Parameters allow functions to operate on different data without rewriting the function itself.

// C++ example

void greet(std::string name) {  // 'name' is a parameter

    std::cout << "Hello, " << name << "!\n";

}

greet("Alice");  // "Alice" is the argument

In C++, stack and heap are two distinct areas of memory used for different purposes, and understanding them is key to writing efficient and safe code

int* ptr = new int(10);  // Allocated on the heap
delete ptr;              // Must be manually freed

Microsoft Copilot

Activities other than computing

adventure: an unusual, exciting or dangerous experience, journey or series of events

cram: to push or force somebody/something into a small space; to move into a small space with the result that it is full

The Volkswagen Beetle is a small family car produced by the German company Volkswagen from 1938 to 2003.

lash: to fasten something tightly to something else with ropes

best: to defeat or be more successful than somebody

scarf down: to eat (something) quickly

giggle: to laugh in a silly way because you are embarrassed or nervous or you think that something is funny

peek: to look at something quickly and secretly because you should not be looking at it

hitch:  to get a free ride in a person’s car; to travel around in this way, by standing at the side of the road and trying to get passing cars to stop

barge: a large boat with a flat bottom, used for carrying goods and people on canals and rivers

agape: if a person’s mouth is agape, it is wide open, especially because they are surprised or shocked

strikingly: in a way that is interesting and unusual enough to attract attention

hospitalize: to send somebody to a hospital for treatment

sanatorium: a place like a hospital where patients who have a long-term illness or who are getting better after an illness are treated

A stock split is when a corporation splits its outstanding stocks to increase the number of shares on the market, which boosts liquidity and decreases its stock price.

liquidity: ​the state of owning things of value that can easily be exchanged for cash

In the financial markets, such as stocks, when prices have generally been increasing or are expected to increase, a bull market exists. Bull markets commonly refer to the stock market but can be applied to anything that is traded, such as bonds, real estate, currencies, and commodities.

commodity: a product or a raw material that can be bought and sold

The P/E ratio (price-to-earnings ratio) is a stock valuation tool that compares a company's stock price to its earnings per share (EPS), indicating the amount investors are willing to pay for each dollar of earnings.

A bond yield is the annual return an investor expects to receive from a bond, expressed as a percentage of its current market price. It is influenced by the bond's price, coupon rate, time to maturity, and broader market conditions like interest rate changes.

streak: a part of a person’s character, especially an unpleasant part

Unitarian: a member of a Christian Church that does not believe in the Trinity and has no formal teachings

minister: a trained religious leader in some Christian churches

lavish on: to give a lot of something, often too much, to somebody/something

cock: to raise a part of your body so that it is pointing upwards or at an angle

A Southern drawl is a feature of a Southern American accent characterized by a slower pace of speech, where pure vowel sounds are lengthened, broken into diphthongs, or prolonged, creating a sliding effect.

diphthong: a combination of two vowel sounds or vowel letters, for example the sounds /aɪ/ in pipe /paɪp/ or the letters ou in doubt

disfigure: to damage the appearance of a person, thing or place

ostracize: to refuse to let somebody be a member of a social group; to refuse to meet or talk to somebody

qualm: a feeling of doubt or worry about whether what you are doing is right

depiction: the act of showing somebody/something in a particular way in words or pictures, especially in a work of art

heel: to lean over to one side

cork: a light, soft material that is the thick bark of a type of Mediterranean oak tree

sloop: a small sailing ship with one mast (= a post to support the sails)

plywood: ​board made by sticking thin layers of wood on top of each other

To "get hooked on" something means to become addicted to a drug or, more commonly, to become very interested, enthusiastic, or obsessed with an activity, hobby, or interest to the point where you can't stop doing it.

relish: to get great pleasure from something; to want very much to do or have something

slack: characterized by slowness, sluggishness, or lack of energy

depth sounder (echo sounder): a device that measures the depth of the sea, or that finds objects underwater, by measuring how quickly sound waves are reflected

dead reckoning: the process of calculating one's position, especially at sea, by estimating the direction and distance travelled rather than by using landmarks or astronomical observations.


Bill Gates "Source Code"

Online Dictionaries Used:

hk.dictionary.search.yahoo.com

www.oxfordlearnersdictionaries.com

www.merriam-webster.com

Some explanations are provided by Google AI Overview.

2025年9月16日星期二

Just Kids?

banishment: the punishment of being sent away from a place, especially from a country

hone: to develop and improve something, especially a skill, over a period of time

coup: a brilliant, sudden, and usually highly successful stroke or act

thaw: to become more friendly and less formal

hiatus: a break in activity when nothing happens

mark up: to mark or correct a text, etc., for example for printing

venture: a business project or activity, especially one that involves taking risks

lay off: to stop employing somebody because there is not enough work for them to do

nail down: to fasten something in place with a nail or nails

repo man: a person whose job is to repossess (= take back) goods from people who still owe money for them and cannot pay

the New Testament: the second part of the Bible, that describes the life and teachings of Jesus Christ

Confirmation: a ceremony at which a person becomes a full member of the Christian Church

aptly: in a way that is suitable or appropriate in the circumstances

decipher: to succeed in finding the meaning of something that is difficult to read or understand


Bill Gates "Source Code"

Online Dictionaries Used:

hk.dictionary.search.yahoo.com

www.oxfordlearnersdictionaries.com

www.merriam-webster.com

攝影常識

合焦︰透過對焦過程,使被攝主體在相機感光元件上形成清晰成像的狀態。

泛焦︰泛焦是一種透過調整光圈與焦點距離,使畫面特定範圍內的景物全部清晰成像的攝影技術。焦距越短,即使光圈較大,亦很容易將泛焦的相片拍攝出來。

曝光補償(Exposure Compensation)︰這個詞指的是在攝影中,當相機的自動測光值無法準確滿足拍攝者意圖時,使用者手動調整影像亮度(增加或降低曝光量)的一種功能。

A classic pointer example in C++

#include <iostream>

using namespace std;


int main() {

    int x = 10;          // a normal integer variable

    int* ptr = &x;       // pointer to int, storing the address of x


    cout << "Value of x: " << x << endl;

    cout << "Address of x: " << &x << endl;

    cout << "Value stored in ptr (address of x): " << ptr << endl;

    cout << "Value pointed to by ptr: " << *ptr << endl; // dereferencing


    // Modify x through the pointer

    *ptr = 20;

    cout << "New value of x after modification via pointer: " << x << endl;


    return 0;

}


int* ptr = &x;ptr stores the address of x.

*ptr → dereferences the pointer, giving access to the value stored at that address.

Changing *ptr actually changes x, since they refer to the same memory location.


Value of x: 10

Address of x: 0x7ffee7b8c9ac

Value stored in ptr (address of x): 0x7ffee7b8c9ac

Value pointed to by ptr: 10

New value of x after modification via pointer: 20


In C++, dereferencing a pointer means accessing the value stored at the memory address the pointer is holding. The operator that does this is the asterisk (*), but only when it’s used outside of a declaration.


Microsoft Copilot

Exercise solution and quiz

std::abs is a standard C++ function used to compute the absolute value of a number. It’s part of the or header, depending on the type of input.

static_cast<int> is a C++ type conversion operator that explicitly converts a value to an int type at compile time.

In C++, a function declaration tells the compiler about a function's name, return type, and parameters—without specifying its actual body. It's like saying, “Hey compiler, this function exists, and here’s what it looks like,” but not yet saying what it does.

In C++, an argument refers to the actual value or variable passed to a function when it is called. It’s what you give to the function so it can do its job.

In C++, a function prototype is a declaration that tells the compiler about a function's name, return type, and parameters—before the function is actually defined. It’s like giving the compiler a heads-up: “This function exists, and here’s how it should be called.”

const my_array& a: It takes a constant reference to a my_array object

Microsoft Copilot

2025年9月15日星期一

科學家的思維模式

    Science grabbed me in part because if fit my need for order and organization and offered the kind of reassuring, satisfying framework I'd already found in math. It also appealed to my hyper-rational sense of the world. At its core, science requires a wildly curious mind that can tame itself with discipline and skepticism. I liked how scientists think, constantly asking themselves, "How do I know?" and "How might I be wrong?"

    科學吸引了我,部分原因是它符合我對秩序與組織的需求,並提供了一種我在數學中早已找到的、令人安心且滿足的框架。它也迎合了我對世界極度理性的看法。從本質上說,科學需要一顆極度好奇的心,同時能以紀律與懷疑精神來約束自己。我喜歡科學家的思維方式——他們不斷問自己:「我怎麼知道的?」以及「我可能錯在哪裡?」

nightwatchman: a man whose job is to guard a building such as a factory at night

German shepherd: a large dog, often trained to help the police, to guard buildings or (especially in the US) to help blind people find their way

smart-aleck: a person who thinks they are very clever and likes to show people this in an annoying way

devil-may-care: cheerful and not worried about the future

facade: the way that somebody/something appears to be, which is different from the way somebody/something really is

groan: DJ[grəun] to make a long deep sound because you are annoyed, upset or in pain, or with pleasure

conspicuously: ​in a way that is easy to see or notice, or that is likely to attract attention

declension: a set of nouns, adjectives or pronouns that change in the same way to show case, number and gender

Latin declension is the set of patterns according to which Latin words are declined—that is, have their endings altered to show grammatical case, number and gender.

lingering: slow to end or disappear

harness: to control and use the force or strength of something to produce power or to achieve something

theorem: DJ[ˋθiərəm] a rule or principle, especially in mathematics, that can be proved to be true

if something complicated or difficult to understand falls or slots into place, it becomes organized or clear in your mind

scaffolding: a system or framework of support provided by an instructor to help a student reach the next level of learning

goofy: silly; stupid

revelation: a fact that people are made aware of, especially one that has been secret and is surprising

"To put off reading" can mean delaying reading for a later time,

planarian: any of various dark-colored freshwater triclad flatworms (family Planariidae) with two eyespots and a triangular head

notion: an idea, a belief or an understanding of something

head first: ​without thinking carefully about something before acting

engaging: interesting or pleasant in a way that attracts your attention

blot out: to deliberately try to forget an unpleasant memory or thought

payoff: an advantage or a reward from something you have done

blossom: DJ[ˋblɔsəm]

intuit: to know that something is true based on your feelings rather than on facts, what somebody tells you, etc.

pounce on: to quickly notice something that somebody has said or done, especially in order to criticize it

frantic: done quickly and with a lot of activity, but in a way that is not very well organized

notion: ​an idea, a belief or an understanding of something

rote: the process of learning something by repeating it until you remember it rather than by understanding the meaning of it

decent: of a good enough standard or quality

upend: overcome

sip: ​to drink something, taking a very small amount each time

devise: to invent something new or a new way of doing something

fizzy: ​(of a drink) having bubbles of gas in it

slog: a period of hard work or effort

plodding: working or doing something slowly and steadily, especially in a way that other people think is boring

strip away: to remove (something that covers a surface) : to pull pieces of a covering away from a surface

monomania: too much interest in or enthusiasm for just one thing so that it is not healthy

esoteric: ​likely to be understood or enjoyed by only a few people with a special knowledge or interest

clarinet: a musical instrument of the woodwind group. It is like a pipe in shape and has a reed and a mouthpiece at the top that you blow into.

delight: a feeling of great pleasure

pyrotechnician: an expert in working with fireworks

concoct: to prepare by combining raw materials

tame: to make an emotion, an organization, a situation, etc., less powerful or easier to control

skepticism: an attitude of doubting that claims or statements are true or that something will happen

impressionable: (of a person, especially a young one) easily influenced or affected by somebody/something


Bill Gates "Source Code"

Online Dictionaries Used:

hk.dictionary.search.yahoo.com

www.oxfordlearnersdictionaries.com

www.merriam-webster.com

Some explanations are provided by Google AI Overview.

Translated by Microsoft Copilot