CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: class

  1. #1

    class

    HeaderFile:
    Code:
    #ifndef __CSHAPE_H
    #define __CSHAPE_H
    
    #include <string.h>
    #include <iostream.h>
    
    class cSHAPE
    {
       protected:	//Member Variables
    
    	char* ptrName;
    
       public:     //Methods	
    	 //Constructor	   
    	   cSHAPE(const char* ptr) 
    	   { 
    		   ptrName=NULL;
    		   if (ptr) 
    		   {
    			   ptrName = new char[strlen(ptr)+1]; 
    			   strcpy(ptrName,ptr);
    		   }
    	   }
    
    	   ~cSHAPE()
    	   {
    		   if (ptrName)
    			   delete[] ptrName;
    	   }
    
    	   const char* getName() 
    	   {
    		   return(ptrName);
    	   }
               virtual void displayShape() = 0;
    	   virtual void eraseShape() = 0;
    };
    
    {
    public:
    	cCIRCLE():cSHAPE("Circle")
    	{}
    
    	void displayShape()
    	{
    		cout << "-   " << ptrName << " has a radius "<< endl;
    	}
    	
    	void eraseShape()
    	{
    		cout << "-   "  << "Erasing:" << ptrName << endl;
    	}
    };
    class cTRIANGLE:public cSHAPE
    {
    public:
    	cTRIANGLE():cSHAPE("Triangle")
    	{}
    
    	void displayShape()
    	{
    		cout << "-   "  << ptrName << " can be located in Bermuda "<< endl;
    	}
    	
    	void eraseShape()
    	{
    		cout << "-   "  << "Erasing:" << ptrName<<  endl;
    	}
    };
    #endif
    CPP file:
    Code:
    #include "cshape.h"
    #include <iostream.h>
    
    const int MAX = 3;
    void main (void)
    {
    	cCIRCLE c1;
    	cTRIANGLE t1;	
    	cRECTANGLE r1;
    	cSHAPE arShape(MAX); <---------ERROR
    }
    i want to declare arSHAPE as an array pf 3 pointers to cSHAPE and initilize each object in the array
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News back2games.com ::: Coming this Summer

  2. #2
    ok i have init. arSHAPE
    Code:
    cSHAPE *arShape[MAX];
    but how would i assign arShape[0] to c1, [1] to t1, and [2] to r1
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News back2games.com ::: Coming this Summer

  3. #3
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Code:
    cSHAPE *arShape[MAX];
    cCIRCLE c1;
    cTRIANGLE t1;	
    cRECTANGLE r1;
    arShape[0] = &c1;
    arShape[1] = &t1;
    arShape[2] = &r1;

  4. #4
    thxs that did it (forgot to put the reference)

    in my header file
    Code:
     void doNonVirtual()
    	   {
    		   cout << "-   I am the doNonVirtual of the base class" << endl;
    	   }
    in my cpp file
    Code:
    for (int i=0;i < MAX ; i++)
    	{
    cout << " Object is a " << arShape[i]->getName() << endl;		
    		cout << "calling doNonVirtual Function" << arShape[i].doNonVirtual();
    	}
    	system("PAUSE");
    }
    when i call the virtual function it gives me a syntax error, why is that
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News back2games.com ::: Coming this Summer

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Code:
    arShape[i]->doNonVirtual()

  6. #6
    what!
    i tried this in a cout statement and it kept giving me an error, but when i tried it without cout it works. how come???

    and why does it call the function in the superclass rather than the 1 in subclass?
    Last edited by dayknight; April 18th, 2004 at 05:00 PM.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News back2games.com ::: Coming this Summer

  7. #7
    i have declared a subclass that inherits cRECTANGLE subclass
    Code:
    class cSQUARE:public cRECTANGLE
    {
    public:
    	cSQUARE():cRECTANGLE()
    	{
    		ptrName = "SQUARE";
    	}
    	void displayShape()
    	{
    		cout << "-   "  << ptrName << " is a SQUARE since all sides are equal "<< endl;
    	}
    
    };
    now what i have to do i add the object to arShape and dynamically alocate memory for SQUARE.

    can some1 give me a hint how to do this?
    this is what i think but it is not working
    Code:
    if (arShape)
    	{
    		delete [] arShape;
    		new cSHAPE* arShape[4];
    		arShape[0] = &c1;
    		arShape[1] = &t1;
    		arShape[2] = &r1;
    		arShape[3] = &s1;
    	}
    Last edited by dayknight; April 18th, 2004 at 05:46 PM.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News back2games.com ::: Coming this Summer

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    a few notes:

    I don't know if your first post had the entire Shape class,
    but ...

    1) Shape (like any base class that you inherit publicly
    from) should have a virtual destructor. Also, your constructor
    does a "new []" , but where is the corresponding "delete []" ?
    (It probably should be in the destructor).

    2) you should probably have a copy constructor and
    an assignment operator for this class.

    3) "i tried this in a cout statement and it kept giving
    me an error, but when i tried it without cout it works. how
    come???"

    I would probably need to see complete code.

    4) "why does it call the function in the superclass rather
    than the 1 in subclass?"

    Which function are you talking about ? A virtual or non-virtual
    function ?

    5) Just a note: often deriving a square from a rectangle is
    not a good idea. For example, Rectangle might have a
    function which changes the height, but not the width. If
    a Square object called this function, it would no longer
    be a square.

  9. #9
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    Originally posted by dayknight
    what!
    i tried this in a cout statement and it kept giving me an error, but when i tried it without cout it works. how come???
    doNonVirtual() returns a void, and you're trying to pass that to the << operator of cout. That shouldn't and doesn't work.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449
    dayknight,

    1) the proper header is <iostream>, not <iostream.h>.

    2) it is int main(), not void main().

    3) But most important, and to add to what Philip has stated, your class is very unstable the way you've coded it. Here is a detailed explanation why:

    Your class has a pointer to dynamically allocated memory "ptrName", but there is no copy constructor, assignment operator. Also, the base class has no virtual destructor.

    My suggestion is to fix these problems now, before you write any other code to derive classes. For example, this very simple program below using your class leads to disastrous results:
    Code:
    // include the shape, circle, rectangle
    int main()
    {
        cCIRCLE c1;
        cCIRCLE c2;
        c1 = c2;   // assignment
        cCIRCLE c3 = c1;  // copy
    }  // destruction of c1, c2, c3
    The reason why this is disastrous is that since you have no proper copy constructor and assignment operator, c1's ptrName has the same value as c2's ptrName. Then c3 gets created, and its ptrName is the same as c1 (and c2).

    When c1, c2, and c3 get destroyed, the destructor for c1, c2, and c3 will attempt to delete ptrName. You now have not a double deletion error, but a multiple deletion error, where you are using "delete" on the same pointer 3 times. Error.

    Also, you now have a memory leak. The original ptrName value for c1 was never deleted when the assignment was done. Error.

    The reason why this is important is because copying and assignment is something that the compiler does to your objects without you having to explicitly do these operations yourself. For example, passing or returning by value requires the compiler to make temporary copies. If your object is not safely copyable, you will get errors as I've described above.

    The problem with no virtual destructor in the base class is easily demonstrated.
    Code:
    // include the shape, circle, rectangle
    int main()
    {
        cSHAPE *pCircle = new cCIRCLE;
        delete pCircle; // no virtual destructor -- undefined behavior
    }
    The rule in C++ is that if you dynamically create an object where the static type is different than the dynamic type (cSHAPE is the static type, cCIRCLEe is the dynamic type), the static type must have a virtual destructor. If the destructor is non-virtual in the static type, the behavior is undefined when the object is destroyed.

    Therefore there are many things you have to resolve before embarking on anything else with your cSHAPE, cCIRCLE, and cSQUARE classes.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 18th, 2004 at 09:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured