简体   繁体   中英

how to work with classes

I have been learning on classes in c++ and I got a certain code from an old russian book its about a book class, I tried modifying it and running it its not working may some help me understanding why the authour used this code(what does strdup do?)

Author = strdup(autho);

inside the constructor and was wrong with this line of code

Book s("edgar", "science", "chemistry for dummies", "502","12.11.13","1.12.96");

Anyone with a simple straight explanation?

Main code below

using namespace std;

class Book{

    char * Author;
    char * Type;
    char * Title;
    int * Pages;
    unsigned int * Yearpublished;
    unsigned int  * Publishing;

    Book(char * autho, char * type, char * title,   int * pages, unsigned int * yearpublished, unsigned int  * publishing ){

        Author = strdup(autho);
        Type = strdup(type);
        Title = strdup(title);
        Pages = pages;
        Yearpublished = yearpublished;
        Publishing = publishing;

    }

    ~Book(){

        if(Author != NULL){

             free(Author);

        }
        if(Type != NULL){

            free(Type);
        }

        if(Title != NULL){

            free(Title);
        }
    }

};

int main(){

    cout << "main start" << endl;

    Book s("edgar", "science", "chemistry for dummies", "502","12.11.13","1.12.96");

    cout << "main finish" << endl;
return 0;
}

There are many, many things wrong with the code posted. So many things that virtually every single line is in error.

One of the most obvious is that you're trying to store a year as an int * , and then passing in a string containing "12.11.13" . That doesn't work. You're doing the same for pages ; accepting an int* and passing in a string containing an int. You can't do that, that isn't how pointers work. Most of your usage of pointers indicates that you don't really know what the * does, and you should stop and read about pointers before you introduce some very difficult to track down bugs into your code. You should either store your year as a string (very bad idea) or store it as an integer in unix time , which is pretty standard.

You should remove using namespace std and replace it with #include <string> . You should throw out all the lines that start with char* , and replace them with std::string and throw out your int* lines and make them int .

You're also declaring a private constructor and destructor. You need to add public: after the member variable declarations but before Book() . Then you should throw out the body of your constructor and use initializer lists.

You're also not including <iostream> , so your cout calls are probably causing errors.

Once you've done the above, you should remove the destructor ~Book() completely.

For example:

class Book{

    std::string Author;
    // ...
    int Pages;
    // ...

    public:

    Book(std::string author, /* ... */ int pages /* ... */)
    : Author(author), Pages(pages) {

    }
};

There are many errors there.

1) You are using way too many pointers. You do not need even a single pointer in this snippet of code. As pointed by meagar, you can replace char* by std::string . You do not need pointer to integers or unsigned integers, you can use the type as-is.

2) You are passing strings of characters (denoted by the use of quotes "") instead of numbers when calling the constructor. If you want to pass numbers, do not use quotes.

3) You are using "using namespace std" which is wrong for many reasons and has been explained so many times on so many websites that I will let you search why.

4) You are using free() on memory that you have not allocated with malloc() (and that would not even be suggested in C++ since we have new and delete ).

Edit: If this is truly code you have taken from a book on C++, then please use this book as fire starter because it's not worth more than that.

Here is a much simpler version of your code that is written in C++.

#include <iostream>
#include <string>

class Book
{
private:
    std::string Author, Type, Title, Publishing;
    unsigned int Pages, Yearpublished;

public:
    Book(const char* autho, const char* type, const char* title, unsigned int pages,
        unsigned int yearpublished, std::string publishing )
    {
        Author = strdup(autho);
        Type = strdup(type);
        Title = strdup(title);
        Pages = pages;
        Yearpublished = yearpublished;
        Publishing = publishing;
    }

    ~Book()
    {

    }

};

int main()
{
    std::cout << "main start" << std::endl;
    Book s("edgar", "science", "chemistry for dummies", 502, 2012,"1.12.96");
    std::cout << "main finish" << std::endl;

    return 0;
}

There is still much to improve but this will at least compile and run just fine.

I'm a bit confused of your question but i think you mainly ask for what is strdup :

It copies passed string and returns a pointer to jsut newly created one. And in destructor you destroy these allocated strings, if any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM