main.cpp
#include <iostream> #include<vector> #include"Book.h" using namespace std; int main() { vector<Book> books; string cx,cv; float pl; while(cin>>cx>>cv>>pl) { Book a(cx,cv,pl); books.push_back(a); } for(int i=0;i<books.size();i++) { books[i].print(); } return 0; }
book.h
#ifndef BOOK_H_INCLUDED #define BOOK_H_INCLUDED #include<iostream> #include<string> #include<cstring> using namespace std; class Book { public: Book(string a,string c,float v); void print(); private: string isbn; string title; double price; }; #endif // BOOK_H_INCLUDED
book.cpp
#include "Book.h" Book::Book(string a,string b,float c):isbn(a),title(b),price(c){} void Book::print() { cout<<"出版编号:"<<isbn<<" "<<"书名:"<<title<<" "<<"定价:"<<price<<" RMB"<<endl; }
原文:https://www.cnblogs.com/haruna/p/9080089.html