Single-level inheritance is the simplest form of inheritance, where a child class inherits properties and behaviors from a single parent class.
📌 Basic Example of Single Level Inheritance
🚀 Approach
- Class
Parent
→ Contains a functionshowParent()
. - Class
Child
(Derived from Parent) → InheritsshowParent()
.
🔹 C++ Code: Basic Example of Single Level Inheritance
#include <iostream>
using namespace std;
// Base Class
class Parent {
public:
void showParent() {
cout << "This is the Parent class." << endl;
}
};
// Derived Class (Inherits from Parent)
class Child : public Parent {
public:
void showChild() {
cout << "This is the Child class." << endl;
}
};
int main() {
Child obj;
obj.showParent(); // Inherited from Parent
obj.showChild(); // Defined in Child
return 0;
}
🔹 Output
This is the Parent class.
This is the Child class.
📌 Eating Coding Program
🚀 Approach
- Class
Person
→ Haseat()
function. - Class
Coder
(Derived from Person) → Inheritseat()
and addscode()
function.
🔹 C++ Code: Eating & Coding Example
#include <iostream>
using namespace std;
// Base Class
class Person {
public:
void eat() {
cout << "I am eating!" << endl;
}
};
// Derived Class
class Coder : public Person {
public:
void code() {
cout << "I am coding in C++!" << endl;
}
};
int main() {
Coder obj;
obj.eat(); // Inherited from Person
obj.code(); // Defined in Coder
return 0;
}
🔹 Output
I am eating!
I am coding in C++!
📌 Print Salary Bonus Example in C++
🚀 Approach
- Class
Employee
→ Storessalary
. - Class
Bonus
(Derived from Employee) → Computes bonus.
🔹 C++ Code: Salary Bonus Calculation
#include <iostream>
using namespace std;
// Base Class
class Employee {
protected:
float salary;
public:
void setSalary(float s) {
salary = s;
}
void displaySalary() {
cout << "Base Salary: " << salary << endl;
}
};
// Derived Class
class Bonus : public Employee {
public:
void displayBonus() {
cout << "Bonus: " << (salary * 0.1) << endl;
}
};
int main() {
Bonus emp;
float sal;
cout << "Enter salary: ";
cin >> sal;
emp.setSalary(sal);
emp.displaySalary();
emp.displayBonus();
return 0;
}
🔹 Example Output
Enter salary: 50000
Base Salary: 50000
Bonus: 5000
📌 Multiplication using Single Level Inheritance
🚀 Approach
- Class
Numbers
→ Stores two numbers. - Class
Multiply
(Derived from Numbers) → Multiplies them.
🔹 C++ Code: Multiplication using Single Level Inheritance
#include <iostream>
using namespace std;
// Base Class
class Numbers {
protected:
int num1, num2;
public:
void setNumbers(int a, int b) {
num1 = a;
num2 = b;
}
};
// Derived Class
class Multiply : public Numbers {
public:
void displayProduct() {
cout << "Product = " << num1 * num2 << endl;
}
};
int main() {
Multiply obj;
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
obj.setNumbers(a, b);
obj.displayProduct();
return 0;
}
🔹 Example Output
Enter two numbers: 4 5
Product = 20
📌 Summary
Example | Description |
---|---|
Basic Single Level Inheritance | Parent class method is inherited in Child class. |
Eating Coding Program | Demonstrates inheritance using an eat() and code() function. |
Print Salary Bonus Example | Computes salary bonus using inheritance. |
Multiplication using Single Level Inheritance | Performs multiplication using two inherited numbers. |
🚀 Need more examples? Let me know! 🚀
0 Comments