On Wed, 2024-12-04 at 13:14 +0100, Rosario19 wrote:
On Mon, 02 Dec 2024 01:17:49 +0800, wij wrote:
There are several understandings:
https://www.quora.com/As-an-experienced-OOP-programmer-in-your-opinion-whats-the-biggest-problem-of-OOP-object-oriented-programming
...
OO can have many meaning. I took OO to mean object, the basic entity of the
programming model and the operation of the object. The concept, as
foundmental,
has to be solid, practical and easily usable. Otherwise, more codes and
efforts
will be needed latter to fix it, making the original goal ,practically, a lie.
IOW, (nearly) a flawless model is all the basics. ...
https://sourceforge.net/projects/cscall/files/MisFiles/ClassGuidelines.txt/download
From my view, programming language has to provide a model, so that programmers
know what they are dealing with, to solve the problem (recent C++ seems
solving
just syntax problems).
In (my) OOP, 'portability' (or reusable) is first achieved by making the probramming object compatible (form platform to platfrom or from time to time
in the same platform, but libwy only considers linux, but the idea should be
generally applicable), i.e. like pod types, structures, union may not be
portable.
Programming object or 'concept' are 'better' represented/wrapped by class
(keyword)
All should be simple, I don't know how to make the idea of 'object' more
simpler.
See the guidelines.
oop
what is oo programming?
1 definition
object=memory layout and all the label to access to it, even that
layout apply to the pc memory in the machine
oop= programming way use objects and functions and operations on
objects with this definition even C would be oop, int and all operation with
that would be one object
change definition
2 definition
a object is a memory layout that have a routine for inizialize that
memory (constructor) and one routine for end the object (distructor)
and the programmer can define all the functions and operations to it
and othe objects or types in language
oop is the programming of objects in definition 2
That (the beginning of OOP) is RAII. But, not C++ programs are automatically RAII. You still need to make it RAII.
[Snippet] Object life cycle view
+----------------------------------------------------------+
| Creating objects to program: |
| Sequence of acquisition incident: Object life cycle view |
+----------------------------------------------------------+
1. Name binding
(there might be anonymous object)
2. Size binding
Information to allocate memory
3. Address binding
Object in this stage is called in random state.
4. Semantics binding
Object is in responsible state of the occupied space and contents.
(external resource may be associated in the semantics)
5. Reversal is the object destruct process
Note: Class of objects that can be randomly overwritten is referred to as
discardable in this library. Often, such object has no user defined
destructor.
-----
More to continue:
----- Result of the life cycle view is the basic rule of class members
Constructor(..):
Constructor forms are in basic the Cartesian product view of problem domain
in tupple. IOW, the class (concept) is defined by the arguments of ctor.
Constructor brings an object in random state to responsible state.
Destructor:
The destructed object is no longer considered existing in the C++ language
(ref. [12.4.14]). Since destructing an object the second time results to
undefined behavior, destructor must make sure destructing it once is enough.
In another word, destructor must succeed to the language. Object destruction
actually magnifies the effect of common ignorance of the implicitly created
rollback function (or a design).... The program execution from return/throw/
exit.. to its caller(e.g. main, init..) shall succeed.
Note: C++ language had enforced the semantics that 'destructor must succeed'.
Const member functions:
A set of const function members, often referred to as property or attribute
members, defines whether or not two objects function 'identically'. In
general, a minimum set of such members must exist. These members are often
in complexity O(1), and provide the basic proofs that the class is properly
designed.
Reset(..) members:
Reset member brings the object from responsible state back to random state
and then does whatever the argument corresponding constructor does.
Therefore, If a reset member exists then the argument(s) corresponding
constructor also exists.
Construct/destruct/reset are implemented as composite operations of object
initialization process of the life cycle view (and the symmetrical reverse).
Implementation can optimize the theoretical sequence.
Since this library adopts an implicit rule that object has to be in a 'valid'
state, the reset() postcondition is thus required to be default. Default
state is among the easiest check points to ensure object constructed, in
whatever valid state, can always be successfully destructed. Note that reset
members normally exist but not necessary. For instance, if the class
contains const or reference data members, then the reset would be difficult
to implement.
Move Constructor:
The motivation of devising is from the need to avoid costly and unnecessary
copy operations found in classes that contain allocated resources and the
objects movement in a dynamic array. It has been practiced that such
motivation basically just need a *move constructor*. This library uses
"enum ByMove_t { ByMove }" as the signature denoting a move constructor (for
the moment), which is defined to move the source referenced object to 'this'
pointed address (source object is treated non-existent). The reason is that
such an operation is conceptually elementary, thus, it can enable the
definition of a no-throw swap function template (note: implementation needs
a buffer type, e.g. type_store<T>, but this is a different issue).
Many other libraries do not contain the move constructor. Bit-wise copy
(memcpy) may mostly do the job, e.g. QString of Qt library, so far. Just
make sure that destructor of the moved object won't be called.
Note: C++11 introduced rv-reference and defined another name 'std::move',
a constructor taking a rv-reference argument is therefore called a move
constructor. This library decided to continue the development using this now
standard-confusing move constructor. Simply because ByMove is relatively
more elementary. Many reasons can eventually be translated to being light
weight, which in a way means it can transform easier. Again, This library's
guide "No room should be left for lower-level implement". Another reason is
the ratio of gain vs. cost does not appear good for the author to use it,
hopefully this library can survive.
----
Programs written in C++ does not automatically make it qualify as OOP.
OOP is still a developing idea/theory, we need to make it more clear.
-------- Class member rules
Accessible members give the object semantics. But this guideline can only
address the general cases.
The general rule: Semantics of class members should be consistently about
one thing suggested by the ctor(s) and verifiable except not possible.
Let T denote a given class, the following member names and associated
functionalities are defined.
T() Default constructor. The object thus construted is referred
to as in default state and so the default object.
Postcondition of throw: object does not exist.
Note: If object can mean 'no real contents', the default object is
better thus designed to guarantee safe destruction.
T(const T&) Construct *this to the state as the source object.
Note that copy constructed object should be 'identical' to the source
object, this is implied by the language.
T(T&, ByMove_t) Move the source object to 'this' pointed address.
This member is not really a constructor since nothing is created.
In practice, move ctor only needed in malloc'd array and for
implementing swap.
T(const &TT) Conversion ctor converts concept implicitly. If sizeof(T)!=
sizeof(TT) or the sentence "T is TT and TT is T" does not read right,
declare 'explicit'.
Errno reset(..)
Reconstruct the object to the state as constructed by the arguments
corresponding constructor.
Ex: reset members should ideally function identical to the argument
corresponding ctors as demonstrated in the following example:
Errno T::reset(Type1 a, Type2 b) try {
T obj(a,b);
swap(*this,obj);
return Ok;
}
catch(const Errno& e) { // could be many such catch clauses.
return e; // This is simplied for demo.
};
Note: This rule is important. Real practice may want do things
slightly different in some cases. But no, hidden and hard to
find bugs may exist (maybe the concept or interface of the
class design or naming should be reconsidered).
Note: For reset() (no argument), postcondition of the object is
always default regardless of the Errno returned or stack was
unwinded.
~T Destruct object
bool is_default() const
return true iff object is equivalent to the default constructed
object (i.e a.is_default() <==> a==T(), if operator== is defined).
bool operator==(const T& rhs) const
This function returns true iff *this and rhs are not distinguishable
by using any non-private member on the same condition unless
otherwise explicitly specified.
Note: Object equivalence does not include equivalence of meta info.
and address of its own.
T& operator=(const T&)
Reconstruct object to the state as the copy constructor constructed
(same as reset(const T&) except the return type and throwing error).
void swap(&T)
Exchange object state of *this and the argument indicated object.
Take 'a.swap(b)' (commutative) for instance, the state of a is set
to the state of b. The previous state of a becomes the state of b.
The same applies for b.
Note: No construct and destruct semantics is involved
Reply Class specific throw class inheriting Errno to help identify the
throwing source.
_.. Prefix for specific-implemented members, different rule,...etc.
c_.. Prefix for C stuff. Rule of this member may be different.
wy_.. Prefix for internal members (for testing, etc.).
------
Back to the begin:
Not all encapsulated is OOP.
OOP with class is about the complete set of concise and efficient class function
members. The point is about correctness, not only of the codes but more
importantly, of the concept and idea of the class. The idea is not right,
hardly can the class be. If this complete set of class function members is not
doable for no good reasons, such an idea of the class should be abandoned. OOP
in class is not form only, but also semantics.
-----
Real class has various restrictions, but you get the idea. If the class cannot
explain by the above criteria, it is flawed, not a good class/idea/concept.