C++ Tutorial: OOP in C++
In Object Oriented Programming (OOP), you write programs that manipulate objects.
Object and Class
An object is an "instance" of a class.
A property returns an attribute of an object. Under the hood, a property
is in fact a subroutine that gets or sets a
hidden "field" of the object, but you can work with a property just as with a normal variable. Examples:
int W; W = Panel1->Width; Form1->Caption = 'The width of the panel is ' + IntToStr(W);
A property can return a number or a string, but a property can be an object as well, for example:
A method is a subroutine that acts on an object, such as:
Form1->Show(); Label1->Hide();The creation of an object is called instantiation. Instantiation is also known as "construction".
The VCL components that you set up in the form designer, such as the forms, buttons etc are all "owned" by a global component called "Application". At the start of an application, "Application" automatically creates all of these components, and when it shuts down, it automatically destroys these components.
Object Oriented Programming is based on the following principles:
Hierarchy of classes
TObject is the ultimate ancestor of all classes.
TObject encapsulates fundamental behavior common to all objects,
by introducing methods that create, maintain, and destroy instances of objects, and support message handling.
TComponent is the ancestor of the component classes.
A component is a persistent object that can appear on the IDE palette and that can be manipulated in a Form Designer.
A component can "own" other components. If the form Form1 owns Panel1, then Form1 is responsible for destroying Panel1 when Form1 is destroyed.
TControl is for components that are visible at run time.
A control is a visual component, meaning that you can see it and possibly interact with it at run time. All controls have properties, methods, and events that describe their appearance, such as their position (Left, Top), dimensions (Height, Width), methods to paint or move the control, and events that respond to user actions (such as clicking the mouse or pressing a keyboard key).
TWinControl is the ancestor for controls that are wrappers for MS Windows screen objects ("windows").
A WinControl has a "window handle", such as TEdit, TListBox and TButton. A WinControl can receive user input focus, either by clicking on it, by using the TAB / SHIFT TAB key, or under program control with SetFocus (for example: Button1.Setfocus). Note: button components typically indicate the focus by drawing a rectangle around their caption.
A WinControl can serve as a container ("parent") for other controls, referred to as child controls. Examples of container controls: forms, panels, and toolbars. Note that "parent" does NOT mean the same as "ancestor"!
TScrollingWinControl is for wincontrols that support scrolling.
A scrolling windowed control can display horizontal and vertical scroll bars, and it scrolls a child control into view when the child control receives focus.
TCustomControl is for windowed components that provide their own custom painting.
In contrast, the controls which descend from TWinControl like TEdit and TListbox, already have default drawing capabilities, provided by the encapsulated MS Windows control.
TGraphicControl is for controls which are not windowed controls.
GraphicControls have no window handle. Also, they cannot be parents to other controls.