envoy1084
/
30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
WARNING: This repository is currently undergoing updates and revisions to incorporate the latest information and advancements in Solidity programming. Please be advised that the content may not be up-to-date or accurate during this time. We expect the updates to be completed within the next 30 days, and appreciate your patience during this process. Thank you for your understanding.
Contents
- Day 1 - Licenses and Pragma
- Day 2 - Comments
- Day 3 - Initializing Basic Contract
- Day 4 - Variables and Scopes
- Day 5 - Operators
- Day 6 - Types
- Day 7 - Functions
- Day 8 - Loops
- Day 9 - Decision Making
- Day 10 - Arrays
- Day 11 - Array Operations
- Day 12 - Enums
- Day 13 - Structs
- Day 14 - Mappings
- Day 15 - Units
- Day 16 - Require Statement
- Day 17 - Assert Statement
- Day 18 - Revert Statement
- Day 19 - Function Modifiers
- Day 20…
This is Day 24 of 30 in Solidity Series
Today I Learned About Multiple Inheritance in Solidity.
Multiple Inheritance
In Multiple Inheritance, a single contract can be inherited from many contracts. A parent contract can have more than one child while a child contract can have more than one parent.
Example: In the below example, contract A is inherited by contract B, contract C is inheriting contract A, and contract B, thus demonstrating Multiple Inheritance.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Defining contract A
contract A {
string internal x;
function setA() external {
x = "Multiple Inheritance";
}
}
// Defining contract B
contract B {
uint256 internal pow;
function setB() external {
uint256 a = 2;
uint256 b = 20;
pow = a**b;
}
}
// Defining child contract C
// inheriting parent contract
// A and B
contract C is A, B {
// Defining external function
// to return state variable x
function getStr() external returns (string memory) {
return x;
}
// Defining external function
// to return state variable pow
function getPow() external returns (uint256) {
return pow;
}
}
// Defining calling contract
contract caller {
// Creating object of contract C
C contractC = new C();
// Defining public function to
// return values from functions
// getStr and getPow
function testInheritance() public returns (string memory, uint256) {
contractC.setA();
contractC.setB();
return (contractC.getStr(), contractC.getPow());
}
}
Output:
when we call the testInheritance function, the output is ("Multiple Inheritance", 1024).
Top comments (0)