Operator pointer : char array string « String « C++






Operator pointer

   
#include <iostream>
#include <string.h>
using namespace std;
class Book {
  public:
    Book(char *title, char *publisher, char *author);
    void show_book(void) { 
    cout << "Book: " << title << " by " <<
        author << " Publisher: " << publisher << endl; 
  };
    operator char *();
   
  private:
    char title[64];
    char author[64];
    char publisher[64];
};

Book::Book(char *title, char *publisher, char *author)
{
   strcpy(Book::title, title);
   strcpy(Book::publisher, publisher);
   strcpy(Book::author, author);
}

Book::operator char *(void)
{
   char *ptr = new char[256]; 
   
   return(strcpy(ptr, title)); 
}


int main(void)
{
   Book myBook("A", "B","C");
   
   char *title;
   title = myBook;
   cout << "The book's title is " << title << endl;
}
  
    
    
  








Related examples in the same category

1.Demonstrate the basic null-terminated string functions.
2.Count spaces, punctuation, digits, and letters.
3.Convert char array to upper case
4.Using strcpy() to assign value from one char array to another char array
5.Using strncpy() to assign one char array to another char array
6.Using strcpy and string terminator
7.Using strncpy() and string terminator
8.Get the string length
9.Using strcat() and strncat().
10.Using atoi() function
11.Filling an Array
12.Use strlen() to get the length of a char array buffer