Skip to content

Commit a3f6d7e

Browse files
Add files via upload
1 parent 735d0c3 commit a3f6d7e

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

13 - Abstract Classes/Solution.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Book {
6+
protected:
7+
string title;
8+
string author;
9+
public:
10+
Book(string t, string a) {
11+
title = t;
12+
author = a;
13+
}
14+
15+
virtual void display()=0;
16+
};
17+
//Aditya Seth
18+
19+
class MyBook : public Book {
20+
private:
21+
int price;
22+
public:
23+
MyBook(string title, string author, int price) : Book(title, author) {
24+
this->price = price;
25+
}
26+
27+
void display() {
28+
cout << "Title: " << this->title << endl;
29+
cout << "Author: " << this->author << endl;
30+
cout << "Price: " << this->price << endl;
31+
}
32+
};
33+
34+
int main() {
35+
string title, author;
36+
int price;
37+
getline(cin, title);
38+
getline(cin, author);
39+
cin >> price;
40+
MyBook novel(title, author, price);
41+
novel.display();
42+
return 0;
43+
}

13 - Abstract Classes/Solution.kt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.Scanner
2+
3+
abstract class Book(var title: String, var author: String) {
4+
5+
internal abstract fun display()
6+
}
7+
8+
class MyBook(t: String, a: String, private val price: Int) : Book(t, a) {
9+
10+
override fun display() {
11+
println("Title: $title\nAuthor: $author\nPrice: $price")
12+
}
13+
}
14+
15+
fun main(args: Array<String>) {
16+
val sc = Scanner(System.`in`)
17+
val title = sc.nextLine()
18+
val author = sc.nextLine()
19+
val price = sc.nextInt()
20+
21+
val newNovel = MyBook(title, author, price)
22+
newNovel.display()
23+
}

13 - Abstract Classes/Solution.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
4+
class Book(object, metaclass=ABCMeta):
5+
def __init__(self, title, author):
6+
self.title = title
7+
self.author = author
8+
9+
@abstractmethod
10+
def display(self): pass
11+
12+
#Aditya Seth
13+
class MyBook(Book):
14+
def __init__(self, title, author, price):
15+
super().__init__(title, author)
16+
self.price = price
17+
18+
def display(self):
19+
print("Title: " + self.title + "\nAuthor: " + self.author + "\nPrice: " + str(self.price))
20+
21+
22+
title = input()
23+
author = input()
24+
price = int(input())
25+
new_novel = MyBook(title, author, price)
26+
new_novel.display()

13 - Abstract Classes/Solution.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Book
2+
attr_accessor :title
3+
attr_accessor :author
4+
5+
def initialize(title, author)
6+
raise 'You cannot instantiate an abstract class.'
7+
end
8+
9+
def display
10+
raise 'You must override this method in your implementing class.'
11+
end
12+
end
13+
14+
class MyBook < Book
15+
attr_accessor :price
16+
17+
def initialize(title, author, price)
18+
@title = title
19+
@author = author
20+
@price = price
21+
end
22+
23+
def display()
24+
print "Title: #{self.title}"
25+
print "Author: #{self.author}"
26+
print "Price: #{self.price}"
27+
end
28+
end
29+
30+
title = gets
31+
author = gets
32+
price = gets
33+
34+
new_novel = MyBook.new(title, author, price)
35+
new_novel.display

0 commit comments

Comments
 (0)