program inheritanceExample;
{$MODE OBJFPC} //directive to be used for creating classes
{$M+} //directive that allows class constructors and destructors
constructor Create(t : String; p: real); //default constructor
procedure setTitle(t : String); //sets title for a book
function getTitle() : String; //retrieves title
procedure setPrice(p : real); //sets price for a book
function getPrice() : real; //retrieves price
procedure Display(); virtual; // display details of a book
(* Creating a derived class *)
constructor Create(t: String); overload;
constructor Create(a: String; t: String; p: real); overload;
procedure setAuthor(a: String); // sets author for a book
function getAuthor(): String; // retrieves author name
procedure Display(); override;
constructor Books.Create(t : String; p: real);
procedure Books.setTitle(t : String); //sets title for a book
function Books.getTitle() : String; //retrieves title
procedure Books.setPrice(p : real); //sets price for a book
function Books.getPrice() : real; //retrieves price
procedure Books.Display();
writeln('Title: ', title);
writeln('Price: ', price);
(* Now the derived class methods *)
constructor Novels.Create(t: String);
inherited Create(t, 0.0);
constructor Novels.Create(a: String; t: String; p: real);
procedure Novels.setAuthor(a : String); //sets author for a book
function Novels.getAuthor() : String; //retrieves author
procedure Novels.Display();
writeln('Title: ', title);
writeln('Price: ', price:5:2);
writeln('Author: ', author);
n1 := Novels.Create('Gone with the Wind');
n2 := Novels.Create('Ayn Rand','Atlas Shrugged', 467.75);
n1.setAuthor('Margaret Mitchell');