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.
Day 13: Abstract Classes
Day 13: Abstract Classes
+ 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 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 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 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 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 classint main() { string title,author; int price; getline(cin,title); getline(cin,author); cin>>price; MyBook novel(title,author,price); novel.display(); return 0; }
Load more conversations
Sort 557 Discussions, By:
Please Login in order to post a comment