In: Computer Science
This is my last assignment in a programming class I had to take as a prerequisite for a database course (seems odd). The instructor has been awful and I haven't learned much of anything. I use VBA at work and just can't follow C++. I'm stuck on this assignment and have no idea what I'm doing. I'm a database guy at work, I'm not a programmer. Please help.
In C++, a program that performs the following tasks:
1. Design a Book class that has three data members:
string title
int pages
double price
2. Add constructors and methods (as well as method parameters when necessary):
Book() // constructor(s)
getBookInfo() // print title, pages, price
setPrice() // change price
3. In the main() function, create two Book objects:
b1("C++ Programming", 834, 93.53)
b2("Data Structures", 217, 64.42)
4. Change the price of b2 to 55.46 using the setPrice() method.
5. Using cout, Print out book information using the getBookInfo() method as shown below:
C++ Programming 834 93.53
Data Structures 217 55.46
IN C++, a program that performs the following tasks:
1. Derive a TextBook class (child) from the Book class (parent).
2. Add new data members:
string school
double discount
3. Add new constructors and methods (as well as method parameters when necessary):
TextBook() // constructor(s)
compTextBookPrice() // price = price - discount
4. Override inherited methods:
getBookInfo() // print title, pages, price, school, discount
5. In the main() function, create two Book objects and two TextBook objects:
b1("C++ Programming", 834, 93.53)
b2("Data Structures", 217, 64.42)
tb1("Database", 365, 74.41, "YSU", 20.00)
tb2("Networks", 522, 92.58, "YSU", 20.00)
6. Using cout, print out book information as shown below:
C++ Programming 834 93.53
Data Structures 217 64.42
Database 365 54.41 YSU 20.00
Networks 522 72.58 YSU 20.00
check out the solution and do comment for any queries.
--------------------------------------------------------
#include <iostream>
using namespace std;
class Book
{
private:
string title;
int pages;
double price;
public:
// default constructor - initialize with default values
Book()
{
title = " ";
pages = 0;
price = 0.0;
}
// parameterized constructor - initialized with given values
Book(string t, int pg, double p)
{
title = t;
pages = pg;
price = p;
}
// print title , pages, price
void getBookInfo()
{
cout << "\n" << title << "\t" << pages
<< "\t" << price;
}
// pass the value of changed price
void setPrice(double p)
{
price = p;
}
};
int main() {
// 2 book objects
Book b1("C++ Programming", 834, 93.53);
Book b2("Data Structures", 217, 64.42);
// change the price of b2
b2.setPrice(55.46);
// print the information
b1.getBookInfo();
b2.getBookInfo();
}
-----------------------------------
code :
----------
Output ::
----------------------------------------------------------
Program extension with Inheritance concept.
-------------
#include <iostream>
#include <iomanip>
using namespace std;
class Book
{
public:
string title;
int pages;
double price;
public:
// default constructor
Book()
{
title = " ";
pages = 0;
price = 0.0;
}
// parameterized constructor
Book(string t, int pg, double p)
{
title = t;
pages = pg;
price = p;
}
// print title , pages, price
void getBookInfo()
{
cout << "\n" << title << "\t" << pages
<< "\t" << price;
}
// pass the value of changed price
void setPrice(double p)
{
price = p;
}
};
// child class
class TextBook : public Book
{
public:
// class members
string school;
double discount;
public:
// default constructor
TextBook() : Book()
{
school = " ";
discount = 0.0;
}
// parameterized constructor - call the super class constructor as
shown below passing required parameters to initialize
TextBook(string t, int pg, double p, string s, double d) : Book(t,
pg, p)
{
school = s;
discount = d;
}
// computes the price after deducting discount
void compTextBookPrice()
{
price = price - discount;
}
// override inherit methods
void getBookInfo()
{
cout << "\n" << title << "\t" << pages
<< "\t" << price << "\t" << school <<
"\t" << discount;
}
};
int main() {
// 2 book objects
Book b1("C++ Programming", 834, 93.53);
Book b2("Data Structures", 217, 64.42);
// 2 textbook objects
TextBook tb1("Database", 365, 74.41, "YSU", 20.00);
TextBook tb2("Networks", 522, 92.58, "YSU", 20.00);
// print the information
b1.getBookInfo();
b2.getBookInfo();
cout << fixed << showpoint << setprecision(2); //
to print 2 digits after decimal point
// compute the price after discount by a function call
tb1.compTextBookPrice();
tb2.compTextBookPrice();
// print information of textbook
tb1.getBookInfo();
tb2.getBookInfo();
}
-----------------------
code :
-------------------------------------------------
OUTPUT ::