## Algorithm

Problem Name: 30 days of code - Day 13: Abstract Classes

In this HackerRank in 30 Days of Code Day 13: Abstract Classes problem solution,

Objective
Today, we will extend what we learned yesterday about Inheritance to Abstract Classes. Because this is a very specific object oriented concept, submissions are limited to the few languages that use this construct. Check out the Tutorial tab for learning materials and an instructional video.

Given a Book class and a Solution class, write a MyBook class that does the following:

• Inherits from Book
• Has a parameterized constructor taking these 3 parameters:
• string title.
• string author.
• int price.
• Implements the Book class' abstract display() method so it prints these 3 lines:
• Title: , a space, and then the current instance's title.
• Author:, a space, and then the current instance's author.
• Price:, a space, and then the current instance's price.

Note: Because these classes are being written in the same file, you must not use an access modifier (e.g.: public) when declaring MyBook or your code will not execute.

Input Format

You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object.

Output Format

The void display() method should print and label the respective title,author and price of the MyBook object's instance (with each value on its own line) like so:

Title: $title Author:$author
Price: $price  Note: The is prepended to variable names to indicate they are placeholders for variables. Sample Input The following input from stdin is handled by the locked stub code in your editor: The Alchemist Paulo Coelho 248  Sample Output The following output is printed by your display() method: Title: The Alchemist Author: Paulo Coelho Price: 248 ## Code Examples ### #1 Code Example with C++ Programming Code - C++ Programming  #include <iostream> using namespace std; class Book { protected: string title; string author; public: Book(string t, string a) { title = t; author = a; } virtual void display()=0; }; class MyBook : public Book { private: int price; public: MyBook(string title, string author, int price) : Book(title, author) { this->price = price; } void display() { cout << "Title: " << this->title << endl; cout << "Author: " << this->author << endl; cout << "Price: " << this->price << endl; } }; int main() { string title, author; int price; getline(cin, title); getline(cin, author); cin >> price; MyBook novel(title, author, price); novel.display(); return 0; }  Copy The Code & ### #2 Code Example with C# Programming Code - C# Programming  using System; abstract class Book { protected string title; protected string author; public Book(string t, string a) { title = t; author = a; } public abstract void display(); } class MyBook : Book { int price; public MyBook(string t, string a, int p) : base(t, a) { price = p; } public override void display() { Console.WriteLine($"Title: {title}\nAuthor: {author}\nPrice: {price}");
}
}

class Solution
{
static void Main(String[] args)
{
Book new_novel = new MyBook(title, author, price);
new_novel.display();
}
}

Copy The Code &

### #3 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

abstract class Book {
String title;
String author;

Book(String t, String a) {
title = t;
author = a;
}

abstract void display();
}

class MyBook extends Book {
private int price;

MyBook(String t, String a, int p) {
super(t, a);
price = p;
}

@Override
void display() {
System.out.println("Title: " + title + "\nAuthor: " + author + "\nPrice: " + price);
}
}

public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String title = sc.nextLine();
String author = sc.nextLine();
int price = sc.nextInt();

Book new_novel = new MyBook(title, author, price);
new_novel.display();
}
}

Copy The Code &

### #4 Code Example with Javascript Programming

Code - Javascript Programming


'use strict';

var _input = '';
var _index = 0;
process.stdin.on('data', (data) => {
_input += data;
});
process.stdin.on('end', () => {
_input = _input.split(new RegExp('\n'));
main();
});

return _input[_index++];
}

/**** Ignore above this line. ****/

class Book {
constructor(title, author) {
if (this.constructor === Book) {
throw new TypeError('Do not attempt to directly instantiate an abstract class.');
} else {
this.title = title;
this.author = author;
}
}

display() {
console.log('Implement the \'display\' method!')
}
}

/**
*   Class Constructor
*
*   @param title The book's title.
*   @param author The book's author.
*   @param price The book's price.
**/
class MyBook extends Book {
constructor(title, author, price) {
super(title, author);
this.price = price
}
display() {
console.log(Title: ${this.title}) console.log(Author:${this.author})
console.log(Price: \${this.price})
}
}
/**
*   Method Name: display
*
*   Print the title, author, and price in the specified format.
**/

// End class

function main() {

let book = new MyBook(title, author, price)
book.display()
}

Copy The Code &

### #5 Code Example with Python Programming

Code - Python Programming


from abc import ABCMeta, abstractmethod

class Book(object, metaclass=ABCMeta):
def __init__(self, title, author):
self.title = title
self.author = author

@abstractmethod
def display(self): pass

class MyBook(Book):
def __init__(self, title, author, price):
super().__init__(title, author)
self.price = price

def display(self):
print("Title: " + self.title + "\nAuthor: " + self.author + "\nPrice: " + str(self.price))

title = input()
author = input()
price = int(input())
new_novel = MyBook(title, author, price)
new_novel.display()

Copy The Code &