c++17(13)-operator +

#include <iostream>
#include <cstring> 
using namespace std;

class Book
{
private:
    int _id;
    char* _name;
    float _price;
    static int nowid;
    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()
    {
        delete[] _name;
    }
    Book& operator +(const Book& rightBook){
        if (rightBook._id==_id) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=_bookCount+rightBook._bookCount;
            return *book;
        }
    }
    void print(){
        cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;
    }
};
int Book::nowid{0};//static在类外初始化
int main(int argc, char **argv)
{
   Book *book1=new Book("精通C++", 52.17);
   book1->print();
   *book1=*book1+*book1;
   book1->print();   
}
#include <iostream>
#include <cstring> 
using namespace std;

class Book
{
private:
    int _id;
    char* _name;
    float _price;
    static int nowid;
    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()
    {
        delete[] _name;
    }
    Book& operator +(const Book& rightBook){
        if (rightBook._id==this->_id) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=this->_bookCount+rightBook._bookCount;
            return *book;
        }
    }
    void print(){
        cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;
    }
};
int Book::nowid{0};//static在类外初始化
int main(int argc, char **argv)
{
   Book *book1=new Book("精通C++", 52.17);
   book1->print();
   *book1=*book1+*book1;
   book1->print();   
}
0,精通C++,52.17==>1
1,精通C++,52.17==>2

Hit any key to continue...

2个相同ID的书相加会增加数量,生成新的书。
也可通过friend

#include <iostream>
#include <cstring> 
using namespace std;

class Book
{
private:
    int _id;
    char* _name;
    float _price;
    static int nowid;
    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()
    {
        delete[] _name;
    }
    friend Book& operator +(const Book& leftBook,const Book& rightBook){
        if (rightBook._id==leftBook._id) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount+rightBook._bookCount;
            return *book;
        }
    }
    void print(){
        cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;
    }
};
int Book::nowid{0};//static在类外初始化
int main(int argc, char **argv)
{
   Book *book1=new Book("精通C++", 52.17);
   book1->print();
   *book1=*book1+*book1;
   book1->print();   
}
#include <iostream>
#include <cstring> 
using namespace std;

class Book
{
private:
    int _id;
    char* _name;
    float _price;
    static int nowid;
    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()
    {
        delete[] _name;
    }
    friend Book& operator +(const Book& leftBook,const Book& rightBook){
        if (strcmp (rightBook._name,leftBook._name) == 0) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount+rightBook._bookCount;
            return *book;
        }
    }
    void print(){
        cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;
    }
    void printCount(){
         cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl;       
    }
};
int Book::nowid{0};//static在类外初始化
int main(int argc, char **argv)
{
   Book *book1=new Book("精通C++", 52.17);
   Book *book2=new Book("精通C++", 52.17);  
   book1->print();
   auto book3=*book1+*book2;  
   book3.printCount();   
}
0,精通C++,52.17==>1
精通C++:52.17==>2

Hit any key to continue...
发布了385 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/AI_LX/article/details/104443335