classasn.cpp

来自「《C/C++程序员实用大全》配套代码,适用初学C++的人员。」· C++ 代码 · 共 45 行

CPP
45
字号
#include <iostream.h>
#include <string.h>


class BookStuff {
  public:
    BookStuff(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];
};

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

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


void main(void)
 {
   BookStuff big_book("Jamsa's C/C++ Programmer's Bible", "Jamsa Press", 
	   "Jamsa and Klander");
   
   char *title;
   title = big_book;
   cout << "The book's title is " << title << endl;
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?