Question

give some sample codes that shows the concepts of inheritance and polymorphism and show why they...

give some sample codes that shows the concepts of inheritance and polymorphism and show why they are so important in OOPS

0 0
Add a comment Improve this question Transcribed image text
Answer #1

INHERITANCE

Inheritance of a class, the capability to inherit the features and characteristics of another class. The class which inherits inherits which inherits inherits the properties of another class is known as a child or a subclass. On the other hand other hand hand, the classes properties are inherited by a child class is known as a superclass or parent class.

Inheritance can be divided into 5 broad types as follows:
1. Single inheritance:
Here 1 subclass inherits from from single superclass.

2. Multiple inheritance
Here a single single subclass inherits from multiple super classes classes multiple super classes classes from multiple super classes classes classes.

3. Multilevel inheritance
Here subclass inherit from a from a superclass which is already the child of another superclass.

4. Hierarchical inheritance
In this type of of inheritance type of of inheritance, more than 1 sub classes inherit from a single superclass.

5. Hybrid inheritance or virtual inheritance
This type of inheritance is created by combining two or more types of inheritance that are discussed above discussed above.

The following figures describe the various types of inheritance with more clarity.
Single inheutance C2Mutiple Inhuritance MOTOR VEHICLE FOUR- WHEELERS VEHICLE VEHICLE CAR CA R Multilevel nhartifance VEH ICLE

The following is a sample code that describes the concept of inheritance in C + ++.

//PROGRAM IN C PLUS PLUS TO DEMONSTRATE THE IMPLEMENTATION OF INHERITANCE

#include<iostream>
using namespace std;

//super class or the the parent or the the parent class
class parentClass
{
        public:
        int parent_id;
}

//subclass or the child class that is inheriting properties and characteristics from the class parent.

class childClass : public parentClass       // public mode single inheritance.
{
        public:
        int child_id;
}

//the main function function.

int main()
{
        childClass object_a;

/* the object of childClass is created and has all the the all the the data members and member functions of its parent parent of its parent parent, and hence it is capable of calling to those those data members and member functions as shown below below */

        object_a.parent_id=100;
        object_a.child_id=200;

        cout<<" identification number of the child class is : "<< object_a.child_id<<endl;
        cout<<" identification number of the parent class is : "<< object_a.parent_id<<endl;

        return 0;
}

output:-
identification number of the child class is : 200
identification number of the parent class is : 100

Importance of inheritance.

Suppose we face the need to the need to implement various classes that use the same type of data types and functions, the use of inheritance can increase the usability of the code and removes the need to write write write the same functions and declarations again and again.
For example a class Sports Sports has subclasses such as cricket, football, Polo, basketball, rugby etc. The declaration and definition of the similar kind of functions every time for each of the subclasses each of the subclasses can be avoided by inheriting single standard function from the superclass Sports. Hence the same code is not needed to be implemented multiple Times as it can be easily inherited from the parent class class.
Inheritance here helps the objects to take on the properties of the properties of existing objects. This increases the reusability of the code as as as of the code as as well as makes the program faster, decreases the memory required to store and execute the program and hence contributes towards the object oriented programming model.

POLYMORPHISM

Polymorphism can be described as the ability of the same information to be displayed be displayed in various forms. Similarly the objects can have multiple properties in a program program. Real world example of polymorphism can be a lady who can have various roles of sister, wife, daughter and mother.

In programming polymorphism means polymorphism means a call to A Member Member function to A Member Member function will lead to calling of to calling of calling of a different Member function internally which is to be executed based on the type of object that invokes the Member function function in the first place.

Polymorphism can be of two types types:

1. Runtime polymorphism

This type of polymorphism occurs when the member functions are overridden. When at the time of execution it is discovered that the child class has a definition for the Member function of the parent class, the definition in the child class overrides the parent class class definition parent class class definition of the given Member function function Member function function given Member function function Member function function.

2. Compile time polymorphism

When member functions or operator with with the same name all definition definition are present in a program a program but are differentiated by the number of arguments are the type of arguments they carry carry, it is referred to as compile time polymorphism. It may consist consist of either function overloading or operator overloading.

The following is the sample code that depict the the idea of polymorphism using function overloading by compile time polymorphism mechanism.

// Program to depict compile time polymorphism using function overloading

#include <iostream>
using namespace std;
class nums
{
public:

// Member function which has only one parameter of integer type
void fn(int a)
{
cout << "value of a is " << a << endl;
}

// Member function which has one parameter of of double type
void fn(double a)
{
cout << "value of a is " << a << endl;
}

// Member function with two parameters of integer type of integer type parameters of integer type type.
void fn(int a, int b)
{
cout << "value of a and b is " << a << ", " << b << endl;
}
};

int main() {

nums object1;

// The call of a particular function will depend on the parameters carried by it.
// The following statement will call the first function the first function function.
object1.fn(12);

// The following statement will call the second function.
object1.fn(12.45);

// Following statement will call the third function the third function
object1.fn(12,45);

return 0;
}

Output:-
value of a is 12
value of a is 12.45
value of a and b is 12, 45

Importance of polymorphism

Polymorphism is a very important aspect of object oriented programming can be used to change the characteristic of the program at the time of compilation or execution based on requirement of of of the object.
This facility helps in easily changing the system without affecting the application application on a generic generic level.

That's the same program is capable of having a very fluid and changeable behaviour based behaviour based on the data which is being used.
This helps in increasing the reusability of the code code and making the program faster as well as decreasing the volume of work it will lead to sharing of processing resources instead of making different copies each time.

Add a comment
Know the answer?
Add Answer to:
give some sample codes that shows the concepts of inheritance and polymorphism and show why they...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT