'C++ Unable to print Getter Function
I am currently having trouble with my C++ program. The program is supposed to print out the inputted value and have a setter set the value into a setter method. After that, it is supposed to print out all the setter values using the getter method. But in this coding, the printing of the getter method is always printing empty. I assume that the getter method is unable to retrieve the information from the setter. How do I overcome this problem?
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstring>
using namespace std;
const int SIZE = 3;
class bookDS
{
private:
std::string authorName;
std::string bookName;
std::string bookFormat;
public:
void viewBook();
bookDS()
{
authorName = "";
bookName = "";
bookFormat = "";
}
bookDS(const char authorName[SIZE], const char bookName[SIZE], const char bookFormat[SIZE])
{
this->authorName = authorName[SIZE];
this->bookName = bookName[SIZE];
this->bookFormat = bookFormat[SIZE];
}
void setAuthorName(string authorName)
{
this->authorName = authorName;
}
void setBookName(string bookName)
{
this->bookName = bookName;
}
void setBookFormat(string bookFormat)
{
this->bookFormat = bookFormat;
}
string getAuthorName() const
{
return authorName;
}
string getBookName()
{
return bookName;
}
string getBookFormat()
{
return bookFormat;
}
};
Setter and Getter:
class bookGenre : public bookDS
{
private:
std::string genreBook;
public:
bookGenre()
{
genreBook = "";
}
bookGenre(string genreBook)
{
this->genreBook = genreBook;
}
void setGenreBook(string genreBook)
{
this->genreBook = genreBook;
}
string getGenreBook()
{
return genreBook;
}
};
void bookDS::viewBook()
{
bookGenre ds[10];
for (int i = 0; i < 3; i++)
{
cout << "your name is" << ds[i].getAuthorName() << endl;
cout << "your book is" << ds[i].getBookName() << endl;
cout << "your format is" << ds[i].getBookFormat() << endl;
cout << "your genre is" << ds[i].getGenreBook() << endl;
cout << "\n" << endl;
}
}
Main:
int main()
{
bookDS driver;
bookGenre ds[10];
string name, book, format, genre;
for (int i = 0; i < 3; i++)
{
cout << "Enter name " << endl;
getline(cin, name);
ds[i].setAuthorName(name);
fflush(stdin);
cout << "Enter book " << endl;
getline(cin, book);
ds[i].setBookName(book);
fflush(stdin);
cout << "Enter format " << endl;
getline(cin, format);
ds[i].setBookFormat(format);
fflush(stdin);
cout << "Enter genre " << endl;
getline(cin, genre);
ds[i].setGenreBook(genre);
fflush(stdin);
}
driver.viewBook();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
