Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Polymorphism in C

, 5 Jul 2005 CPOL
The article demonstrates how to implement polymorphism using the C language.
polyc_demo.zip
PolyC.exe
polyc_src.zip
Polyc
PolyC.dsp
poly_src.zip
Poly
Poly.dsp
#include <stdio.h>

#include "Class.h"

void main()
{
	int nChoice;
	X* pClass = NULL;

	do
	{
		printf("\n1. Class X");
		printf("\n2. Class Y");
		printf("\n3. Class Z");
		printf("\n0. Exit\n");
		printf("\nEnter Choice: ");

		scanf("%d", &nChoice);
		printf("\n");

		switch (nChoice)
		{
		case 0:
			break;

		case 1:
			pClass = new X;
			break;

		case 2:
			pClass = new Y;
			break;

		case 3:
			pClass = new Z;
			break;

		default:
			printf("\nInvalid Choice\n");
		}

		if (pClass)
		{
			pClass->One();
			pClass->Two();
			pClass->Three();

			delete pClass;
			pClass = NULL;
		}
	}
	while (nChoice);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

«_Superman_»
Architect
India India
Santosh works as a Technical Architect in God's own Country, Kerala, India. He has been involved with C/C++ since 1996. Started with a few game clones and then went on to commercial software. He started his stint with software training and then went on to professional software development, design and architecture. Unix and C were his favorite in his early days but later grew very fond of Windows especially with the release of Windows NT 4.0 and Visual C++ 6.0. Technologies like COM and .Net fascinate him. He still lurks around Unix once in a while.
 
Music and the guitar are his second favorites and he manages to impress his friends with these skills when there are nobody better than him around. He is a patient and fun loving character who does not think too much about the future. He hates wasting time and so is working hard (in his dreams) to perfect instant transportation and time travel.
 
Oh! Yes. He loves Superman. Always did and always will. He would love to become a Superman and rid the world of all evil.
 
He used to be a Microsoft Visual C++ MVP (October 2009 - September 2013)

| Advertise | Privacy | Mobile
Web04 | 2.8.141029.1 | Last Updated 5 Jul 2005
Article Copyright 2005 by «_Superman_»
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid