Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

abstract function
d
communitycreator

What is an abstract function in D?

Chinweuba Elijah Azubuike

What is a function?

Functions are code blocks that carry out certain instructions in an application. These blocks of code can be called multiple times during the runtime of the application. These blocks of code are reusable.

What is an abstract function?

Abstract functions are instantiated in a derived class but declared in a base class. Let’s see an example.

Example

import std.stdio; 
import std.string; 
import std.datetime; 
 
abstract class Person { 
   int dob; 
   string name; 
  //abstract class
   abstract void print(); 
}
class Employee : Person { 
   int empID;  
   
   override void print() { 
      writeln("The employee details are as follows:"); 
      writeln("Emp ID: ", this.empID); 
      writeln("Emp Name: ", this.name);
       writeln("Date of Borth: ", this.dob); 
    
   } 
} 

void main() { 
   Employee emp = new Employee(); 
   emp.empID = 101; 
   emp.dob = 1980; 
 
   emp.name = "Emp1"; 
   emp.print(); 
}
Abstract class in D programming

Note: Please read this shot to learn about inheritance in D. You’ll learn about what goes on in the code example aside from the abstract function we’re looking at in this shot.

Explanation

  • Line 9: We declare our function. The abstract keyword shows that this function is an abstract function. Also, observe that we only create the function. We do not write anything in it.

  • Line 11: We inherit the Person class that allows us to access the abstract function.

  • Lines 14-20: We define the properties of the print() function in Employee class.

  • Line 24: We create an Employee class instance.

  • Line 29: We call the print() function on the created instance to print its features. The print() function prints the details of the employee.

RELATED TAGS

abstract function
d
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring