#include <iostream>

class Book
{
    public:
        Book(char* char*);
    private:
        char* title;    // dynamic pointer to char array
        char* author;   // dynamic pointer to char array
    public:
        Book& Book::operator=(const Book& b)
        {
            if(this != &p)   // make sure it's not the same object
            {
                delete [] author;   // delete author memory
                delete [] title;    // delete title memory

                author = new char[strlen(b.author) + 1];    // create new author
                title = new char[strlen(b.title) + 1];      // crreate new title

                strcpy(author, b.author);   // copy author data
                strcyp(title, b.title);     // copy title data
            }

            return *this;    // return reference for multiple assignments
        }
};

int main()
{
    Book* pA = new Book("Aardvark", "Be an Aardvark on pennies a day");
    Book* pB = new Book("Speelburgh", "ET vs. Howard the Duck");
    pA = pB;

    return 0;
}

In the code above let's start with the declorations in the main function. What is Book* pA = new Book Is that creating a pointer named pA that points to an object that has no variable name, only a place in memory and only accessible through the pointer variable pA? That's the only sence I can make out of this line. I've never used the keyword new before and once again my book had decided that I don't need an explanation. What about his line -> Book& Book::operator=(const Book& b) When I read a chapter on operator overloading I had a line that looked similar -> Rectangle operator+ (const Rectangle& p1) but there we didn't use the :: operator, what's the difference? And what the hell is return *this. I'm starting to hate the idea of OOP.

Recommended Answers

All 3 Replies

What book are you using if I might ask. It might just be that you need a better book.

Book* pA = new Book("Aardvark", "Be an Aardvark on pennies a day");

That piece of code says make a variable called pA that is a pointer to a book object and give the pointer the address of a new book object created it the parameters "Aardvark" and "Be an Aardvark on pennies a day"

Are you sure you copied the class correctly? Book& Book::operator=(const Book& b) makes no sense to me inside a class. Inside the class it should just be
Book& operator=(const Book& b).

:: is the scope resolution operator and you can see some examples of how it works on the MSDN site.

I'm starting to hate the idea of OOP.

OOP is a way of thinking about and structuring code. Your problems above are all with syntax.

OOP is a means to help you think about the things that are going to "live" in your programs - their properties and behaviors, as well as how they are related to other things (classes). For example, a class "Automobile" might have some properties such as Engine (probably another class with its own properties and behaviors), Suspension, NumberOfSeats, NumberOfDoors, Transmission, etc. It may be a kind-of MovingVehicle which would have some fundamental properties that all things that are a kind-of MovingVehicle would have, such as number of wheels (a Bicycle would have 2, Automobile 3 or 4, Truck 4 or more, etc). Behaviors could be accelerate, decelerate, stop, turn, openDoor, openTrunk, and such. There are also state properties, such as CurrentSpeed, which some of the behaviors (methods) could check, such as openTrunk, which may throw an exception or set an error if the CurrentSpeed <> 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.