Tuesday, April 17, 2007

Polymorphism example in Java, C# and C++

It has been a while since I coded my first C program. Now, because many of my friends at the department began to interest themselves in C++ programming. So I think I should learn some of the basics of it too :P

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"
#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_;
}


C#
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.java
public 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;
}

}

2 comments:

Roadrunner Email Support said...

enjoy the world’s best services and unmatched support with Roadrunner Customer Service which is available 24 hours a day and 7 days a week. You will be connected to our technicians who are available round the clock without any interruption. Roadrunner email is basically used for personal and professional medium. It is an Internet Service Provider which comes with extra features such as online address book, large storage capacity, message sorting, security, user authentication, and authorization. Fix Roadrunner email problems arise while using it but need not to worry. Our Roadrunner Support comprises a team of highly qualified and certified professionals who know techniques to resolve all the issues.

Hoffman Estates Vacuum Repair said...

Hii great reading your post