inheritance in oops code example
Example 1: 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 2: what is inheritance
it is used to define relationship between two class,
which a child class occurs all the properties and behaviours of a parent class.
Provides code reusability.
Ex: in my framework I have a TestBase class which I store
all my reusable code and methods. My test execution classes and
elements classes will extend the TestBase in order to reuse the code.
Example 3: inheritance in oops
it is used to define relationship between two class,
which a child class occurs all the properties and
behaviours of a parent class.
Provides code reusability. We can implement by using
extend keyword
Ex: in my framework I have a TestBase class which I store
all my reusable code and methods. My test execution classes and
elements classes will extend the TestBase in order to reuse the code.