0% found this document useful (0 votes)
4 views7 pages

Blockchain

The document contains Solidity code examples demonstrating various types of inheritance in smart contracts, including single, multilevel, hierarchical, and multiple inheritance. Each section includes a base contract and derived contracts, showcasing how state variables and functions are inherited and utilized. Additionally, there are calling contracts that test the inheritance functionality of the derived contracts.

Uploaded by

karanghare04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Blockchain

The document contains Solidity code examples demonstrating various types of inheritance in smart contracts, including single, multilevel, hierarchical, and multiple inheritance. Each section includes a base contract and derived contracts, showcasing how state variables and functions are inherited and utilized. Additionally, there are calling contracts that test the inheritance functionality of the derived contracts.

Uploaded by

karanghare04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment No.

Code:Single Inheritance
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

// Base contract
contract BaseContract {
string public baseMessage;

constructor() {
baseMessage = "Hello from BaseContract!";
}

function setBaseMessage(string memory _message) public {


baseMessage = _message;
}
}

// Derived contract
contract DerivedContract is BaseContract {
string public derivedMessage;

constructor() {
derivedMessage = "Hello from DerivedContract!";
}

function setDerivedMessage(string memory _message) public {


derivedMessage = _message;
}
}
Output:

Code: Multilevel Inheritance


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Base contract A
contract A {
string internal x;
string a = "Hello";
string b = "World";

function getA() external {


x = string(abi.encodePacked(a, b));
}
}

// Derived contract B inheriting from A


contract B is A {
string public y;
string c = "Solidity";

function getB() external payable returns (string memory) {


y = string(abi.encodePacked(x, c));
return y;
}
}

// Derived contract C inheriting from B


contract C is B {
function getC() external view returns (string memory) {
return y;
}
}

// Contract to test the inheritance


contract Caller {
C cc = new C();

function testInheritance() public returns (string memory) {


cc.getA();
cc.getB();
return cc.getC();
}
}

Output:

Code: Hierarchical Inheritance


// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

// Defining parent contract A


contract A {

// Declaring internal
// state variable
string internal x;

// Defining external function


// to set value of
// internalstate variable
function getA() external {
x = "abc";
}

// Declaring internal
// state variable
uint internal sum;

// Defining external function


// to set the value of
// internal state variable sum
function setA() external {
uint a = 10;
uint b = 20;
sum = a + b;
}
}

// Defining child contract B


// inheriting parent contract A
contract B is A {

// Defining external function to


// return state variable x
function getAstr(
) external view returns(string memory){
return x;
}

// Defining child contract C


// inheriting parent contract A
contract C is A {

// Defining external function to


// return state variable sum
function getAValue(
) external view returns(uint){
return sum;
}
}

// Defining calling contract


contract caller {

// Creating object of contract B


B contractB = new B();

// Creating object of contract C


C contractC = new C();

// Defining public function to


// return values of state variables
// x and sum
function testInheritance() public returns (string memory,
uint) {
return ( contractB.getAstr(), contractC.getAValue());
}
}

Output:

Code: Multiple Inheritance


// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract A {

// Declaring internal
// state variable
string internal x;

// Defining external function


// to set value of
// internal state variable x
function setA() external {
x = "GeeksForGeeks";
}
}

// Defining contract B
contract B {

// Declaring internal
// state variable
uint internal pow;

// Defining external function


// to set value of internal
// state variable pow
function setB() external {
uint a = 2;
uint 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 view returns(string memory) {
return x;
}

// Defining external function


// to return state variable pow
function getPow() external view returns(uint) {
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, uint) {
contractC.setA();
contractC.setB();
return (
contractC.getStr(), contractC.getPow());
}
}

Output:

You might also like