Tuesday, February 16, 2010

C++ Class example

Basic Class C++ example code

// example one
#include
#include

class Book
{
// by default, all data and function are private

int PageCount;
int CurrentPage;
public:
Book( int Numpages) ; // Constructor
~Book(){} ; // Destructor
void SetPage( int PageNumber) ;
int GetCurrentPage( void ) ;
};

Book::Book( int NumPages) {
PageCount = NumPages;
}



void Book::SetPage( int PageNumber) {
CurrentPage=PageNumber;
}

int Book::GetCurrentPage( void ) {
return CurrentPage;
}

int main() {
Book ABook(128) ;   // Call constructor() and set PageNum=128
ABook.SetPage( 56 ) ;  // Set the current page number
std::cout << "Current Page " << ABook.GetCurrentPage() << std::endl;    // print out the current page
return 0;    // call destructor
}



This example will demonstrate inheritance. This is a two class  application  with one class derived from another.

    #include
    #include

    class Point
    {
   // default private
    int x,y;
    public:
    Point(int atx,int aty ) ; // Constructor
    inline virtual ~Point() ; // Destructor
    virtual void Draw() ;
    };




    class Circle : public Point {
    int radius;
    public:
    Circle(int atx,int aty,int theRadius) ;
    inline virtual ~Circle() ;
    virtual void Draw() ;
    };


    Point ::Point(int atx,int aty) {
    x = atx;
    y = aty;
    }

    inline Point::~Point ( void ) {
    std::cout << "Point Destructor called\n";
    }

    void Point::Draw( void ) {
    std::cout << "Point::Draw point at " << x << " " << y << std::endl;
    }


    Circle::Circle(int atx,int aty,int theRadius) : Point(atx,aty) {
    radius = theRadius;
    }

    inline Circle::~Circle() {
    std::cout << "Circle Destructor called" << std::endl;
    }

    void Circle::Draw( void ) {
    Point::Draw() ;
    std::cout << "circle::Draw point " << " Radius "<< radius << std::endl;
    }

    int main() {
    Circle ACircle(10,10,5) ;
    ACircle.Draw() ;
    return 0;
    }

What is Polymorphism?

Polymorphism is a generic term that means 'many shapes'. In C++ the simplest form of Polymorphism is overloading of functions, for instance several functions called SortArray( arraytype ) where sortarray might be an array  of ints, or doubles. 

/* 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
#include "circle.h"
#include "square.h"

main () {

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

        for (int k=0; k
                jigsaw[k] = new Square();
                jigsaw[k+1] = new Circle();
        }
        for (int k=0; k
                jigsaw[k]->setsize(10+k);
                }
        for (int k=0; k
                float syze = jigsaw[k]->getarea();
                cout << "this is sq " << syze << endl;
                }

        return (0);
        }

No comments:

Post a Comment

Search This Blog