Thread: class

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    class

    Code:
    int MAX = 3;
    int main (void)
    {
    
     cCIRCLE c1;
     cTRIANGLE t1;	
     cRECTANGLE r1;
     cSHAPE arShape();  <---Error
    
    }
    how would i declare it so it intializes to c1,t1,and r1;
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    c1, t1 and r1 are all objects....what is arShape()?? Is it a function???
    simple is always an understatement.....

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    cSHAPE is a superclass
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    Yeah...but what is arShape()
    Last edited by sweets; 04-18-2004 at 01:39 PM.
    simple is always an understatement.....

  5. #5
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    arSHAPE is a variable intialized by cSHAPE class; it is a pointer to character.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    > cSHAPE arShape(); <---Error

    arShape is not a variable.... by putting ()'s you are saying its a function
    simple is always an understatement.....

  7. #7
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    here is the whole code:
    HEADER FILE:
    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;
    };
    
    class cCIRCLE:public cSHAPE
    {
    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;
    	}
    };
    class cRECTANGLE:public cSHAPE
    {
    public:
    	cRECTANGLE():cSHAPE("Rectangle")
    	{}
    
    	void displayShape()
    	{
    		cout << "-   "  << ptrName << " may be a square if all sides are equal "<< 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;
    
    	char szStr1[] = "cir";
    	char szStr2[] = "tri";
    	char szStr3[] = "rec";
    	cSHAPE arShape(????);
    }
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  8. #8
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    ok i have declared arShape
    Code:
    cSHAPE* arShape[MAX];
    now how would i assign arShape[1] to c1, [2] to t1, and [3] to r1
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    85
    if the cShape constructor is defined as: cSHAPE(const char* ptr) {...}

    wouldn't you have to change cSHAPE arShape(????); to cSHAPE arShape("const char pointer"); ?

  10. #10
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    Code:
    cSHAPE *arShape[MAX]
    where MAX is a const int

    initalizes three pointers to cSHAPE object, now i am trying to assign each pointer to each shape (ie: arShape[0] to circle, arShape[1] to triangle and arShape[2] to rectangle)
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  11. #11
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    k i got it
    Code:
    arShape = &c1
    .
    .
    .
    now i am trying to call a virtual function in my header file:
    Code:
    	   void doNonVirtual()
    	   {
    		   cout << "-   I am the doNonVirtual of the base class" << endl;
    	   }
    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");
    }
    but i am getting a syntax error when i call the virtual function
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    but i am getting a syntax error when i call the virtual function
    Which virtual function would that be? I don't see any in that code.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM