give some sample codes that shows the concepts of inheritance and polymorphism and show why they are so important in OOPS
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.
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.
give some sample codes that shows the concepts of inheritance and polymorphism and show why they...
explain the following concepts providing well structured notes and examples. a) inheritance b) polymorphism c) aggregation d) encapsulation
Demonstrate through code and explanation each of the following object oriented concepts: Inheritance Encapsulation Polymorphism Discuss the advantages of applying object orienting coding principles.
State the purpose of the Current Procedural Terminology (CPT) codes and discuss some of the ways these codes are used throughout the United States. (b) What critical link do CPT and ICD-10 codes provide when put together, and (c) why is their accuracy so important? Minimum word count for parts 150 words.
Give 2 examples of different species concepts, define them, and discuss why one would be preferred over the other in some areas.
1. Economists think in terms of key concepts. List the concepts that economists think in terms of. Define opportunity cost. Use the concept of opportunity cost to explain why some things are not done. Explain why economists consider costs and benefits, instead of only benefits. Give an example from your life where you have considered both costs and benefits. Explain why it is important to consider unintended effects.
1a) What are some of the most common motivations for someone to emigrate? b. Give some examples of immigration during the patriarchal period in the book of Genesis? c) Why was Israel’s immigrant experience so important to their national identity?
JAVA -
Abstraction and Encapsulation are one pillar of OOP (Object
Oriented Programming). Another is inheritance and polymorphism. In
this assignment we will use inheritance and polymorphism to solve a
problem.
Part (a) of the figure below shows a symbolic representation of
an electric circuit called an amplifier. The input to the amplifier
is the voltage vi and the output is the voltage vo. The output of
an amplifier is proportional to the input. The constant of
proportionality is called...
Define the concepts internal and external validity. Discuss why we say that for some experiments an attempt at increasing one type tends to jeopardize the other type. research methods-psy230
why does some materials have
greater than Egap give an example?
Why does some materials have E=h nu greater than Egap give an example?
Give examples why some nail showed corrosion at the air/water interface. An in ground petrol storage tank at an ADNOC station is buried at depth so that sites partially below the water table. Over a period of time where would you expect most external corrosion of the tank? Give reason for your answer Explain the reason for the different amounts of corrosion in the nails in tap water, distilled water and boiled distilled water · » If corrosion of the...