C:\cygwin\/Shapes.h:35: error: stray ‘\2’ in program (Repeating occasionally)
.
C:\cygwin\/Shapes.h:37: error: stray ‘\34’ in program (Repeating occasionally)
.
C:\cygwin\/Shapes.h:49: error: stray ‘\24’ in program (Randomly thrown in once)
.
. (skipping down a few hundred warnings)
.
C:\cygwin\/Shapes.h:1409:4: error: invalid preprocessing directive #e
C:\cygwin\/Shapes.h:1409:5: warning: null character(s) ignored
C:\cygwin\/Shapes.h:1409:7: warning: null character(s) ignored
C:\cygwin\/Shapes.h:1409:9: warning: null character(s) ignored
C:\cygwin\/Shapes.h:1409:11: warning: null character(s) ignored
C:\cygwin\/Shapes.h:1409:13: warning: null character(s) ignored
C:\cygwin\/Shapes.h:1: error: expected unqualified-id before ‘/’ token
C:\cygwin\/Shapes.h:77: error: expected unqualified-id before ‘/’ token
C:\cygwin\/Shapes.h:81: error: expected unqualified-id before ‘/’ token
If anyone can point me in the right direction to find what these errors mean, I'd be very grateful!
What I do know is if I don't include (at least empty) virtual deconstructors in class Shape (base class), class TwoDimensionalShape, and class ThreeDimensionalShape, I get a few more error messages. Again, with or without that change, Visual c++ 2008 compiled with NO errors or warnings...
Below is my Shapes.h file code followed by my Main.cpp test code (with the output at the end commented out).
P.S. If anyone notices the formulas are wrong...please let me know... Thanks in advanced!
/*
* Program : Shapes.h
* Description : Header file containing a hierarchy of
* Shape based classes.
* --The bottom two levels in the hierarchy
* are pure virtual classes.
* --The classes derived from TwoDimensionalShape
* have working Area() methods.
* --The classes derived from ThreeDimensionalShape
* have working Area() and Volume() methods.
*
* Base class Shape
* │
* ├-derived class TwoDimensionalShape
* │ ├-class Square
* │ ├-class Rectangle
* │ ├-class Circle
* │ └-class Triangle
* │
* └-derived class ThreeDimensionalShape
* ├-class Cube
* ├-class Sphere
* ├-class Tetrahedron
* └-class RectangularPrism
*/
#ifndef Shapes_H
#define Shapes_H
#include <cmath> // for pow() and sqrt() functions.
using namespace std; // used when calling <cmath> functions
const double pi = 3.14159; // for use with circle and sphere calculations.
/*
* Class Shape
*
* Purpose: Used as the base for TwoDimensionalShape
* and ThreeDimensionalShape.
*
* Contains: Pure Virtual methods Area() and Volume().
*/
class Shape
{
public:
virtual ~Shape() {};
virtual double Area() = 0;
virtual double Volume() = 0;
}; // end class Shape.
/*
* Class TwoDimensionalShape
*
* Purpose: Used as the base for 2D shapes including
* (but not limited to) Square, Circle,
* Triangle, and Rectangle.
*
* Contains: Pure Virtual methods Area() and Volume().
*/
class TwoDimensionalShape : public Shape
{
public:
virtual ~TwoDimensionalShape() {}
double Area() = 0;
double Volume() = 0;
}; // end class TwoDimensionalShape.
/*
* Class Square
*
* Purpose: Derived from class TwoDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) method Area() and
* private method Volume (returns 0 ).
*
* Details: Squares have 4 even sides of positive length.
* This is represented by one variable 'side'
* which defaults to 0 if a negative is entered.
*/
class Square : public TwoDimensionalShape
{
double side;
public:
Square() // Default
{
side = 0;
}
Square( Square& SQ1 ) // Copy
{
side = SQ1.side;
}
Square( double tempSide ) // Assignment
{
side = 0;
if( tempSide > 0 )
{
side = tempSide;
}
}
double Area() // Formula : edge^2
{
return ( pow(side, 2) );
}
private: double Volume() { return 0; }
}; // end class Square.
/*
* Class Rectangle
*
* Purpose: Derived from class TwoDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) method Area() and
* private method Volume (returns 0 ).
*
* Details: Rectangles have 2 sets of even sides of positive
* length. This is represented by two variables
* 'side1' and 'side2' which defaults to 0 if a
* negative is entered.
*/
class Rectangle : public TwoDimensionalShape
{
double side1,
side2;
public:
Rectangle() // Default
{
side1 = side2 = 0;
}
Rectangle( Rectangle& RCT) // Copy
{
side1 = RCT.side1;
side2 = RCT.side2;
}
Rectangle( double S1, double S2 ) // Assignment
{
side1 = side2 = 0;
if( ( S1 > 0 ) && ( S2 > 0 ))
{
side1 = S1;
side2 = S2;
}
}
double Area() // Formula: Length * Width
{
return( side1 * side2 );
}
private: double Volume() { return 0; }
}; // end class Rectangle.
/*
* Class Circle
*
* Purpose: Derived from class TwoDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) method Area() and
* private method Volume (returns 0 ).
*
* Details: Circles have a radius of (1/2)*Diameter.
* This is represented by the variable 'radius'
* which defaults to 0 if a negative is entered.
*/
class Circle : public TwoDimensionalShape
{
double radius;
public:
Circle() // Default
{
radius = 0;
}
Circle( Circle& CRC) // Copy
{
radius = CRC.radius;
}
Circle( double tempRadius ) // Assignment
{
radius = 0;
if( tempRadius > 0 )
{
radius = tempRadius;
}
}
double Area() // Formula: Pi(r^2)
{
return( pi * pow(radius, 2) );
}
protected: double Volume() { return 0; }
}; // end class Circle.
/*
* Class Triangle
*
* Purpose: Derived from class TwoDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) method Area() and
* private method Volume (returns 0 ).
*
* Details: Equilateral Triangle of positive size assumed.
* This is represented by one variable 'side'
* which defaults to 0 if a negative is entered.
* The 'height' variable is created automatically
* based on the 'side' value.
*/
class Triangle : public TwoDimensionalShape
{
double side;
double height;
public:
Triangle() // Default
{
side = height = 0;
}
Triangle( Triangle& OriginalTriangle ) // Copy
{
side = OriginalTriangle.side;
height = OriginalTriangle.height;
}
Triangle( double sideLength ) // Assignment
{
side = 0;
if( sideLength > 0 )
{
side = sideLength;
}
// height formula based on pythagorean theorem:
// side/2 = a, height = b, side = c
// b^2 = c^2 - a^2
height = sqrt( pow(side, 2) - pow( (side / 2.0) , 2) );
}
double Area() // Formula: (1/2)*base*height
{
return ( side * height / 2.0 );
}
private: double Volume() { return 0; }
}; // end class Triangle.
/*
* Class ThreeDimensionalShape
*
* Purpose: Used as the base for 3D shapes including
* (but not limited to) Cube, Sphere,
* Tetrahedron, and RectangularPrism.
*
* Contains: Pure Virtual Functions Area() and Volume().
*/
class ThreeDimensionalShape : public Shape
{
public:
virtual ~ThreeDimensionalShape() {}
double Area() = 0;
double Volume() = 0;
}; // end class ThreeDimensionalShape.
/*
* Class Cube
*
* Purpose: Derived from class ThreeDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) methods Area() and Volume().
*
* Details: Cubes are made of 6 squares with 12 even edges
* of positive length.
* This is represented by one variable 'side'
* which defaults to 0 if a negative is entered.
*/
class Cube : public ThreeDimensionalShape
{
double side;
public:
Cube() // Default
{
side = 0;
}
Cube( Cube& CBE ) // Copy
{
side = CBE.side;
}
Cube( double tempSide) // Assignment
{
side = 0;
if( tempSide > 0 )
{
side = tempSide;
}
}
//Surface Area
double Area() // Formula: (face count)*(face area)
{
return( 6 * pow(side, 2) );
}
double Volume() // Formula: (edge^3)
{
return( pow(side, 3) );
}
}; // end class Cube.
/*
* Class Sphere
*
* Purpose: Derived from class ThreeDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) methods Area() Volume().
*
* Details: Spheres have a radius of (1/2)*Diameter.
* This is represented by the variable 'radius'
* which defaults to 0 if a negative is entered.
*/
class Sphere : public ThreeDimensionalShape
{
double radius;
public:
Sphere() // Default
{
radius = 0;
}
Sphere( Sphere& tempSphere ) // Copy
{
radius = tempSphere.radius;
}
Sphere( double tempRadius ) // Assignment
{
radius = 0;
if( tempRadius > 0 )
{
radius = tempRadius;
}
}
// Surface Area
double Area() // Formula: (4) * Pi * (r^2)
{
return ( 4 * pi * pow( radius, 2) );
}
double Volume() // Formula: (4/3) * Pi * (r^3)
{
return ( (4 * pi * pow( radius, 3) ) / 3.0 );
}
}; // end class Sphere.
/*
* Class Tetrahedron
*
* Purpose: Derived from class ThreeDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) methods Area() and Volume().
*
* Details: Equilateral Tetrahedrons are made of 4 triangles
* with even edges of positive length.
* This is represented by one variable 'side'
* which defaults to 0 if a negative is entered.
* The 'height' variable is created automatically
* based on the 'side' value.
*/
class Tetrahedron : public ThreeDimensionalShape
{
double side,
height,
FaceHeight;
public:
Tetrahedron() // Default
{
side = 0;
height = 0;
}
Tetrahedron( Tetrahedron& OriginalTetra ) // Copy
{
side = OriginalTetra.side;
height = OriginalTetra.height;
FaceHeight = OriginalTetra.FaceHeight;
}
Tetrahedron( double sideLength ) // Assignment
{
side = 0;
if( sideLength > 0 )
{
side = sideLength;
}
// height formula : sqrt(6)/3 *side
height = ( ( sqrt(6.0) / 3.0 ) * side);
// FaceHeight formula based on pythagorean theorem:
// side/2 = a, height = b, side = c
// b^2 = c^2 - a^2
FaceHeight = sqrt( pow( side, 2 ) - pow( ( side /2.0 ), 2 ) );
}
// Surface Area
double Area() // Formula: (face count) * (face area)
{
return ( 2 * side * FaceHeight );
}
double Volume() // Formula: (1/3) * height * (face area)
{
return ( side * FaceHeight * height / 6.0 );
}
}; // end class Tetrahedron.
/*
* Class RectangularPrism
*
* Purpose: Derived from class ThreeDimensionalShape
* to demonstrate class hierarchy and
* polymorphism.
*
* Contains: Working (virtual) methods Area() and Volume().
*
* Details: Rectangular Prisms are made of 12 edges, every
* 4 parallel are even and of positive length.
* Each of the 3 groups are represented by variables
* 'length' , 'width' , and 'height' which default
* to 0 if a negative is entered.
* If even 1 side is 0 units long, the RectangularPrism
* no longer exists (reason for all to default to 0.)
*/
class RectangularPrism : public ThreeDimensionalShape
{
double length,
width,
height;
public:
RectangularPrism() // Default
{
length = width = height = 0;
}
RectangularPrism( RectangularPrism& OriginalRectPrism ) // Copy
{
length = OriginalRectPrism.length;
width = OriginalRectPrism.width;
height = OriginalRectPrism.height;
}
RectangularPrism( double Len, double Wid, double Hei ) //Assignment
{
length = width = height = 0;
if( ( Len > 0 ) && ( Wid > 0 ) && ( Hei > 0 ) )
{
length = Len;
width = Wid;
height = Hei;
}
}
//Surface Area
double Area() // Formula: 2* ( face1 area +
{ // face2 area +
// face3 area )
return ( 2 *( ( length * width ) +
( length * height ) +
( width * height ) ) );
}
double Volume() // Formula: length * width * height
{
return ( length * width * height );
}
}; // end class RectangularPrism.
#endif
Driving program:
/*
* Program : ShapesMain.cpp
*
* Description : Driving Program to Demonstrate
* the hierarchy of classes derived
* from base class Shape.
* Creates a vector of Shape pointers
* and assigns each a derived shape
* class with starting values.
* Verifies that each element in the
* vector belongs to the derived classes
* TwoDimensionalShape or ThreeDimensionalShape.
* Every TwoDimensionalShape prints its Area.
* Every ThreeDimensionalShape prints its Area
* and Volume.
*
* Response To : Exercise 13.13 [ Shape Hierarchy ]
*
*/
#include <iostream>
#include <typeinfo> // used with typeid() to print the name
// of each variable type created.
#include <vector> // vector required by
// Exercise 13.13 instructions.
#include "Shapes.h"
using namespace std;
// Renaming 2nd level Classes in hierarchy for ease of typing.
typedef TwoDimensionalShape Shape2D;
typedef ThreeDimensionalShape Shape3D;
// initiallized to 9 currently to allow for an empty Shape pointer.
const int vectorSize = 9;
int main( )
{
// vector of Shape pointers created:
vector< Shape * > ShapePtr( vectorSize );
// TwoDimensionalShape and ThreeDimensionalShape pointers created
// for use in testing for derived classes.
Shape2D *Temp2D_Ptr = 0;
Shape3D *Temp3D_Ptr = 0;
// creates one instance of each shape and assigns ShapePtr to point
// to each, but ShapePtr[8] remains 'empty'
ShapePtr[0] = new Square ( 3 );
ShapePtr[1] = new Circle ( 1 );
ShapePtr[2] = new Triangle ( 2 );
ShapePtr[3] = new Rectangle ( 2, 3 );
ShapePtr[4] = new Cube ( 3 );
ShapePtr[5] = new Sphere ( 1 );
ShapePtr[6] = new Tetrahedron ( 2 );
ShapePtr[7] = new RectangularPrism ( 1 , 2 , 3 );
// Loop to test each element of ShapePtr[].
for( int i = 0; i < vectorSize; i++ )
{
// Test ShapePtr to be sure it's of class Shape type. ( should always pass )
if( typeid( ShapePtr[i] ) == typeid( Shape * ) )
{
// casts current ShapePtr as TwoDimensionalShape pointer and ThreeDimensionalShape
// pointer and assigns it to Temp2D_Ptr and Temp3D_Ptr respectively.
Temp2D_Ptr = dynamic_cast< Shape2D *>( ShapePtr[i] );
Temp3D_Ptr = dynamic_cast< Shape3D *>( ShapePtr[i] );
cout << "ShapePtr[" << i << "] is of ";
// If the cast did not work:
if( !( Temp2D_Ptr ) && !( Temp3D_Ptr ) )
{
cout << "neither TwoDimensionalShape nor ThreeDimensionalShape !" << endl;
} // end if ( not 2DShape nor 3DShape )
// if the cast worked:
else
{
// Print the shape type and Area:
cout << typeid( *ShapePtr[i] ).name() << "." << endl;
cout << "\tArea : " << ShapePtr[i]->Area() << " units." << endl;
// if it's a ThreeDimensionalShape - Print the Volume also.
if( Temp3D_Ptr )
{
cout << "\tVolume : " << ShapePtr[i]->Volume() << " units." << endl;
} // end if ( 3DShape )
} // end else ( valid 2DShape or 3DShape test )
} // end if( typeid check )
// if a ShapePtr is not a vector of class Shape type:
else
{
cout << "\n\n This vector is not a Shape vector!!!" << endl;
} // end else ( base class NOT class Shape * )
cout << endl;
} // end for();
// clean up non-vector pointers.
delete Temp2D_Ptr;
delete Temp3D_Ptr;
return ( 0 );
}
/* Sample output
ShapePtr[0] is of class Square.
Area : 9 units.
ShapePtr[1] is of class Circle.
Area : 3.14159 units.
ShapePtr[2] is of class Triangle.
Area : 1.73205 units.
ShapePtr[3] is of class Rectangle.
Area : 6 units.
ShapePtr[4] is of class Cube.
Area : 54 units.
Volume : 27 units.
ShapePtr[5] is of class Sphere.
Area : 12.5664 units.
Volume : 4.18879 units.
ShapePtr[6] is of class Tetrahedron.
Area : 6.9282 units.
Volume : 0.942809 units.
ShapePtr[7] is of class RectangularPrism.
Area : 22 units.
Volume : 6 units.
ShapePtr[8] is of neither TwoDimensionalShape nor ThreeDimensionalShape !
*/
This post has been edited by Seyeco: 27 April 2010 - 11:53 PM

New Topic/Question
Reply



MultiQuote





|