prog5.cpp

来自「C++语言程序设计题典」· C++ 代码 · 共 50 行

CPP
50
字号
#include <iostream.h>
#include <string.h>
class Book
{
	char *title;      //书名
	char *author;     //作者
    int numsold;      //月销售量
public:
	Book() {}
	Book(const char *str1,const char *str2,const int num)
	{
		int len=strlen(str1);
		title=new char[len+1];
		strcpy(title,str1);
		len=strlen(str2);
		author=new char[len+1];
		strcpy(author,str2);
		numsold=num;
	}
	void setbook(const char *str1,const char *str2,const int num)
	{
		int len=strlen(str1);
		title=new char[len+1];
		strcpy(title,str1);
		len=strlen(str2);
		author=new char[len+1];
		strcpy(author,str2);
		numsold=num;
	}
	~Book()
	{
		delete title;
		delete author;
	}
	void print(ostream& output)    //输出流引用作为参数
	{
		output << "输出数据" << endl;
		output << "  书名:" << title << endl;
		output << "  作者:" << author << endl;
		output << "  月销售量:" << numsold << endl;
	}
};
void main()
{
	Book obj1("C语言程序设计","潭浩强",800),obj2;
	obj1.print(cout);
	obj2.setbook("C++语言程序设计","李春葆",300);
	obj2.print(cout);
}

⌨️ 快捷键说明

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