We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 13: Abstract Classes
  5. Discussions

Day 13: Abstract Classes

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 557 Discussions, By:

recency

Please Login in order to post a comment

  • lucasopoka
    2 weeks ago+ 0 comments
    from abc import ABCMeta, abstractmethod
    class Book(object, metaclass=ABCMeta):
        def __init__(self,title,author):
            self.title=title
            self.author=author   
        @abstractmethod
        def display(): pass
    
    class MyBook(Book):
        def __init__(self, title, author, price):
            super().__init__(title, author)
            self.price = price
        def display(self):
            print(f'Title: {self.title}', f'Author: {self.author}', f'Price: {self.price}', sep='\n')   
        
    
    title=input()
    author=input()
    price=int(input())
    new_novel=MyBook(title,author,price)
    new_novel.display()
    
    0|
    Permalink
  • elsebaey95
    4 weeks ago+ 0 comments

    c++

    // Write your MyBook class here
    class MyBook : Book{
        int price;
        
        //   Class Constructor
        //   
        //   Parameters:
        //   title - The book's title.
        //   author - The book's author.
        //   price - The book's price.
        //
        // Write your constructor here
        
        public:
        MyBook (string title, string author, int price ):Book(title,author){
            this->price=price;
        }
            
        //   Function Name: display
        //   Print the title, author, and price in the specified format.
        //
        // Write your method here
        
        void display() {
            cout << "Title: " << title << endl;
            cout << "Author: " << author << endl;
            cout << "Price: " << price << endl;
        }
    
    // End class
    
    };
    
    0|
    Permalink
  • ehabmagdy
    1 month ago+ 0 comments

    java

    class MyBook extends Book{
        int price;
        MyBook(String title, String author, int price){
            super(title, author);
            this.price = price;
        }
        @Override
        void display(){
            System.out.println("Title: "+super.title);
            System.out.println("Author: "+super.author);
            System.out.println("Price: "+ price);
        }
    }
    
    0|
    Permalink
  • kayceejenz
    2 months ago+ 0 comments

    Typescript Implementation:

    function main() {
        // Enter your code here
        const title: string = readLine();
        const author: string = readLine();
        const price: number = parseInt(readLine())
        
        const book = new MyBook(title,author,price);
        book.display();
    }
    
    abstract class Book {
        constructor(public title: string, public author: string, public price: number){}
        display(){
            console.log(`Title: ${this.title}\nAuthor: ${this.author}\nPrice: ${this.price}`)
        }
    }
    class MyBook extends Book{
        constructor(title: string, author: string, price: number){
            super(title, author, price);
        }
    }
    
    0|
    Permalink
  • paul_sowrin
    3 months ago+ 0 comments

    Solution in C++:

    include

    include

    include

    include

    include

    include

    using namespace std; class Book{ protected: string title; string author; public: Book(string t,string a){ title=t; author=a; } virtual void display()=0;

    };

    // Write your MyBook class here

    class MyBook : public Book{

    int price;
    //   Class Constructor
    //   
    //   Parameters:
    //   title - The book's title.
    //   author - The book's author.
    //   price - The book's price.
    //
    // Write your constructor here
    public:
    MyBook(string title, string author, int price) : Book(title, author), price(price)
    {
        this->price = price;
    }
    
    //   Function Name: display
    //   Print the title, author, and price in the specified format.
    //
    // Write your method here
    
    void display() override
    {
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Price: " << price << endl;
    }
    

    };
    // End class

    int main() { string title,author; int price; getline(cin,title); getline(cin,author); cin>>price; MyBook novel(title,author,price); novel.display(); return 0; }

    0|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy