Hi, Frank Masingill!
On 04 Sep 97 06:46:30 you wrote to Kurt Kuzba
FM> Kurt and Cliff and Jerry, I worked this out just by looking at
FM> patterns and I think some of it is beginning to sink in. It seems like
FM> beating a dead horse to you fellows but for me, I think I'm edging
FM> forward. Maybe this will help some other novice trying to learn
FM> classes. Fortunately, I don't mind being laughed at!!
FM> Criticize away if you have time and inclination.
Hm, let's give it a try.
FM> ----------------------------START HERE-----------------------------
FM> // Example of a class with only one data item.
FM> // but the data item can be used in as many objects as you create
Here we miss (in a comment block) the whole (intended) purpose of the class.
That's one of the most important parts of te proggy.
FM> #include
FM> #include
FM> class templatex // no objects created this is only a pattern
FM> {
FM> private:
FM> int dataTemp1; // private data is going to be an int
FM> int dataTemp2; // private data is going to be an int also
Those comments are useless, and the purpose of the veriables are missing. The
name of them are misguiding.
FM> int dataTempSum;
This member needs very careful commenting (see later).
FM> public:
FM> void setupData(int x,int y, int z) // define & init data member function
This should better been the ctor.
FM> {
FM> dataTemp1 = x;
FM> dataTemp2 = y;
FM> z = x+y;
FM> dataTempSum = z;
FM> }
Hm. Why z is a parameter? The passed value is overwritten before the value
could be used. Another unclear point is why you even do a calculation on it,
instead of sending the sum directly to dataTempSum.
FM> void showData() // member function to display the data
I'd not hardcode cout into the function. Maybe a proto of
void showData(ostream & outstream = cout)
could make a better job.
FM> {
FM> //display with iostream
FM> cout << dataTemp1 << "+" << dataTemp2 << "=" << dataTempSum
FM> << endl;
FM> }
FM> };
FM> void main()
FM> {
FM> templatex a,b,c; // create three objects of class templatex
FM> int d;
FM> a.setupData(24,38,d); // place data into a.setupData member function
Here you can see that the third parameter is just a "I must provide, but have
no meaning".
FM> b.setupData(25,39,d); // place data into b.setupData member function
FM> c.setupData(26,40,d); // place data into c.setupData member function
Another nor useful comments.
FM> --------------------------------END HERE------------------------------
Some other points on the concept.
In dataTempSum you store a precalculated function of another stored members.
This can bee good but introduces negative redundancy in the data so that's a
potentionally risky situation. One must think about the pros and cons what is
better.
As you can guess, the precalculation benefits if the function itself is time-
consuming or you fetch that result very often. The tradeoff is a couple bytes
of memory allocated with every instance of the class, and possibly the more
important one: you must not forget to call the calculation function whenever
the other data members change.
In your case those variables are private that is the safest option, as only
function in this actual class definition will have a chance to change them.
Take a look at this example for the same topic
- -------------------- (public domain code) ---------
/* class myAdd is created just to demonstrate how we
store data in the class and how we use wery simple member
functions
The only functionality of the class is to add the two
members given at construction
*/
#include
class MyAdd {
private:
// data members
int m_member1; // first member of addition
int m_member2; // second member of addition
// they must be private as we precalculate the sum.
int m_sum; // the precalculated sum goes here.
// never forget to call Precalc() if amy member change
// functions
void Precalc() // make consistent state of the class data
{ m_sum = m_member1 + m_member2; }
public:
// ctor
MyAdd(int mem1 = 0, int mem2 = 0) : m_member1(mem1),
m_member2(mem2)
{ Precalc(); }
// data member setter and fetcher functions
int GetMem1() const {return m_member1;}
int GetMem2() const {return m_member2;}
int GetSum() const {return m_sum;}
void SetMem1(int new_mem1)
{m_member1 = new_mem1; Precalc();}
void SetMem2(int new_mem2)
{m_member2 = new_mem2; Precalc();}
void SetBoth(int new_mem1, int new_mem2)
{m_member1 = new_mem1; m_member2 = new_mem2;
Precalc();}
// output function
ostream & PrintOn (const char * name = 0, ostream & fo = cout) const
{
fo << "MyAdd (" << (name ? name : "-") << "): "
<< m_member1 << "+"
<< m_member2 << "="
<< m_sum << endl;
return fo;
}
}; // class MyAdd
// we just test that MyAdd class
int main()
{
MyAdd a0; // try an empty one
MyAdd a1(1); // one arg
MyAdd a23(2,3); // two args
cout << "Init state\n";
a0.PrintOn("a0");
a1.PrintOn("a1");
a23.PrintOn("a23");
cout << "\nAll further printout is for a23\n";
cout << "mem1: " << a23.GetMem1()
<< " mem2: " << a23.GetMem2()
<< " sum: " << a23.GetSum() << endl;
cout << "\nnow we change mem1, mem2 then both\n";
a23.SetMem1(10); a23.PrintOn();
a23.SetMem2(21); a23.PrintOn();
a23.SetBoth(32,45); a23.PrintOn();
cout << "\nThat's all, folks!" << endl;
return 0;
}
- -----------------------------------------------------------------------
Paul
... Have a Nice Day !
--- OS/2 Warp
---------------
* Origin: The FlintStones' Cave in BedRock (2:371/20)
|