A simple RTTI demonstration in C++ C++.


Source listing for the CShape parent class
//======================================================================
// CShape.h
// Purpose: Interface file defining a base class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#ifndef CSHAPE_H
#define CSHAPE_H

#include <string.h>

enum SHAPE_TYPE {SHAPE, RECTANGLE, OVAL, LINE, POLYGON};

class CShape
{
	protected:
		SHAPE_TYPE m_eType;
		char m_sTypeName[32];

	public:
		CShape();
		~CShape();
		virtual SHAPE_TYPE getType();
		virtual char *getTypeName();
};
#endif

//======================================================================
// CShape.cpp
// Purpose: Implementation file defining a base class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#define _CRT_SECURE_NO_WARNINGS

#include "CShape.h"


//---------------------------------------
// Default constructor
//---------------------------------------
CShape::CShape()
{
	m_eType = SHAPE;
	strcpy(m_sTypeName, "CShape");
}

//---------------------------------------
// Default destructor
//---------------------------------------
CShape::~CShape()
{
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
SHAPE_TYPE CShape::getType()
{
	return m_eType;
}

//---------------------------------------
// Virtual function to return type name
//---------------------------------------
char *CShape::getTypeName()
{
	return m_sTypeName;
}

Source listing for the CRectangle sub-class
//======================================================================
// CRectangle.h
// Purpose: Interface file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#ifndef CRECTANGLE_H
#define CRECTANGLE_H

#include "CShape.h"

class CRectangle : public CShape
{
	public:
		CRectangle();
		~CRectangle();
		SHAPE_TYPE getType();
		char *getTypeName();
};

#endif

//======================================================================
// CRectangle.cpp
// Purpose: Implementation file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#define _CRT_SECURE_NO_WARNINGS

#include "CRectangle.h"

//---------------------------------------
// Default constructor
//---------------------------------------
CRectangle::CRectangle()
{
	m_eType = RECTANGLE;
	strcpy(m_sTypeName, "CRectangle");
}

//---------------------------------------
// Default destructor
//---------------------------------------
CRectangle::~CRectangle()
{
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
SHAPE_TYPE CRectangle::getType()
{
	return m_eType;
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
char *CRectangle::getTypeName()
{
	return m_sTypeName;
}


Source listing for the COval sub-class
//======================================================================
// COval.h
// Purpose: Interface file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#ifndef COVAL_H
#define COVAL_H

#include "CShape.h"

class COval : public CShape
{
	public:
		COval();
		~COval();
		SHAPE_TYPE getType();
		char *getTypeName();
};

#endif

//======================================================================
// COval.cpp
// Purpose: Implementation file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#define _CRT_SECURE_NO_WARNINGS

#include "COval.h"

//---------------------------------------
// Default constructor
//---------------------------------------
COval::COval()
{
	m_eType = OVAL;
	strcpy(m_sTypeName, "COval");
}

//---------------------------------------
// Default destructor
//---------------------------------------
COval::~COval()
{
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
SHAPE_TYPE COval::getType()
{
	return m_eType;
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
char *COval::getTypeName()
{
	return m_sTypeName;
}

Source listing for the CLine sub-class
//======================================================================
// CLine.h
// Purpose: Interface file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#ifndef CLINE_H
#define CLINE_H

#include "CShape.h"

class CLine : public CShape
{
	public:
		CLine();
		~CLine();
		SHAPE_TYPE getType();
		char *getTypeName();
};

#endif

//======================================================================
// CLine.cpp
// Purpose: Implementation file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#define _CRT_SECURE_NO_WARNINGS

#include "CLine.h"

//---------------------------------------
// Default constructor
//---------------------------------------
CLine::CLine()
{
	m_eType = LINE;
	strcpy(m_sTypeName, "CLine");
}

//---------------------------------------
// Default destructor
//---------------------------------------
CLine::~CLine()
{
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
SHAPE_TYPE CLine::getType()
{
	return m_eType;
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
char *CLine::getTypeName()
{
	return m_sTypeName;
}

Source listing for the CPolygon sub-class
//======================================================================
// CPolygon.h
// Purpose: Interface file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#ifndef CPOLYGON_H
#define CPOLYGON_H

#include "CShape.h"

class CPolygon : public CShape
{
	public:
		CPolygon();
		~CPolygon();
		SHAPE_TYPE getType();
		char *getTypeName();
};

#endif

//======================================================================
// CPolygon.cpp
// Purpose: Implementation file defining a derived class for this RTTI 
//				demonstration.
// Author: Dr. Rick Coleman
//======================================================================
#define _CRT_SECURE_NO_WARNINGS

#include "CPolygon.h"

//---------------------------------------
// Default constructor
//---------------------------------------
CPolygon::CPolygon()
{
	m_eType = POLYGON;
	strcpy(m_sTypeName, "CPolygon");
}

//---------------------------------------
// Default destructor
//---------------------------------------
CPolygon::~CPolygon()
{
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
SHAPE_TYPE CPolygon::getType()
{
	return m_eType;
}

//---------------------------------------
// Virtual function to return type
//---------------------------------------
char *CPolygon::getTypeName()
{
	return m_sTypeName;
}

Source listing for the demonstration.
//======================================================================
// RTTI_Demo1Main.cpp
// Purpose: This is the simpliest type of Run Time Type Identification 
//		possible, but it does require that you define all the subclasses 
//		with the appropriate type ID field and that you use virtual 
//		functions to get the type.
// Author: Dr. Rick Coleman
//======================================================================
#include <vector>
#include "CShape.h"
#include "CRectangle.h"
#include "COval.h"
#include "CLine.h"
#include "CPolygon.h"
#include <iostream>

using namespace std;

void main()
{
	vectorsVec;

	// Create some shapes and add to the vector
	sVec.push_back(new CRectangle());
	sVec.push_back(new COval());
	sVec.push_back(new CLine());
	sVec.push_back(new CPolygon());
	sVec.push_back(new CRectangle());
	sVec.push_back(new COval());
	sVec.push_back(new CLine());
	sVec.push_back(new CPolygon());
	sVec.push_back(new CRectangle());
	sVec.push_back(new COval());
	sVec.push_back(new CLine());
	sVec.push_back(new CPolygon());

	// See what we have
	for(vector::iterator it=sVec.begin(); it != sVec.end(); it++)
	{
		cout << "Type = " << (*it)->getType() << ", Class = " << (*it)->getTypeName() << endl;
	}
	// Remember, the iterator is a pointer to the object in the vector, but in this case the
	//  object is itself a pointer to a CShape object.  Thus, the need to dereference the it
	//  pointer (*it) to get the CShape pointer which then must use the pointer operator (->)

}