• Welcome to Jose's Read Only Forum 2023.
 

ProgEx25 - We've Finally Gotten To C++ Classes!

Started by Frederick J. Harris, November 09, 2009, 01:20:52 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/*
 ProgEx25   C++ Classes

 We've finally gotten to classes!  The below program is a strange one though.  
 Lets take a top level view of it first.  Note that I declared a pointer to a
 FILE struct at global scope.  I believe this might be my first use of a global
 entity so far in this series.  The reason I say it is at global scope is that
 it is declared outside of any functions - including main().  So its available
 everywhere in this program.  

 Further note that I open the file Output.txt as the 1st statement in main().
 Following that is a declaration of something called a Box, whatever that is.
 Well, a Box is the class declared right beneath the #include <stdio.h> and
 the FILE*.  This is backwards from what you've been seeing me do so far where
 I always declare my variables at the top of main seperated by a blank line and
 then followed with the program statements.

 I did this because I wanted the file to be existing and open when this line
 executes...

 Box bx;

 because the execution of that line will cause the Box Constructor to be
 called, and I wanted this line in the Box Constructor...

 fprintf(fp,"CBox Constructor Called!\n");  

 to satisfactorily print to the file.  After that the program essentially
 finishes as nothing else is done.  You'll see this line on your display...

 Box Constructor Called!

 but you won't see this line from the Box Destructor...

 Box Destructor Called!

 because the C++ runtime has already terminated its connection to the console
 window by the time it gets around to calling the destructor for the Box
 class.  However, you'll note my bad programming practice of not closing the
 file I opened.  There is no accompanying fclose(fp) as with every other
 program in which I've used files so far.  For you see, while the runtime has
 disconnected the console, the file is still open, and the Box destructor call
 shows up there!  Devious Huh?

 You see, in complex classes involving virtual inheritance programs can
 actually crash after they close.  What I've just showed you here is a good way
 to find out what is going on in the destructors for your classes.  While I
 don't anticipate getting into virtual inheritance and virtual destructors in
 this series, I wanted you to be aware of this technique at least.  Debugging
 is a very important topic.

 Having made that digression, lets finally take a look at the class itself.
 Classes in C++ evolved from a struct.  Instead of a struct keyword we have the
 class keyword.  Other keywords within a class can be 'public:', 'protected',
 and 'private'.  Note that the Box() Constructor and Destructor are both in
 the public interface to the class.  If they weren't, it wouldn't be possible
 to create instances of the class.  I put the length, width and height members
 in a private section of the class simply because this is usually the best
 practice, i.e., to not allow clients to access them directly.  Unlike with
 PowerBASIC, you can put any of the parts of a class anywhere you like.  I
 usually put my member variables at bottom.  You can put them at top if you
 like though.  Lets move on to the next program and we'll flush out some
  more details.

*/

#include <stdio.h>
FILE* fp;

class Box
{
public:
Box()   //CBox Uninitialized Constructor
{
 printf("Box Constructor Called!\n");
 fprintf(fp,"Box Constructor Called!\n");        
}
     
~Box()  //Box Destructor
{
 printf("Box Destructor Called!\n");
 fprintf(fp,"Box Destructor Called!\n");
}
 
private:
double m_Length;
double m_Width;
double m_Height;  
};    

int main(void)
{
fp=fopen("Output.txt","w");
Box bx;
getchar();  
   
return 0;  
}

/*  --Screen Output--
CBox Constructor Called!
*/

/*  --File Output--
CBox Constructor Called!
CBox Destructor Called!
*/