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 My c++ solution :D
class book{ protected: string title; string author; book(string title, string author) : author(author), title(title) {} }; class MyBook: book { private: double price; public: MyBook(string author,string title, double price) : book(author, title), price(price) {} void display(){ cout<<"Title: "<<title<<endl; cout<<"Author: "<<author<<endl; cout<<"Price: "<<price<<endl; }; };
+ 0 comments My c++ solution :D
class book{ protected: string title; string author; book(string title, string author) : author(author), title(title) {} }; class MyBook: book { private: double price; public: MyBook(string author,string title, double price) : book(author, title), price(price) {} void display(){ cout<<"Title: "<<title<<endl; cout<< "Author: "<<author<<endl; cout<<"Price: "<<price<<endl; }; };
+ 0 comments C# is here...:)
class MyBook : Book { int Price { get; set; } public MyBook(string title, string author, int price) : base(title, author) { Price = price; } public override void display() { Console.WriteLine($"Title: {title}"); Console.WriteLine($"Author: {author}"); Console.WriteLine($"Price: {Price}"); } }
+ 0 comments class MyBook extends Book {
int price; MyBook(String title ,String author, int price) { super(title,author); this.price=price; } public void display() { System.out.println("Title: "+title); System.out.println("Author: "+author); System.out.println("Price: "+price); } }
+ 0 comments My C++ solution:
#include <string> #include <iostream> using namespace std; class Book{ private: string title; string author; protected: string getTitle() const { return title; } string getAuthor() const { return author; } public: Book(string title, string author) : title(title), author(author) {} virtual void display() = 0; }; class MyBook: Book{ private: int price; public: MyBook(string title, string author, int price) : Book(title, author), price(price) {} void display() { cout << "Title: " << getTitle() << endl << "Author: " << getAuthor() << endl << "Price: " << price << endl; } }; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ string title,author; getline(cin,title); getline(cin,author); int price; cin >> price; MyBook b(title,author,price); b.display(); return 0; }
Load more conversations
Sort 574 Discussions, By:
Please Login in order to post a comment