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 12: Inheritance
Day 12: Inheritance
Sort by
recency
|
1186 Discussions
|
Please Login in order to post a comment
Here is HackerRank Day 12 Inheritance solution - https://programmingoneonone.com/hackerrank-day-12-inheritance-30-days-of-code-solution.html
python code
class Person: def init(self, firstName, lastName, idNumber): self.firstName = firstName self.lastName = lastName self.idNumber = idNumber def printPerson(self): print("Name:", self.lastName + ",", self.firstName) print("ID:", self.idNumber) class Student(Person): def init(self,firstName,lastName,idNumber,scores): super().init(firstName,lastName,idNumber) self.scores=scores def calculate(self): m=sum(self.scores)/len(self.scores) if m>=90: return 'O' elif m>=80: return 'E' elif m>=70: return 'A' elif m>=55: return "P" elif m>=40: return 'D' else: return 'T' line = input().split() firstName = line[0] lastName = line[1] idNum = line[2] numScores = int(input()) # not needed for Python scores = list( map(int, input().split()) ) s = Student(firstName, lastName, idNum, scores) s.printPerson() print("Grade:", s.calculate())
Given code has compliation error C#:
Solution.cs(91,21): error CS0019: Operator '+' cannot be applied to operands of type 'string' and 'void'
Please fix it, we cannot edit these code. thanks!
If this is your first time with cpp classes I recommend you these 2 videos:
Constructor video
Inheritance video
For constructors with inheritance you can do something like this:
import java.util.Scanner;
class Person { String firstName; String lastName; int id;
}
class Student extends Person { private int[] testScores;
}
public class Solution { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String firstName = scn.next(); String lastName = scn.next(); int id = scn.nextInt(); int numScores = scn.nextInt(); int[] scores = new int[numScores];
}