Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
Polymorphism - Sample tutorials       Share
2009-12-04 |  RatheeshTR  | Viewed: 285  |    1

 
/*  A  base  class  of  Shape  gives  rise  to
two  derived  classes  -  Circle  and  Square.

An  application  called  Polygon  sets  up  an
array  of  Circles  and  Squares,  and  uses  a
getarea  method  to  find  the  area  of  each.

This  getarea  method  is  defined  as  a  virtual
method  in  the  base  class,  so  that  an  array
of  shapes  CAN  be  defined  on  which  two
different  getarea  methods  can  be  run
depending  on  which  type  of  shape  is  the
current  one  at  the  time.  */


::::::::::::::
shape.h
::::::::::::::

#ifndef SHAPE
#define SHAPE 1

class Shape {

        public:

                
void setsize(int owbig);
                virtual float getarea() {};

        protected:

                
int givensize;
        };


#endif
::::::::::::::

shape.cpp

::::::::::::::

#include "shape.h"

void Shape::setsize(int sv) {
        givensize = sv;

        }


::::::::::::::

square.h

::::::::::::::

#include "shape.h"

class Square: public Shape {

        public:

                float getarea();

        };

::::::::::::::

square.cpp

::::::::::::::

#include "square.h"

float Square::getarea() {

        float result = givensize * givensize;

        return (result);

        }

::::::::::::::

circle.h

::::::::::::::

#include "shape.h"

class Circle: public Shape {

        public:

                float getarea();

        };

::::::::::::::

circle.cpp

::::::::::::::

#include "circle.h"

float Circle::getarea() {

        float result = 3.14159265f * givensize;

        return result;

        }

::::::::::::::

polygon.cpp

::::::::::::::

#include <iostream.h>
#include "circle.h"
#include "square.h"

main () {


        
int np = 10;
        Shape *jigsaw[np];


        
for (int k=0; k<np; k+=2) {
                jigsaw[k] = new Square();

                jigsaw[k+1] = new Circle();

        }

        
for (int k=0; k<np; k++) {
                jigsaw[k]->setsize(10+k);

                }

        
for (int k=0; k<np; k++) {
                float syze = jigsaw[k]->getarea();

                cout << "this is sq " << syze << endl;

                }


        return (0);

        }


Reference:

//http://www.wellho.net/resources/ex.php4?item=c234/PMdemo


Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
Cpp - Access a Class Member Function Without Creating a Class Object
Cpp - Writing Macro Definition
Cpp - using namespace std
Cpp - Using enum
Cpp - Pointers in C++
Cpp - How to use DLL file in the C++ Program
Cpp - creating and Linking DLL Files to C++ program
Cpp - 1) vector method and iterator (simple Example)
Cpp - 2) Sort and Binary search
Cpp - 3) Vector and Copy vector
Cpp - 4) for_each ( Vector )
Cpp - 5) List and Iterator
Cpp - 6) list and Vector example.2
Cpp - 7) example"fill, copy, list" statement
Cpp - 8) list and set

Related References
cpp - operator overloading
cpp - default arguments
cpp - c++ interview tips
cpp - virtual destructors
cpp - c++ destructors
cpp - csc interview questions
object oriented programming paradigm
elements of object oriented programming
basic concepts of oops and structure of c++ program
exception (runtime error) handling - cpp
cpp function pointers
cpp - access specifiers between base and derived classes
cpp - inautix interview questions
comparing popular programming languages

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us