Write a C++ Program to solve the following problem: A vendor sells different pro
ID: 3689588 • Letter: W
Question
Write a C++ Program to solve the following problem:
A vendor sells different products which include Books and Software.
Create an abstract base class named Product which has three protected members (code, description and price). Product class contains one virtual function called displayProduct() (returns specific product information) which will be overridden by its derived classes (Book and Software). Besides inheriting from its base class, Book has author field, Software has version field. Write Constructors (including copy constructor), getter for each data member and one setData() function to set the values for all fields, Create an array of pointer which points to each product instance and display the product information in a loop. You can give fixed value to test your classes.
please use the members given... thank you so much in advance.
Explanation / Answer
#include<iostream>
using namespace std;
/* Abstract class: Product Class */
class Product{
protected:
int code;
string description;
double price;
public:
virtual void displayProduct() = 0; // pure virtual function
// getter methods
int getCode(){
return code;
}
string getDescription(){
return description;
}
double getPrice(){
return price;
}
};
class Book: public Product{
private:
string author;
public:
/* constructor that takes all inforamtion */
Book(int code, string description, double price, string author){
this->code = code;
this->description = description;
this->price = price;
this->author = author;
}
/* constructor that takes only code */
Book(int code){
this->code = code;
}
/* copy constructor */
Book(const Book &other){
this->code = other.code;
this->description = other.description;
this->price = other.price;
this->author = other.author;
}
void setData(string description, double price, string author){
this->description = description;
this->price = price;
this->author= author;
}
/* overrided method from base calss */
void displayProduct(){
cout<<"Code: "<<code<<", Description: "<<description<<
", Price: "<<price<<", Author: "<<author;
}
string getAuthor(){
return author;
}
};
class Software: public Product{
private:
string version;
public:
/* constructor that takes all inforamtion */
Software(int code, string description, double price, string version){
this->code = code;
this->description = description;
this->price = price;
this->version = version;
}
/* constructor that takes only code */
Software(int code){
this->code = code;
}
/* copy constructor */
Software(const Software &other){
this->code = other.code;
this->description = other.description;
this->price = other.price;
this->version = other.version;
}
/* setter methos */
void setData(string description, double price, string version){
this->description = description;
this->price = price;
this->version= version;
}
/* overrided method from base calss */
void displayProduct(){
cout<<"Code: "<<code<<", Description: "<<description<<
", Price: "<<price<<", Version: "<<version;
}
string getVersion(){
return version;
}
};
int main(){
Product *products[5];
products[0] = new Book(1, "CSE", 345.34, "Alex");
Book *book2 = new Book(2);
book2->setData("EEE", 543, "Bob");
products[1] = book2;
products[2] = new Software(3, "Adobe", 6543, "Kindwd dsadwqd");
Book *book3 = new Book(4);
book3->setData("EEE", 543, "Bob");
products[3] = book3;
Software *soft2 = new Software(5);
soft2->setData("Word", 4567, "Microsoft");
products[4] = soft2;
for(int i=0; i<5; i++){
products[i]->displayProduct();
cout<<endl;
}
return 0;
}
/*
sample run:
Code: 1, Description: CSE, Price: 345.34, Author: Alex
Code: 2, Description: EEE, Price: 543, Author: Bob
Code: 3, Description: Adobe, Price: 6543, Version: Kindwd dsadwqd
Code: 4, Description: EEE, Price: 543, Author: Bob
Code: 5, Description: Word, Price: 4567, Version: Microsoft
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.