In my learning path on blockchain Solidity course by Patrick Collins, today, I learnt about inheritance, Override and Virtual, I did recaps on array and struct, and I learnt a few more things which I am sharing with you all.
contract Newcheckmate is scofieldNew{
//ovaride
//virtual
function store(uint _favouriteNumbner) public override{
newuser = _favouriteNumbner + 5;
}
}
In the Solidity programming language, inheritance is implemented using the `is` keyword. The code provided demonstrates an example of inheritance in Solidity, where the contract “Newcheckmate” inherits from the contract “scofieldNew”.
This means that “Newcheckmate” has access to all the properties and methods defined in “scofieldNew”, as well as any properties and methods defined in any contracts that “scofieldNew” may inherit from.
Overriding functions
contract scofieldNew{
function store(uint _favouriteNumbner) public virtual{
newuser = _favouriteNumbner;
}
}
contract Newcheckmate is scofieldNew{
function store(uint _favouriteNumbner) public override{
newuser = _favouriteNumbner + 5;
}
}
In this example, the “Newcheckmate” contract overrides the “store” function defined in “scofieldNew”.
The “override” keyword indicates that the function in the derived contract is intended to replace the function with the same name in the base contract.
The “virtual” keyword before the function in the base contract indicates that the function can be overridden.
Arrays in Solidity
struct addUser{
string name;
uint favoriteNumber;
}
addUser[] public TestUser;
In the contract, “scofieldNew” has an array of struct “addUser”, which is a public variable where other contracts can access it. “addUser” struct has two fields “name” of type string and favoriteNumber of type uint. This demonstrates the use of arrays in Solidity.
Structs in Solidity
A struct is a user-defined type that contains a collection of named fields, each with its type. In this example, the “addUser” struct has two fields name and favoriteNumber of type string and unit, respectively.
struct addUser{
string name;
uint favoriteNumber;
}
Mapping in Solidity
Mapping is a data structure that allows you to store key-value pairs, where the key is a unique identifier, and the value is any data. In this example, the mapping stores the user’s name as the value and the user’s favourite number as the key.
mapping(uint256 => string) public newMappingForUser;
The provided code demonstrates inheritance, override, virtual, arrays, structs and mapping in the Solidity programming language.
These are essential concepts for developing smart contracts on the Ethereum blockchain and understanding how the code works together to accomplish different tasks.
In conclusion, the code provided demonstrates the use of several essential concepts in the Solidity programming language, including inheritance, overriding functions, arrays, structs and mapping.
Inheritance allows one contract to inherit the properties and methods of another contract, allowing for code reuse and the ability to build on existing functionality.
Using the “override” and “virtual” keywords allows for replacing or modifying functions in the base contract.
Arrays, structs and mapping are essential data structures that allow for the storage and manipulation of data in smart contracts.
The use of arrays allows storing multiple data elements of the same type, structs allow for creating user-defined types with multiple fields, and mapping allows storing key-value pairs.
Understanding and implementing these concepts is essential for developing smart contracts on the Ethereum blockchain and creating efficient and effective decentralized applications.
Follow the series here