class inheritance code example
Example 1: what is a child inheritance in python with example
# =============================================================================
# Inhertance
# =============================================================================
class A:
def feature1(self):
print('Feature 1 in process...')
def feature2(self):
print('Feature 2 in process...') #Pt.1
class B:
def feature3(self):
print('Feature 3 in process...')
def feature4(self):
print ('Feature 4 in process...')
a1 = A()
a1.feature1()
a1.feature2()
a2 = B()
a2.feature3()
a2.feature4()
# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE
# WITH THE USE OF INHERITANCE IS BELOW
class A:
def feature1(self):
print('Feature 1 in process...')
def feature2(self):
print('Feature 2 in process...')
class B(A):
def feature3(self):
print('Feature 3 in process...') # Pt.2
def feature4(self):
print ('Feature 4 in process...')
a1 = A()
a1.feature1()
a1.feature2()
a2 = B()
a2.feature3()
a2.feature4()
# NOW TO CHECK OUT THE DIFFERENCE BETWEEN Pt.1
# AND Pt.2 TRY RUNNIG THE CODE ON THE BASIS OF
# INHERITANCE, IN OTHER WORDS TRY RUNNING ONLY
# B CLASS IN Pt.2 AND THEN RUN ONLY a2
# YOU WILL SEE A DIFFERENCE IN THE RUNNING OF
# ONLY a2,,,, IT WILL STILL SHOW THAT FEATURE 3
# AND 4 IS IN PROCESS,, THIS MEANS THAT B IS THE
Example 2: inheritance in python 3 example
class Robot:
def __init__(self, name):
self.name = name
def say_hi(self):
print("Hi, I am " + self.name)
class PhysicianRobot(Robot):
def say_hi(self):
print("Everything will be okay! ")
print(self.name + " takes care of you!")
y = PhysicianRobot("James")
y.say_hi()
Example 3: inheritance in c++
#include <iostream>
class Entity {
public:
float x =0 , y = 0;
void Move(float xa, float ya) {
x += xa;
y += ya;
}
};
class Player:public Entity
{
public :
const char* name = nullptr;
void Print() {
std::cout << name << std::endl;
}
};
int main()
{
Player D;
D.x = 5.5f;
D.y = 4.4f;
D.Move(1.1f,2.2f);
D.name = "Caleb";
D.Print();
std::cout <<"Size of Entity was : " << sizeof(Entity) << std::endl;
std::cout <<"Size of Player was : "<< sizeof(Player) << std::endl;
std::cin.get();
}
Example 4: inheritence in python
# =============================================================================
# Inhertance
# =============================================================================
class A:
def feature1(self):
print('Feature 1 in process...')
def feature2(self):
print('Feature 2 in process...') #Pt.1
class B:
def feature3(self):
print('Feature 3 in process...')
def feature4(self):
print ('Feature 4 in process...')
a1 = A()
a1.feature1()
a1.feature2()
a2 = B()
a2.feature3()
a2.feature4()
# THE ABOVE PROGRAM IS A PROGRAM WITHOUT USING INHERITANCE
# WITH THE USE OF INHERITANCE IS BELOW
class A:
def feature1(self):
print('Feature 1 in process...')
def feature2(self):
print('Feature 2 in process...')
class B(A):
def feature3(self):
print('Feature 3 in process...') # Pt.2
def feature4(self):
print ('Feature 4 in process...')
a1 = A()
a1.feature1()
a1.feature2()
a2 = B()
a2.feature3()
a2.feature4()