C++ Programming Blog

 
 
 
//Conversion BASIC TO CLASS TYPE
//string to class object

#include <iostream.h>
#include <conio.h>
#include <string.h>

class string
{
  char *p;
  int len;
  public:
    string::string()
    {}
    string::string(char *a)
    {
      len=strlen(a);
      p=new char[len+1];
      strcpy(p,a);
    }
    void display()
    {
      cout<<p;
    }
};

void main()
{
 clrscr();
 string s1,s2;
 char *name1=\"vivek\";
 char *name2=\"patel\";
 s1=string(name1);       //Invoke constructor
 s2=name2;               //Invoke constructor

 s1.display();
 cout<<endl;
 s2.display();
 getch();
 }

 
 
Didn't find what you were looking for? Find more on Program for converting string to class object