Program Dynamic Construction of Objects


#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
class string
{
        char *s;
        int length;
        public:
        string();
        string(char*);
        void concat(string&);
        void print_string();
};
string::string()
{
    length = 0;
    s = new char[length + 1];

    s[0] = '\0';             // empty string
}
string::string(char *str)
{
    length = strlen(str);
    s = new char[length + 1];
    strcpy(s,str);
}
void string::concat(string& instring)
{
    length = length + instring.length;
    strcat(s,instring.s);
}
void string::print_string()
{
    cout.width(12);
    cout << s;
    cout.width(11);
    cout << "Length = " << length << "\n";
}
void main(void)
{
    clrscr();
    string s1("Good "),s2("Morning"),s3;

    s3.concat(s1);
    s3.concat(s2);

    s1.print_string();
    s2.print_string();
    s3.print_string();
    getch();
}

0 comments:

Post a Comment

 
 
 
 


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