CLASSES WITH POINTERS AS DATA MEMBERS - STRING CLASS


SOURCE CODE:
#include<iostream.h>
#include<string.h>
class string
{
    char *name;
    int length;
    public:
       string()
      {
         length=0;
         name=new char[length+1];
      }
       string(char *s)
      {
         length=strlen(s);
         name=new char[length+1];
         strcpy(name,s);
      }
       void display()
      {
         cout<<name<<endl;
      }
       void join(string &a,string &b);
};
void string::join(string &a,string &b)
{
    length=a.length+b.length;
    delete name;
    name=new char[length+1];
    strcpy(name,a.name);
    strcat(name,b.name);
}
void main()
{
  char *first="JEBASTIN";
  string   name1(first),name2("MADURAVOYAL"),name3("CHENNAI"),s1,s2;
  s1.join(name1,name2);
  s2.join(s1,name3);
  name1.display();
  name2.display();
  name3.display();
  s1.display();
  s2.display();
  }

OUTPUT :

JEBASTIN
MADURAVOYAL
CHENNAI
JEBASTINMADURAVOYAL
JEBASTIN MADURAVOYALCHENNAI
Previous
Next Post »

Still not found what you are looking for? Try again here.