Home > C++ > Filled dimon or UnFilled dimon, OOPs Technique..

Filled dimon or UnFilled dimon, OOPs Technique..

#include <iostream.h>

using namespace std;

class dimon
{
private:
    int size;
public:
    dimon()
    {
        size = 0;
    } 
    void setSize(int isize)
    {
        size = isize;
    }
    int getsize()
    {
        return size;
    }
    void pWhiteSpace(int num ) // draw the white space
    {
        for(int i=0; i<num ; i++)
        {
            cout << " " ;
        }        
    }
    void pStar(int num) // draw the star "*"
    {
        for(int i=0; i<num; i++)
        {
            cout << "*" ;
        }
    }
    
};

class FillDimon : public dimon
{
public:
    void show()
    {
        int size = getsize() ;
        int middle = size / 2;
        int j = middle ;
        for(int i=0; i < size ; i++)
        {
            if(i<=middle)
            {
                pWhiteSpace(j--);
                pStar(i);
                pStar(i+1);
            }
            else
            {
                pWhiteSpace(++j+1);
                pStar(size - i);
                pStar(size - i -1 );
            }
            cout << endl;
        }
    }
};

class UnFillDimon : public dimon
{
private:
    void pSW(int num)
    {
        int whitenum;
        if(num==1)
        {
            cout << "*";
        }
        else if(num >1 )
        {
            whitenum = num - 2;
            cout << "*";
            pWhiteSpace(whitenum);
            pWhiteSpace(whitenum + 1);
            cout << "*";
        }
    }
public:
    void show()
    {
        int size = getsize();
        int middle = size/2;
        int j = middle;
        for(int i = 0; i<size; i++)
        {
            if(i <= middle)
            {
                pWhiteSpace(j--);
                pSW(i+1);
            }
            else
            {
                pWhiteSpace(++j+1);
                pSW(size-i);            
            }
            cout << endl;
        }
    }
};

int main(int argc, char** argv) {
    int ch;
    FillDimon mydimon;
    UnFillDimon myudimon;
    cout << " Enter number 1 or 2 to display Filled Dimon or UnFilled Dimon :" << endl;
    cout << " >> " ;
    cin >> ch ;
    if ( ch == 1 )
    {
        mydimon.setSize(25);
        mydimon.show();
    }
    else
    {
        myudimon.setSize(25);
        myudimon.show();
    }
    
    return (EXIT_SUCCESS);
}

This is another version of Filled / Unfilled Dimon , using OOP.
and try to use inheritance , base class , derive class. . . . have fun

Categories: C++
  1. steerapong
    July 10, 2008 at 6:14 pm

    Yeb , good code 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: