| | |
Can't find the solution and looking for help
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 9
Reputation:
Solved Threads: 0
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;
}
----------------------------------------------------------------------------------------------------------
#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;
}
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
I tried that and ended up with a compiler error and I still don't know what I did wrong
[code]
// paste code here
[/code]
•
•
Join Date: Sep 2008
Posts: 9
Reputation:
Solved Threads: 0
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;
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;
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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]
// 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. •
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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.
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.
Klatu Barada Nikto
![]() |
Similar Threads
- Please help me to find a solution. (Networking Hardware Configuration)
- BUG in Internet Explorer. Please help find a solution! (HTML and CSS)
- Cannot find server: msg, HELP (Web Browsers)
- cannot find a solution (Viruses, Spyware and other Nasties)
- Need to find a solution similar to Yahoo News related search (JavaScript / DHTML / AJAX)
- internet explorer"cannot find server" (Web Browsers)
- IE won't let me access threads. Could someone email me a solution please? (Web Browsers)
- Cannot Find Server. (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: c++ for c# developers
- Next Thread: Question about a loop freezing, or stopping
Views: 505 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






