*** C++ Programming Language . *** Learn how to create a class that can be aggre
ID: 3838811 • Letter: #
Question
*** C++ Programming Language. ***
Learn how to create a class that can be aggregated to another class.
Create an object that accesses methods in another class.
Pass data to, and retrieve data from, aggregated objects created through composition.
Instructions:
Read the problem,
Create a program that collects book titles for a book seller. Prompt the user for the title of the book, the author, the publisher and the ISBN number. The ISBN number must 13 digits. After data is entered the program should display the title, author, publisher and ISBN.
----------------------------------------------------------------------
----------------------------------------------------------------------
#1) The main() function should do nothing but create an object of the primary class type and call it's methods.
ISBN should be in an aggregate class.
#2) Write code that solves the problem described in #1.
Pause the program so that the console does not disappear until a key is pressed.
Insure that you properly document your code.
Assignment CompositionCreate a project that uses am aggregate class. Purpose
Learn how to create a class that can be aggregated to another class.
Create an object that accesses methods in another class.
Pass data to, and retrieve data from, aggregated objects created through composition.
Explanation / Answer
#include<iostream>
using namespace std;
#include<cstring>
class Book
{
//variables
private:
char title[30];
long long int ISBN;
char author[30];
char publisher[30];
public:
//set and get methods
void setTitle(char title[30])
{
strcpy(this->title,title);
}
void setISBN(long long int ISBN)
{
this->ISBN = ISBN;
}
void setAuthor(char author[30])
{
strcpy(this->author,author);
}
void setPublisher(char publisher[30])
{
strcpy(this->publisher,publisher);
}
char* getTitle()
{
return title;
}
int getISBN()
{
return ISBN;
}
char* getPublisher()
{
return publisher;
}
char* getAuthor()
{
return author;
}
};
//display method
void displayBook(Book b)
{
cout<<" Book name : "<<b.getTitle()<<" ISBN :"<<b.getISBN()<<" Author : "<<b.getAuthor()<<" Publisher: "<<b.getPublisher();
}
int main()
{
Book b;
char title[30],author[30],publisher[30];
int i;
long long int isbn;
//enter book data
cout<<" Enter ISBN : ";
cin>>isbn;
b.setISBN(isbn);
cin.ignore(); //to take care of character arrays
cout<<" Enter book title : ";
cin.getline(title,sizeof(title));
b.setTitle(title);
cout<<" Enter publisher : ";
cin.getline(publisher,sizeof(publisher));
b.setPublisher(publisher);
cout<<" Enter author : ";
cin.getline(author,sizeof(author));
b.setAuthor(author);
displayBook(b); // call to method
return 0;
}
Output:
Enter ISBN : 1912277282
Enter book title : God of small Things
Enter publisher : BPB
Enter author : Arunditi Roy
Book name : God of small Things ISBN :1912277282 Author : Arunditi Roy Publisher: BPB
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.