I'm having an issue that so that Book A and Book B's Author and Title will print at the end of the program. So This will verify that the overloaded = is functioning.

#include <iostream>
using namespace std;

class Book
{
   public:
      Book(char*, char*);
   private:
      char* title;
      char* author;

   public:
          Book& Book::operator=(const Book& b)
          {
                if(this != &b)
                {
                        delete [] author;
                        delete [] title;

                        author = new char[strlen(b.author)+1];
                        title = new char[strlen(b.title)+1];
                        strcpy(author, b.author);
                        strcpy(title, b.title);
                }
                return *this;
          }

};

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

  pA = pB;

  //complete the program so that Book A and Book B's Author and Title print
  cout << "The value of Book A's Author and Title are: " << pA.author<<" "<<pA.title<<endl;
  cout << "The value of Book B's Author and Title are: " << pB.author<<" "<<pB.title<<endl;

  system("pause");
  return 0;
}

Recommended Answers

All 8 Replies

pA and pB are both pointers to objects, not objects per se. Therefore use the arrow operatos to acess the data members, not the dot operator.

I tried that and ended up with a compiler error and I still don't know what I did wrong

I tried that and ended up with a compiler error and I still don't know what I did wrong

We don't either until you show what you tried. Lerner is correct. You need the arrow operator, not the dot operator. Currently you have the dot operator. Use code tags:

[code]

// paste code here

[/code]

The issue is with these two snippets of code:_______________________________________________________________________________________________________
cout << "The value of Book A's Author and Title are: " << pA.author<<" "<<pA.title<<endl;
cout << "The value of Book B's Author and Title are: " << pB.author<<" "<<pB.title<<endl;

The issue is with these two snippets of code:_______________________________________________________________________________________________________
cout << "The value of Book A's Author and Title are: " << pA.author<<" "<<pA.title<<endl;
cout << "The value of Book B's Author and Title are: " << pB.author<<" "<<pB.title<<endl;

Code tags please:

[code]
// paste code here
[/code]

Use the arrow operator as Lerner mentioned:

cout << "The value of Book A's Author and Title are: " << pA->author<<" "<<pA->title<<endl;

Same with the other line. Use the -> operator with pointers.

that worked but I still am having an issue with the a compiling error
for char* author;

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

That syntax requires definition (which I don't see in the code posted) as well as declaraton (which you arlready have) of a nondefault constructor taking two char * as arguments. It will probably look a fair amount like the code for the assignment operator thay you have already written.

the program has many pros,
1. constructor has no definition.
2. in the cout u r trying to access the private variables directly

write a contructor and a public function to access display the private data

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.