#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.