0

Error in a code regarding classes

This is the question: Write a class Book with three data members BookID, Pages and Price. It also contains the following member function: The get( ) function is used to input values. The show( ) function is used to display values. The set( ) function is used to set the values of data members using parameters. The program should create two objects of the class and input values for these objects. The program should then displays the details of both books. Here is a code i wrote to display book id no of pages and price of a book: my .h file #pragma once #include<string> using namespace std; class Book { private: string BookID; int Pages; int Price; public: Book(void); Book(string x,int y,int z); void get(); void show(); void set(string x,int y,int z); }; my .cpp file #include "Book.h" #include<iostream> #include<string> using namespace std; string a; int b,c; Book::Book() { cout<<"Welcome to the book directory \n"; get(); } void Book::get(){ cout<<"Please enter the requested information..."<<endl; cout<<"Enter the book ID:"<<endl; cin>>a; cout<<"Enter the number of pages"<<endl; cin>>b; cout<<"Enter the price"<<endl; cin>>c; set (a,b,c); } Book::Book(string x, int y, int z) { cout<<"Constructing parameters of the book \n"<<endl; x=a;y=b;z=c; } void Book::set(string x,int y,int z){ BookID=x; Pages=y; Price=z; } void Book::show() { cout<<"The book ID is:"<<BookID<<endl; cout<<"The number of pages is:"<<Pages<<endl; cout<<"The price of book is Rs."<<Price<<endl; } my main.cpp file #include<iostream> #include"Book.h" using namespace std; int main() { string x; int y,z; Book b1; Book b2(x,y,z); b1.set(x, y, z); b1.show(); return 0; } This gives errors.Keep in mind that i am restricted to use the .h file.Any help would be very apprecaited.Thanks!I have been stuck here for 2 days with little progress c++classes

13th Dec 2020, 9:49 AM
Dex
Dex - avatar
1 Answer
0
the warning was issued in main.cpp, when you used uninitialized values of y, z, and your x(string), was only an empty string. Otherwise, it runs okay. I think your Book class should not be responsible for getting the input from the stdin, rather use a secondary input class or get the input in the main.cpp and pass those values to your setter or constructor. here is your code that runs okay. https://code.sololearn.com/cA201a9A244a
13th Dec 2020, 1:28 PM
Flash