Program to illustrates Container Classes


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class character
{
        char element;
    public :
        void set_char(char);
        void display_char();
};
class word                          // container class
{
        character w[5];         // contain array of objects of
        int length;                        // class 'character'
    public :
        void set_word(char*);
        int get_length();
        void display_word();
};
class line               // container class
{
        word l[6];            // contain array of objects of
                                // class 'word'
    public :
        void set_line(char**);
        void display_line();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'character'
void character::set_char(char a)
{
    element = a;
}
void character::display_char()
{
    cout << element;
}

// member function definitions for 'word'
void word::set_word(char* a)
{
    length = strlen(a);
    for (int i=0; i<length; i++)
        w[i].set_char(a[i]);
}
int word::get_length()
{
    return length;
}
void word::display_word()
{
    for (int i=0; i<length; i++)
        w[i].display_char();
    cout << " ";
}
// member function definitions for 'line'
void line::set_line(char** a)
{
    for (int i=0; i<6; i++)
        l[i].set_word(a[i]);
}
void line::display_line()
{
    cout << "The Line of text is :\n";
    for (int i=0; i<6; i++)
        l[i].display_word();
    cout << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    static char* a[6] = {"abc", "def", "ghij", "klmn", "op", "qr"};
    line L;
    L.set_line(a);
    L.display_line();
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|