c++17(18)-重载I/O运算符
生活随笔
收集整理的這篇文章主要介紹了
c++17(18)-重载I/O运算符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
0,精通Python,62.37==>1
1,精通C++,49.21==>1
1,精通C++,49.21==>1
訪問索引超過范圍
精通C++ 49.21
Please the name: rust
Plrase the price: 52.15rust 52.15Hit any key to continue...
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;class Book
{
private:int _id;char* _name;float _price;static inline int nowid{0};int _bookCount{1};
public : Book(int id, char* name, float price): _id(id), _price(price){int nameLen = strlen(name);_name = new char[nameLen + 1];strcpy(_name, name);}Book(char* name, float price):Book(nowid,name,price){nowid++;}Book():Book(nowid,"",0.0){nowid++;}~Book(){delete[] _name;}friend const Book& operator +(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount+rightBook._bookCount;return *book;}else{return leftBook;}}friend const Book& operator -(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount-rightBook._bookCount;if ( book->_bookCount<0) book->_bookCount=0;return *book;}else{return leftBook;} } Book& operator -=(const Book& otherBook){if (*this==otherBook) {_bookCount-=otherBook._bookCount;if ( _bookCount<0) _bookCount=0;return *this;}else{return *this;} } Book& operator +=(const Book& otherBook){if (*this==otherBook) {_bookCount+=otherBook._bookCount;return *this;}else{return *this;} } friend const bool operator ==(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return true;}else{return false;} } friend const bool operator !=(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return false;}else{return true;} } friend ostream& operator<<(ostream& out, const Book& myBk){out<<myBk._name<<" "<<myBk._price;return out; } friend istream & operator>>(istream & is, Book& myBk){myBk._id=myBk.nowid;char name[100];cout << "Please the name: "; is>> name;cout << "Plrase the price: ";is>> myBk._price;cout << endl; int nameLen = strlen(name);myBk._name = new char[nameLen + 1];strcpy(myBk._name,name); return is;} const void print() const{cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;}void printCount(){cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl; }
};
class BookS{
private:vector<Book*> _myBooks;
public:void addBook(Book* book){_myBooks.push_back(book); }void print() {for(const Book *bk:_myBooks){bk->print();}}Book& operator[](int i){ return *(_myBooks.at(i));}};int main(int argc, char **argv)
{Book *book1=new Book("精通Python",62.37); Book *book2=new Book("精通C++",49.21); BookS myBks;myBks.addBook(book1); myBks.addBook(book2); myBks.print();try{ myBks[1].print();myBks[2].print(); }catch(std::out_of_range e){cout<<"訪問索引超過范圍"<<endl;}cout<<myBks[1]<<endl;Book book3;cin>>book3;cout<<book3<<endl;
}
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;class Book
{
private:int _id;char* _name;float _price;static inline int nowid{0};int _bookCount{1};
public : Book(int id, char* name, float price): _id(id), _price(price){int nameLen = strlen(name);_name = new char[nameLen + 1];strcpy(_name, name);}Book(char* name, float price):Book(nowid,name,price){nowid++;}Book():Book(nowid,"",0.0){nowid++;}~Book(){delete[] _name;}friend const Book& operator +(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount+rightBook._bookCount;return *book;}else{return leftBook;}}friend const Book& operator -(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount-rightBook._bookCount;if ( book->_bookCount<0) book->_bookCount=0;return *book;}else{return leftBook;} } Book& operator -=(const Book& otherBook){if (*this==otherBook) {_bookCount-=otherBook._bookCount;if ( _bookCount<0) _bookCount=0;return *this;}else{return *this;} } Book& operator +=(const Book& otherBook){if (*this==otherBook) {_bookCount+=otherBook._bookCount;return *this;}else{return *this;} } friend const bool operator ==(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return true;}else{return false;} } friend const bool operator !=(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return false;}else{return true;} } friend ostream& operator<<(ostream& out, const Book& myBk){out<<myBk._name<<" "<<myBk._price;return out; } friend istream & operator>>(istream & is, Book& myBk){myBk._id=myBk.nowid;char name[100];//cout << "Please the name: "; is>> name >> myBk._price;//cout << "Plrase the price: ";// is>> myBk._price;// cout << endl; int nameLen = strlen(name);myBk._name = new char[nameLen + 1];strcpy(myBk._name,name); return is;} const void print() const{cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;}void printCount(){cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl; }
};
class BookS{
private:vector<Book*> _myBooks;
public:void addBook(Book* book){_myBooks.push_back(book); }void print() {for(const Book *bk:_myBooks){bk->print();}}Book& operator[](int i){ return *(_myBooks.at(i));}};int main(int argc, char **argv)
{Book *book1=new Book("精通Python",62.37); Book *book2=new Book("精通C++",49.21); BookS myBks;myBks.addBook(book1); myBks.addBook(book2); myBks.print();try{ myBks[1].print();myBks[2].print(); }catch(std::out_of_range e){cout<<"訪問索引超過范圍"<<endl;}cout<<myBks[1]<<endl;Book book3;cin>>book3;cout<<book3<<endl;
}
0,精通Python,62.37==>1
1,精通C++,49.21==>1
1,精通C++,49.21==>1
訪問索引超過范圍
精通C++ 49.21
rust 12.44
rust 12.44Hit any key to continue...
總結
以上是生活随笔為你收集整理的c++17(18)-重载I/O运算符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 下如何查看mysql表单_Navicat
- 下一篇: ATen(A TENsor librar