Knowee
Questions
Features
Study Tools

Problem StatementProgram to implement the run time polymorphism.Input Format:Single line of input contain a dog or cat.Output Format:Single line of output contain input animal speaking sound.Sample Input:Enter the type of animal (Dog or Cat): catSample Output:The cat says: Meow!

Question

Problem StatementProgram to implement the run time polymorphism.Input Format:Single line of input contain a dog or cat.Output Format:Single line of output contain input animal speaking sound.Sample Input:Enter the type of animal (Dog or Cat): catSample Output:The cat says: Meow!

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution for the problem:

class Animal:
    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return "Bark!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

animal_type = input("Enter the type of animal (Dog or Cat): ")
animal = None

if animal_type.lower() == "dog":
    animal = Dog()
elif animal_type.lower() == "cat":
    animal = Cat()

if animal is not None:
    print(f"The {animal_type} says: {animal.speak()}")
else:
    print("Invalid animal type")

This program uses the concept of polymorphism in object-oriented programming. It first defines a base class Animal with a method speak(). Then it defines two subclasses Dog and Cat that override the speak() method to return their respective sounds.

The user is asked to input the type of animal. Depending on the input, an instance of Dog or Cat is created and assigned to the animal variable. Then the speak() method is called on this instance, and the returned sound is printed. If the input is neither "dog" nor "cat", the program prints "Invalid animal type".

This problem has been solved

Similar Questions

____________ can be achieved through inheritance.Select one:a.code reusabilityb.run time polymorphismc.both run time polymorphism & code reusabilityd.none of the options

Classifier Send Feedback Problem Statement: You are given a numerical input n. Write a function "classifier" that takes n as input and uses conditional statements to determine the appropriate category label based on the given input. Input: The function takes one integer as input n (int): The numerical input. Output: The function returns the category label. The category labels are as follows: 0 corresponds to "cat" 1 corresponds to "dog" Any other value corresponds to "human" Sample Input 1: classifier(0) Sample Return 1: cat Sample Input 2: classifier(1) Sample Return 2: dog Sample Input: classifier(5) Sample Return: human

Which of the following is an example of runtime polymorphism?Function overloadingOperator overloadingVirtual functionsInline functions

Problem StatementAman is assigned a program that takes a generic pointer (void *) to handle different data types. You have to guide him in completing the program.The program prompts the user to input a data type identifier ('i' for integer, 'f' for float) and a corresponding value. Using a switch statement, print the entered value with the appropriate data type label. Any other input except ('i' and 'f') is given, print "Invalid type entered!" Input format :The first line of input consists of a character, representing a data type (i for integer or f for float).If the character is i, the second line consists of an integer value.If the character is f, the second line consists of a floating-point value.Output format :If the character is i, print "Integer: " followed by the integer value.If the character is f, print "Float: " followed by the float value, rounded off to two decimal places.If any other character is entered, print "Invalid type entered!"Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ integer ≤ 1501.0 ≤ float ≤ 150.0Sample test cases :Input 1 :i4Output 1 :Integer: 4Input 2 :f5.5Output 2 :Float: 5.50Input 3 :p50Output 3 :Invalid type entered!Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:voidSet 2:*

Create a class called Animal with a method sound() that displays a generic sound. Then, create a subclass called Dog that inherits from Animal and overrides the sound() method to display "Woof". Create another subclass called Cat that inherits from Animal and overrides the sound() method to display "Meow". Finally, create a subclass called Cow that inherits from Animal and overrides the sound() method to display "Moo". In the main method, create objects of Dog, Cat, and Cow classes and call the sound() method for each object

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.