Inheritance Assignment

After reading the article (very cool, btw), I figured that we should have the “selfdestruct” operation in function on a new file. The new contract should inherit the “onlyOwner” modifier from “Ownable”. And by getting an error on the compiler with the new function, I noticed that the address associated with “selfdestruct” should be a payable one (probably to send funds when the contract stops executing.
My new file “Destroyable”:

import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable{
    function closeContract() public onlyOwner{
        selfdestruct(msg.sender);
    }
}

And on the main file:

import "./Ownable.sol";
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract InheritanceTest is Ownable, Destroyable{
1 Like

Hi @9i9

You don’t really need to import HelloWorld.sol here as you are not using the contract in this file, also you need to make your Helloworld contract inherit from the destroyable contract you have created

1 Like

Hi @pedromndias
Great answer :+1:
Btw you can use a contract address which is not payable as a parameter of selfdestruct, using selfdestruct function is a trick often use to send eth to a contract which is not supposed to receive ether .

1 Like

In this inheritance assignment I am trying to add a list of names without writing code for each name. Is it best to add a .csv file with list of names.

#include <iostream>
#include <string>

using namespace std;

class Team{
        //private variables
        //if want to use variables outside use getter, see below
private:
        
        int age;
        string gender;
        int weight;
        string position;
     //so it is accessible only in the class itself
        //protected variables access only in the class
protected:
    string name;
    //string roster;
    
    
        
    
    //all variables should be private not public
    public:
        
        
        //constructor is Team
        Team(string n, int a, string g,string p, string r){
            
            name = n;
            age = a;
            gender = g;
            position = p;
            
            //because weight is private must be written inside constructor
            //not outside in the int main()
            weight = 300;
            //roster = r;
            
            
        }
        
        
        
        void printInfo(){
            cout << "The name of the<< player<<" is: "<<name<<endl;
            cout << "The age of the person is: "<<age<<endl;
            cout << "The gender of the person is: "<<gender<<endl;
            cout << "The weight of the person is: "<<weight<<" pounds "<<endl;
            cout << "Player's position is: "<<position<<endl;
            //cout << "This year's roster is: "<<roster<<endl;
        }
        void setWeight(int w){
            if(w>0){
                weight = w;
            }
            else{
                weight = 0;
            }
        }
        //to get weight from outside
        int getWeight(){
            return weight;
            
        }
        
 };       
        //inherited all attributes from above
        class Defense : public Team{
        public:
            int salary;
            Defense(string n, int a, int s, string g, string p): Team(n,a,g,p){
                salary = s;
            }
            void printSalary(){
                cout<<"Salary of "<< name << " is " << salary << " thousand dollars "<< "\n";
            }/*
            class Players : public Team {
    public:
        int player;
        
        Players(int player);
            Defense (string r): Team(n,a,g,p){
                
                player = r;
            }
            };
            void printPlayer(){
                cout<<"Position for "<< player <<" is: "<<"\n";
            }
            
        }*/
            
            
 
        };

        


/*
 * 
 */
int main() 
{
    //constructor object is fred
    //change constructor to inherit Defense
    Defense azeez = Defense("Azeez Al-Shaair", 21, 750000, "male", "Defensive Back");
    //fred.setWeight(2000);
    azeez.printInfo();
 
    
    //setting weight to 300 instead of 2000
    //fred.setWeight(300);
    //this is called a getter
    cout<<"Weight is "<<azeez.getWeight()<<"\n";
    azeez.printSalary();
    //Constructing w/o arguments
    //Team julie = Team();
    //julie.printInfo();
  
    return 0;
};

Edit: Btw this is not the right categoryPreformatted text

@cherrybluemoon


:wink:

Hey @gabba, thanks! And that sounds interesting… Didn’t know that trick was possible, since I got that compiler error.

1 Like

Hello Gabba,

Thank you for the correction. I realized only after. Thanks again!

:grinning:I have clicked on </> Isn’t this the right category inheritance?

You need to select your code first then click on the </> but it s all fine now :wink:
This topic if related to the solidity course, as you can see there is no answers related to c++

pragma solidity 0.5.12;
contract SelfDestruct{

address public owner;

modifier onlyOwner() {
require(msg.sender == owner);
_;
}

constructor() public {
owner = msg.sender;
}

function destroy() public onlyOwner { 

selfdestruct(msg.sender);
}
}
and in HelloWorld

import “./selfDestruct.sol”;

1 Like

To the ‘HelloWorld’ contract:

  1. At top of file, insert the line ‘import “./Destroyable.sol”;’
  2. Modify contract line to ‘contract HelloWorld is Ownable, Destroyable’.

Create a new contract called ‘Destroyable.sol’:

pragma solidity 0.5.12;
import "./Ownable.sol";

contract Destroyable is Ownable
{
    function DestroyContract () public onlyOwner 
     { 
         address payable receiverOfFunds = msg.sender;
         selfdestruct(receiverOfFunds);  // `owner` is the owners address
    }
}
1 Like

I create :slight_smile:

import “./Ownable.sol”;

pragma solidity 0.5.12;

contract Destroyable is Ownable {

function close() public onlyOwner { //onlyOwner is custom modifier
   selfdestruct(msg.sender);  // `` is the owners address
}

}

hello world :slight_smile:
import “./Ownable.sol”;
import “./Destroyable.sol”;

pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable {

struct Person {
  uint id;
  string name;
  uint age;
  uint height;
  bool senior;
}

event personCreated(string name, bool senior);
event personDeleted(string name, bool senior, address deletedBy);


uint public balance;




modifier costs(uint cost){
    require(msg.value >= cost);
    _;
}



mapping (address => Person) private people;
address[] private creators;

function createPerson(string memory name, uint age, uint height) public payable costs(1 ether){
  require(age < 150, "Age needs to be below 150");
  require(msg.value >= 1 ether);
  balance += msg.value;

    //This creates a person
    Person memory newPerson;
    newPerson.name = name;
    newPerson.age = age;
    newPerson.height = height;

    if(age >= 65){
       newPerson.senior = true;
   }
   else{
       newPerson.senior = false;
   }

    insertPerson(newPerson);
    creators.push(msg.sender);

    assert(
        keccak256(
            abi.encodePacked(
                people[msg.sender].name,
                people[msg.sender].age,
                people[msg.sender].height,
                people[msg.sender].senior
            )
        )
        ==
        keccak256(
            abi.encodePacked(
                newPerson.name,
                newPerson.age,
                newPerson.height,
                newPerson.senior
            )
        )
    );
    emit personCreated(newPerson.name, newPerson.senior);
}
function insertPerson(Person memory newPerson) private {
    address creator = msg.sender;
    people[creator] = newPerson;
}
function getPerson() public view returns(string memory name, uint age, uint height, bool senior){
    address creator = msg.sender;
    return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
}
function deletePerson(address creator) public onlyOwner {
  string memory name = people[creator].name;
  bool senior = people[creator].senior;

   delete people[creator];
   assert(people[creator].age == 0);
   emit personDeleted(name, senior, owner);

}
function getCreator(uint index) public view onlyOwner returns(address){
return creators[index];
}

function withdrowALL() public onlyOwner returns(uint) {
uint toTransfer = balance;
balance = 0;
msg.sender.transfer(balance);
return(toTransfer);
}

}

Ownable file :

pragma solidity 0.5.12;

contract Ownable {

    address public owner;

    modifier onlyOwner(){
    require(msg.sender == owner);
    _; //Continue execution
}

    constructor() public{
    owner = msg.sender;
}

}

Seems to work properly

1 Like

import “./Ownable.sol”;
pragma solidity 0.5.12;
contract Destroyable is Ownable{
function destroycontract() public onlyOwner {
self destruct(address(uint160(owner)));
}
}

1 Like

I spent way too long struggling with why this would not compile:

import “./Ownable.sol”;
pragma solidity 0.5.12;

contract Destroyable is Ownable{

function killKill() public onlyOwner {
    selfdestruct(owner);
} 

}

I finally figured out the compiling issue with owner(not a payable address) and simply inserted msg.sender instead(which is a payable address). I see that Guy and Filip already talked about this on the forum (and gave a more in-depth explanation). Anyway, here’s my fixed function:

function killKill() public onlyOwner {
selfdestruct(msg.sender);
}

1 Like

Made in Ownable the owner member ‘address payable’ and added
Destroyable.sol

import “./Ownable.sol”;

pragma solidity 0.5.12;

contract Destroyable is Ownable {

function destroy() public onlyOwner() {
    selfdestruct(owner);
}

}

1 Like
import "./Ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable{
    
    function destroy() public onlyOwner {
        address payable owner_payable = address(uint160(owner));
        selfdestruct(owner_payable);
    }
}

Edit @ivga80 ( Hi, you can use the preformatted text button to show the code when you post. :slightly_smiling_face:).

1 Like

Contract Destroyable:

import "./ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable{
    
    function destroy() public onlyOwner {
        selfdestruct(owner);
    }
}

Import in HelloWorld

import "./ownable.sol";
import "./destroyable.sol";

pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable{
....
}
1 Like

pragma solidity 0.5.12;

import “./Ownable.sol”;

contract Destoryable is Ownable{

function destroy() public onlyOwner { 
selfdestruct(msg.sender); 

}

}

1 Like

Destructable.sol

import "./Ownable.sol";

pragma solidity 0.5.12;

contract Destructable is Ownable {
    function close() public onlyOwner {
      selfdestruct(address(uint160(owner))); 
    }
}

HelloWorld.sol

import "./Ownable.sol";
import "./Destructable.sol";
pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destructable {
2 Likes

I used this, but not sure how this contract will destroy and send the fund balance to the owner, is this in the selfdestruct function ?
Hope its explained in the solution Thanks

pragma solidity 0.5.12;

contract Destroy{
address public owner;

modifier onlyOwner(){
    selfdestruct(msg.sender == owner);
    _; //Continue execution
}
1 Like