Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Help with creating a derived c++ class (http://www.go4expert.com/forums/help-creating-derived-cpp-class-t13160/)

edouard89 22Aug2008 23:56

Help with creating a derived c++ class
 
Hello everyone! I am need of assistance with creating a derived class. I have the following abstract base class:
Code: c++

class Field{
    public:
    virtual void display() = 0;
    virtual int edit() = 0;
    virtual bool editable() const = 0;
    virtual void *data() = 0;
    virtual Field *clone() const = 0;
};

I need to make a derived class with the following specification:
Quote:

Your LField class is derived from your Field class and represents a line field.

Upon instantiation, an LField object receives between three and five values, in the following order:

* required:
o an int holding the row number on which the line field is positioned,
o an int holding the column number at which the line field begins,
o an int holding the width of the line field,
* optional:
o a char string holding the initial value of the line field, and
o a bool holding the editability flag for the line field: true for editable, false for not editable.

Your design does not impose any limit on the width of the line field.

If the calling function specifies the first three parameters only, your LField object holds an empty string and is editable. If the calling function specifies the first four parameters only, your object is editable.

If the calling function specifies invalid coordinates, your object uses the closest on-screen coordinates to those specified. If the width is too long for the line field to fit on a screen, your object reduces the width so that the contracted field does fit on the screen. If the initial value occupies more than the field width, your object uses only as many characters of the initial value as will fit within the specified, and if necessary, contracted width.
This is how I have my derived class in my header file:
Code: c++

class LField : public Field{
    int row_loc, col_loc, width_tot, index;
    char *initval;
    bool edib;
   
    public:
        LField(int row, int col, int width);
        LField(int row, int col, int width, char *s);
        LField(int row, int col, int width, char *s, bool editable);
        void initialize(int r, int c, int w, bool e);
        ~LField();
        void display();
        int edit();
        bool editable() const;
        void *data();
        Field *clone() const;
        LField& operator = (const LField & rhs);
        LField(const LField & source );
}


This is what I have done so far:
Code: c++

LField::LField(int row, int col, int width){
    //Checks for valid coords
    if(row < 0){
        row = 0;
    }
    if(row > dtioRows()){
        row = dtioRows() - 1;
    }
    if(col < 0){
        col = 0;
    }
    if(col > dtioColumns()){
        col = dtioColumns() - 1;
    }
    //Checks for width and location
    if((col + width) > dtioColumns()){
        width = width - ((col + width) - dtioColumns());
    }
   
    initialize(row, col, width, true);
    initval = NULL;
}
LField::LField(int row, int col, int width, char *s){
    //Checks for valid coords
    if(row < 0){
        row = 0;
    }
    if(row > dtioRows()){
        row = dtioRows() - 1;
    }
    if(col < 0){
        col = 0;
    }
    if(col > dtioColumns()){
        col = dtioColumns() - 1;
    }
    //Checks for width and location
    if((col + width) > dtioColumns()){
        width = width - ((col + width) - dtioColumns());
    }
    initialize(row, col, width, true);
    if(strlen(s) > width){
        initval = new char[width];
        for(int i = 0; i < width; i++){
            initval[i] = s[i];
        }
    }
    else{
        initval = new char[strlen(s) + 1];
        strcpy(initval, s);
    }
   
}
LField::LField(int row, int col, int width, char *s, bool editable){
    //Checks for valid coords
    if(row < 0){
        row = 0;
    }
    if(row > dtioRows()){
        row = dtioRows() - 1;
    }
    if(col < 0){
        col = 0;
    }
    if(col > dtioColumns()){
        col = dtioColumns() - 1;
    }
    //Checks for width and location
    if((col + width) > dtioColumns()){
        width = width - ((col + width) - dtioColumns());
    }
    initialize(row, col, width, editable);
    if(strlen(s) > width){
        initval = new char[width];
        for(int i = 0; i < width; i++){
            initval[i] = s[i];
        }
    }
    else{
        initval = new char[strlen(s) + 1];
        strcpy(initval, s);
    }
}
//Function that initializes LField
void LField::initialize(int r, int c, int w, bool e){
    row_loc = r;
    col_loc = c;
    width_tot = w;
    edib = e;
    index = 0;
}

I am unsure if what I am doing is correct and would like some input. If there is something that has to be changed or corrected please tell me. Any input is greatly appreciated!

asadullah.ansari 9Feb2009 14:06

Re: Help with creating a derived c++ class
 
it's okay... If any problem or specific features you wanna then post exact point? beacuse here there is no team for review your code without giving any proper reason/problem/code defects/ features...


All times are GMT +5.5. The time now is 15:16.