C++ is a powerful programming language with tons of language features and hard-to-understand syntaxes. However, I am only interested in its object oriented features such as Class, Inheritance, Composition, Polymorphism, blah blah. Thanks to my background knowledge in OO, it did not take me long to understand all these.
I tried to create a very simple Polymorphism example in C++ using the famous open-source IDE, Dev-C++. Unfortunately, I experienced some problems about building C++ project with multiple files. The IDE kept telling me that there is multiple definition of functions in my code. So, I went googling and found this page, Organizing Code Files in C and C++. It provided me the basic understandings of compiling and linking process.
After finished coding the C++ example, I thought I should create the Java and C# version to compare with it too. I noticed that C# is more like C++ in that the functions (or methods) to be overriden must declared to be virtual while there is no such modifier in Java.
Here are my output and codes.
C++
main.cpp
#include
#include "shape.h"
#include "circle.h"
using namespace std;
int main(int argc, char *argv[])
{
Shape *shape = new Shape();
shape->calculateArea();
shape->test();
Shape *circle = new Circle(10);
circle->calculateArea();
circle->test();
system("PAUSE");
return EXIT_SUCCESS;
}
shape.h
#ifndef _SHAPE_CLASS
#define _SHAPE_CLASS
class Shape{
public:
virtual void showName();
virtual double calculateArea();
void test(){
showName();
}
};
#endif
shape.cpp
#include "shape.h"
#include
using namespace std;
void Shape::showName(){
cout << "Shape: I am a shape!" << endl;
}
double Shape::calculateArea(){
cout << "Shape: Dunno how to calc my area ..." << endl;
return 0.0;
}
circle.h
#ifndef _CIRCLE_CLASS
#define _CIRCLE_CLASS
#include "shape.h"
class Circle : public Shape{
public:
Circle(double radius) : radius_(radius){}
virtual void showName();
virtual double calculateArea();
protected:
double radius_;
};
#endif
circle.cpp
#include "circle.h"C#
#include
#include
using namespace std;
void Circle::showName(){
cout << "Circle: I am a circle!" << endl;
}
double Circle::calculateArea(){
cout << "Circle : My area = " << M_PI*radius_*radius_ << endl;
return M_PI*radius_*radius_;
}
Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpPolymorphism
{
class Program
{
static void Main(string[] args)
{
Shape shape = new Shape();
shape.showName();
shape.calculateArea();
Circle circle = new Circle(10.0);
circle.showName();
circle.calculateArea();
Console.Read();
}
}
}
Shape.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpPolymorphism
{
class Shape
{
public virtual void showName()
{
Console.WriteLine("Shape: I am a shape!");
}
public virtual double calculateArea()
{
Console.WriteLine("Shape: Dunno how to calc my area ...");
return 0.0;
}
}
}
Circle.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpPolymorphism
{
class Circle : Shape
{
protected double radius;
public Circle(double radius)
{
this.radius = radius;
}
public override void showName()
{
Console.WriteLine("Circle: I am a circle!");
}
public override double calculateArea()
{
Console.WriteLine("Circle : My area = " +
Math.PI * radius * radius);
return Math.PI * radius * radius;
}
}
}
Java
Main.java
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Shape shape = new Shape();
shape.showName();
shape.calculateArea();
Circle circle = new Circle(10.0);
circle.showName();
circle.calculateArea();
}
}
Shape.javapublic class Shape {
/** Creates a new instance of Shape */
public Shape() {
}
public void showName(){
System.out.println("Shape: I am a shape!");
}
public double calculateArea(){
System.out.println("Shape: Dunno how to calc my area ...");
return 0.0;
}
}
Circle.java
public class Circle extends Shape{
protected double radius;
/** Creates a new instance of Circle */
public Circle(double radius) {
this.radius = radius;
}
public void showName() {
System.out.println("Circle: I am a circle!");
}
public double calculateArea() {
System.out.println("Circle : My area = " + Math.PI*radius*radius);
return Math.PI*radius*radius;
}
}