Monday, December 12, 2011

[Cpp-Programming] Re: Inheritance and termination housekeeping

I often struggle to find words to express my ideas. Best way I can
express my gratitude right now is;

My hat off to you sir!

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

[Cpp-Programming] Re: Inheritance and termination housekeeping

also note in these lines from above:

> int a; // declaration
> a = 42; // first assignment, or "initialisation" of a

I've written initialisation in quotes:

when you're thinking in terms of an object of type A the declaration
"A a;" would call the default constructor of A and thus actually
initialise the variable...
in a sense the default constructor of any base type is also
initialising the variable. that value is arbitrary, though: usually
the value in an "uninitialised" base type variable is whatever value
the allocated block of memory held before. again in terms of objects
you can think of this as the default constructor of base types.

when you're not using the constructor initialisation list for your
constructor, remember that all the members were initialised before you
enter the constructor body with their default constructor (this holds
true for actual class type members, your base classes and in the above
sense also for base type members). this is why your pointers will
usually not point to NULL but any arbitrary address - which you should
avoid categorically.

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

[Cpp-Programming] Re: Inheritance and termination housekeeping

In C++ you have declarations and initialisations of variables:

int a; // declaration
a = 42; // first assignment, or "initialisation" of a

int b = 13; // declaration + initialisation

where "int b = 13;" may also be written as "int b(13);" which does
exactly the same, but is called "constructor initialization"
as a reference see under section "Initialization of variables" here:
http://www.cplusplus.com/doc/tutorial/variables/

for members of a class the concept of constructor initialisation is
used in constructor initialisation lists
as a reference see here: http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/

I find it good practice to use constructor initialisation lists.
And as a rule of thumb: always initialise your pointers (to allocated
memory or NULL) ;-)

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

[Cpp-Programming] Re: Inheritance and termination housekeeping

Can you explain this line of code in detail?

CEmployee::CEmployee(int s) : salary(s), fname(NULL), lname(NULL) { }

Thanks

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

[Cpp-Programming] Re: Inheritance and termination housekeeping

This is what I was looking for-a simple straight answer. While I was
posting it occured to me in my head, the solution you have outlined. I
am working off a Dietel and Dietel book for C++. They use char
through-out and the dynamic memory allocation technique is verbatim
copy of what they illustrate in the book. In any event, thank you my
good sir!

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

[Cpp-Programming] Re: Inheritance and termination housekeeping

Hi!

> man object will cause program to crash because it does not use
> dynamic memory. To protect against this crash, I have used infamous
> IsBadStringPtr.  How can this program be structured to resolve this
> problem?

First and foremost I would recommend you use std::string, which does
exactly what you want, but safely!
have a look here: http://www.cplusplus.com/reference/string/string/

Second... if you have to use your own unsafe character buffers...
which you really should not do and you should regard this as an FYI
only:
Deleting NULL will always work. So set your fname and lname to NULL in
case you don't use any first+last names.

> CEmployee::CEmployee(int s)
> {
>         salary = s;
>
> }

change to
CEmployee::Cemployee(int s) : salery(s), fname(NULL), lname(NULL) { }

> CEmployee::~CEmployee(void)
> {
>         if(!IsBadStringPtr(fname, 25))
>         {
>                 delete [] fname;
>                 delete [] lname;
>         }}

change to
CEmployee::~CEmployee() {
delete[] fname; // it's ok if pointer is NULL
delete[] lname;
}

Greets,
BeyelerStudios

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

[Cpp-Programming] Inheritance and termination housekeeping

// Base class declaration
class CEmployee
{
public:
CEmployee(char* f, char* l, int salary);
CEmployee(int s);
~CEmployee(void);
int getSalary();

int age;

protected:
char* fname;
char* lname;
int salary;
};


// Derived class declaration
class CEmpManager : public CEmployee
{
public:
CEmpManager(int age, int s);
CEmpManager(int age, char* f, char* l, int s);
int getAge();

~CEmpManager(void);


private:
int salary;
};


// CLASS DEFINITIONS

// CEmployee is base class
CEmployee::CEmployee( char* f, char* l, int s)
{
fname = new char [25];
lname = new char [25];
strcpy(fname, f);
strcpy(lname, l);
salary = s;
}
CEmployee::CEmployee(int s)
{
salary = s;

}
CEmployee::~CEmployee(void)
{
if(!IsBadStringPtr(fname, 25))
{
delete [] fname;
delete [] lname;
}
}
int CEmployee::getSalary()
{
return salary;
}


// CEmpManager is derived class, i.e, inherits from CEmployee above
CEmpManager::CEmpManager(int a, int s) : CEmployee(s)
{
salary = s;
age = a;
}
CEmpManager::CEmpManager(int a, char* f, char* l, int s) :
CEmployee(f, l, s)
{
salary = s;
age = a;
}
CEmpManager::~CEmpManager(void)
{
}
int CEmpManager::getAge()
{
return age;
}


void main(void)
{
// Start instantiating objects
CEmployee emp("Joe", "Black", 2000);

cout << "Employee salary: " << emp.getSalary() << endl;

CEmpManager man(45, 555);
CEmpManager man2(50, "Foo", "Bar", 56000);

cout << "Manager salary: " << man.getSalary() << endl;

cout << "Manager2 salary: " << man2.getSalary()
<< " Age: " << man2.getAge() << endl;
}


emp object uses dynamic memory. man object does not use dynamic
memory. man2 uses dynamic memory allocation. All three, emp, man, and
man2 will call:

CEmployee::~CEmployee(void)
{
if(!IsBadStringPtr(fname, 25))
{
delete [] fname;
delete [] lname;
}
}

man object will cause program to crash because it does not use
dynamic
memory. To protect against this crash, I have used infamous
IsBadStringPtr. How can this program be structured to resolve this
problem?


--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.