Hi, for this question I am confused on the issue of header and .cpp file. Could
ID: 3708362 • Letter: H
Question
Hi, for this question I am confused on the issue of header and .cpp file. Could someone kindly provide the answer, if possible? Thanks
Write a C++ class called Book, which inherits from the Publication class given below. A Book has a year and a publisher and has its own display() method which prints out all information about a book, i.e. author, title, year and publisher. Book should also have a constructor which allows all book information to be initialised and a destructor which prints out a message when a Book object is destroyed. Explain the meaning of the virtual keyword, used in the definition of the display() method. class Publication protected: string author, title; public: Publication(string ar, string tl) : author (ar), title(tl) ( ) virtual void display() coutExplanation / Answer
Virtual keyword is used to print the method of child class using the base class pointer which is initialized with address of base class.
Please find the source files and the comments for your question regarding header file and cpp file.
main.cpp
#include "Book.h"
int main() {
Publication p1("chegg","Chegg Solution"), *ptr_pub;
p1.display(); // Normal object display call
cout<<" ";
ptr_pub = &p1;
ptr_pub->display(); // Call to display by pointer
cout<<" ";
Book b("chegg", "Chegg Solution", 2018, "Chegg LTd"), *ptr_book;
b.display(); // Normal object display call.
cout<<" ";
ptr_book = &b;
ptr_book->display(); // Ptr call to display method
cout<<" ";
// This is initialization of base class pointer with derived class pointer.
ptr_pub = &b;
// If the function display is not virtual then the ptr_pub would have called
// Publication::display() function.
// Since it is virtual it will check for the type of pointer pointing to address.
// Thus it prints display from Book class.
ptr_pub->display();
cout<<" ";
return 0;
}
Book.h
#ifndef __HEADER__
#define __HEADER__
#include <iostream>
#include <string>
using namespace std;
/*
This is header file for managing declaration of functions.
Here I have given declaration of Book class.
*/
// Class publication as it is given in the question.
class Publication {
protected :
string author, title;
public:
Publication(string ar, string tl): author(ar), title(tl) {}
virtual void display() { cout<< author<<","<<title;}
};
// This class inherits from Publication class.
class Book: public Publication {
protected:
int year;
string publisher;
public:
// declaration of constructor
Book(string ar, string tl, int yr, string pub);
// Declaration of display method.
void display();
// Declaration of destructor.
~Book();
};
Book.cpp
#include "Book.h"
// Definition of constructor.
// Given call to the parent class constructor in initialization list.
Book::Book(string ar, string tl, int yr, string pub): Publication(ar,tl) {
year = yr;
publisher = pub;
}
// Definition of display method.
void Book::display() {
// Giving call to the display method of parent class.
Publication::display();
// Printing the year and publisher.
cout<< ","<<year<<","<<publisher;
}
// The destructor of Book class that is called when the scope of book object is completed.
Book::~Book() {
cout<<"Book object destroyed successfully ";
}
Sample output:
./a.out
chegg,Chegg Solution
chegg,Chegg Solution
chegg,Chegg Solution,2018,Chegg LTd
chegg,Chegg Solution,2018,Chegg LTd
chegg,Chegg Solution,2018,Chegg LTd
Book object destroyed successfully
Thanks,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.