In: Computer Science
C++
In Object Oriented Programming, classes represent abstractions of real things in our programs. We must quickly learn how to define classes and develop skills in identifying appropriate properties and behaviors for our class. To this end, pick an item that you work with daily and turn it into a class definition. The class must have at least three properties/data fields and three functions/behaviors (constructors and get/set functions don’t count). Do not pick examples from the textbook and do not redefine a class already defined by a classmate. Use proper C++ syntax to present your class.
C++ PROGRAM:
OUTPUT SNAPSHOTS:
TEXT CODE:
#include
using namespace std;
// Book Class
class Book{
// A Book can have its Name, Author and Number of Pages has its
attributes.
private:
string name;
string author;
int numberOfPages;
public:
// Getter Function for name
string getName(){
return name;
}
// Getter Function for author
string getAuthor(){
return author;
}
// Getter Function for numberOfPages
int getNumberOfPages(){
return numberOfPages;
}
// Setter Function for name
void setName(string n){
name = n;
}
// Setter Function for author
void setAuthor(string a){
author = a;
}
// Setter Function for numberOfPages
void setNumberOfPages(int pages){
numberOfPages = pages;
}
// Function to check if the book is popular or not by its
name
bool IsBookPopular(string nameOfBook){
if(nameOfBook=="C++ Primer" ||
nameOfBook=="Effective C++" ||
nameOfBook=="A Tour of C++"){
return true;
} else {
return false;
}
}
// Function to decide the Book size by the number of pages in
it
string BookSize(int pages){
if(pages<100){
return "Thin";
} else if(pages>=100 && pages< 300) {
return "Medium";
} else {
return "Thick";
}
}
// Function to check if the book author is famour of not
bool IsBookAuthorFamous(string author){
if(author=="Bjarne Stroustrup" ||
author=="Greg Perry" ||
author=="Brian Kernighan"){
return true;
} else {
return false;
}
}
};
int main()
{
// Creating the Book object
Book b1;
// Setting the values using Book Class object and its setter
function
b1.setName("Effective C++");
b1.setAuthor("Bjarne Stroustrup");
b1.setNumberOfPages(400);
// Checking the Behavior of the Book
if(b1.IsBookPopular(b1.getName()) == true){
cout << "Book is Popular!" << endl;
} else {
cout << "Book is Not Popular!" << endl;
}
if(b1.IsBookAuthorFamous(b1.getAuthor()) == true){
cout << "Book Author is Famous!" << endl;
} else {
cout << "Book Author is Not Famous!" << endl;
}
cout << "Book size is " <<
b1.BookSize(b1.getNumberOfPages()) << endl;
return 0;
}
EXPLANATION: