求C++大神给看看,是设计一个类的构造函数和析构函数

2024-11-28 13:55:30
推荐回答(2个)
回答1:

/*        book.h        */
#ifndef __BOOK_H__
#define __BOOK_H__
#include 
using namespace std;
class book
{
public:
    book();
    book(string,string,unsigned,double);
    book(string,string);
    ~book();
private:
    string    bookName;
    string    author;
    unsigned    page;
    double    price;
};
#endif//__BOOK_H__
/*-----------------------------------------*/
/*        book.cpp        */
#include "book.h"
book::book()
{
    cout<<"对象已构造"<}
book::book(string bookName,string author,unsigned page,double price)
{
    this->bookName = bookName;
    this->author = author;
    this->page = page;
    this->price = price;
}
book::book(string bookName,string author)
{
    this->bookName = bookName;
    this->author =author;
}
book::~book()
{
    cout<<"对象已析构"<}
/*-----------------------------------------*/
/*        test.cpp        */
#include "book.h"
int main()
{
    book b1("语文","qwe",200,66.55);
    book b2("数学","rte",100,213.1);
    book b3;
    return 0;
}
/*-----------------------------------------*/

回答2:

恩恩,好。擅长做。。给你帮助.