Demonstration of Polymorphism

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Spacemail by SpaceshipSpacemail by Spaceship
Watch

qedft

Account Closed
Impact
0
Hello this is just a project I did when I was bored.. :-P it was programmed in Delphi C++, if anyone is interested I'm going to be posting the code below, enjoy.

main
Code:
#include <stdio.h>
#include <math.h>

#define PI 3.141592654

#include "shape.h"    // base class

#include "rect.h"     // specific derived classes
#include "tri.h"      // ...
#include "circle.h"   // ...
#include "pentagon.h" // ...

void DescribeShape(Cshape* s);  // takes an abstract pointer to a shape object
                                // and prints shape-specific info.

// Program Entry //////////////////////////////

int main(int argc, char *argv[])
{
Crect       r(4, 5);   // a rectangle, longest side 5, shortest side 4

Ccircle     c(3.5);    // a circle, radius 3.5

Cpentagon   p(2.567);  // a pentagon with sides of 2.567 units
Cpentagon   p2(p);     // a copy of the pentagon

Ctri        t(8);      // a triangle with side-length of 8 units
Ctri        t2;        // another triangle

t2 = t;     // set second triangle equal to first triangle (operator=)

// Print information about each of the shapes
DescribeShape(&r);
DescribeShape(&c);
DescribeShape(&p);
DescribeShape(&t);
DescribeShape(&p2);
DescribeShape(&t2);

// Prompt to end
printf("Press any key to quit.");
getchar();

return 0;
}

// Print shape information using abstract interface.
void DescribeShape(Cshape* s)
{
s->WhoAmI();
s->PrintLength();
s->PrintPerimeter();
s->PrintArea();
printf("\n\n");
}


header
Code:
#ifndef TRI_INCLUDED    // Circular include protection
#define TRI_INCLUDED

// Tri.h: derives a triangle shape from the base class

class Ctri : public Cshape
{
public:

Ctri();
Ctri(float length);
Ctri(const Ctri& r);

void WhoAmI();

};

Ctri::Ctri()
{
side_length = 4;
perimeter   = 6;
area        = side_length * ( sqrt((side_length*side_length) - (side_length/2)*(side_length/2)) );
}

Ctri::Ctri(float length)
{
side_length = length;
perimeter   = length*3;
area        = side_length * ( sqrt((side_length*side_length) - (side_length/2)*(side_length/2)) );
}

Ctri::Ctri(const Ctri& t)
{
perimeter   = t.perimeter;
area        = t.area;
side_length = t.side_length;
}

void Ctri::WhoAmI()
{
printf("I am a triangle.\n", perimeter);
return;
}

#endif


circle header
Code:
#ifndef CIRCLE_INCLUDED     // Circular include protection
#define CIRCLE_INCLUDED

// Circle.h: derives a circle shape from the base class

class Ccircle : public Cshape
{
public:

Ccircle();
Ccircle(float radius);
Ccircle(const Ccircle& r);

float radius;

void WhoAmI();
void PrintLength(); // Override this member because a circle doesn't have sides, it has a radius

Ccircle* operator= (const Ccircle& c);

};

Ccircle::Ccircle()
{
side_length = 0;
radius      = 4;
perimeter   = PI * (2 * radius);
area        = PI * (radius * radius);
}

Ccircle::Ccircle(float rad)
{
side_length = 0;
radius      = rad;
perimeter   = PI * (2 * rad);
area        = PI * (rad * rad);
}

Ccircle::Ccircle(const Ccircle& c)
{
radius      = c.radius;
perimeter   = c.perimeter;
area        = c.area;
side_length = c.side_length;
}

Ccircle* Ccircle::operator= (const Ccircle& c)
{
if (this != &c)
{
  perimeter   = c.perimeter;
  area        = c.area;
  side_length = c.side_length;
  radius      = c.radius;
}
}

void Ccircle::WhoAmI()
{
printf("I am a circle.\n");
return;
}

void Ccircle::PrintLength()
{
printf("My radius is %4.4f unit(s).\n", radius);
return;
}


#endif


pentagon header
Code:
#ifndef PENTAGON_INCLUDED   // circular include protection
#define PENTAGON_INCLUDED

// Pentagon.h: derives a pentagon shape from the base class

class Cpentagon : public Cshape
{
public:

Cpentagon();
Cpentagon(float side);
Cpentagon(const Cpentagon& r);

void WhoAmI();

};

void Cpentagon::WhoAmI()
{
printf("I am a pentagon.\n");
return;
}

Cpentagon::Cpentagon()
{
side_length = 2;
perimeter   = 10;
area        = 5 * (side_length * ( sqrt((side_length*side_length) - (side_length/2)*(side_length/2)) ));
}

Cpentagon::Cpentagon(float length)
{
side_length = length;
perimeter   = 5 * length;
area        = 5 * (side_length * ( sqrt((side_length*side_length) - (side_length/2)*(side_length/2)) ));
}

Cpentagon::Cpentagon(const Cpentagon& p)
{
perimeter   = p.perimeter;
area        = p.area;
side_length = p.side_length;
}

#endif


shape header
Code:
#ifndef SHAPE_INCLUDED  //Circular include protection
#define SHAPE_INCLUDED

// Shape.h: Defines the base class from which all shapes are derived

class Cshape
{
public:

Cshape();
Cshape(const Cshape& s);

float perimeter;
float area;
float side_length;

virtual void WhoAmI();
virtual void PrintPerimeter();
virtual void PrintArea();
virtual void PrintLength();

Cshape* operator= (const Cshape& s);

};

Cshape::Cshape()
{
perimeter   = 0;
area        = 0;
side_length = 0;
}

Cshape::Cshape(const Cshape& s)
{
perimeter   = s.perimeter;
area        = s.area;
side_length = s.side_length;
}

Cshape* Cshape::operator= (const Cshape& s)
{
if (this != &s)
{
  perimeter   = s.perimeter;
  area        = s.area;
  side_length = s.side_length;
}
}

void Cshape::WhoAmI()
{
printf("I am a generic shape.\n", perimeter);
return;
}

void Cshape::PrintPerimeter()
{
printf("My perimeter is %4.4f unit(s).\n", perimeter);
return;
}

void Cshape::PrintArea()
{
printf("My area is %4.4f square unit(s).\n", area);
return;
}

void Cshape::PrintLength()
{
printf("My sides are %4.4f unit(s) long.\n", side_length);
return;
}

#endif

If you have no clue what it does compile it and check it out ;) its pretty neat.

Edit: Mmmm Wtf is with the similies in my fricken code? Should that not be allowing in the [ code ] tags [ / code ] ?!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains — AI StorefrontUnstoppable Domains — AI Storefront
Polymorphism - such a nice name!

Nice script learnt a few things from it!

Cheers!

Joseph
 
0
•••
You probably mean Borland C++ Builder ;)
Nice tutorial anyway

Cheers,
Matt
 
0
•••
Olitt — high-converting AI websites, only from $1/moOlitt — high-converting AI websites, only from $1/mo

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover
URLs.com
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back