I'm in a bit of trouble understanding how to create and extend an abstract class. I am doing an assignment for school at the moment - and am also having trouble understanding what the teacher wants me to do at some parts. I'm going to try to keep this brief and to the point.
I will post two classes, with my instructions for each (which I'd also appreciate your opinion on).
Instructions for my abstract class:
Quote
It should have 3 variables.
Fornafn, eftirnafn, and titill (first name, last name, and title, respectively).
And getters and setters for each.
The constructor in the function should take in all the variables. (I presume he meant to say class).
The class should also implement the magic function __toString() which prints out his combined name and his title. It should also define the abstract functions tekjur() (income)
My attempt at making said abstract class: - I try to put questions in the comments where appropriate.
abstract class Starfsmadur
{
//My variables - why should an abstract class have these?
private $fornafn; //first name
private $eftirnafn; //last name
private $titill; //title
//Getters and setters for the variables, should these be abstract public functions?
public function setFornafn($fornafn)
{
$this->fornafn = $fornafn;
}
public function getFornafn()
{
return $this->fornafn;
}
public function setEftirnafn($eftirnafn)
{
$this->eftirnafn = $eftirnafn;
}
public function getEftirnafn()
{
return $this->eftirnafn;
}
public function setTitill($titill)
{
$this->titill = $titill;
}
public function getTitill()
{
return $this->titill;
}
//The constructor that takes in the variables
public function __construct($fornafn, $eftirnafn, $titill)
{
$this->setFornafn($fornafn);
$this->setEftirnafn($eftirnafn);
$this->setTitill($titill);
}
//The __toString() function
public function __toString()
{
return "Nafn: " . $this->fornafn . " " . $this->eftirnafn . " Titill: " . $this->titill;
}
//The abstract function tekjur()
protected abstract function tekjur();
}
Now for the class that I created that attempts to extend the abstract class.
My instructions:
Quote
It inherits starfsmadur (employee) and has one variable, laun (salary).
Its constructor should take in fornafn, eftirnafn, and laun. (first name, last name, and salary, respectively.)
Laun (salary) is of the type int.
Implement the __toString() function and the ones needed for the laun (salary).
The code: Again I try to put questions in the comments where appropriate.
class Forstjori extends Starfsmadur
{
//The only variable I'm supposed to have in this class
private $laun; //salary
function setLaun($laun) //setSalary
{
$this->laun = $laun;
}
function getLaun() //getSalary
{
return $this->laun;
}
//This function, as instructed, is supposed to take in the variables
//fornafn, eftirnafn, and laun (first name, last name, and salary)
//Note that fornafn & eftirnafn only exist in the abstract parent class
function __construct($fornafn, $eftirnafn, $laun)
{
//parent::__construct($fornafn, $eftirnafn);
//The line above won't work - I have no title parameter nor was I instructed to have one.
//Perhaps I should create setters and getters for fornafn and eftirnafn somhow
//And call the setters/getters in the abstract class?
$this->setLaun($laun);
}
//The tekjur function (income)
//I have no clue what to do with this one, it is abstract
//As I was instructed to do, but as to what to do with it, I haven't the faintest idea
function tekjur()
{
//Return laun?
}
//Implementing (although not really)? the __toString() function
//Was I supposed to make it public abstract?
function __toString()
{
parent::__toString() . " tekjur eru $this->laun";
}
}
I would really appreciate it if someone could point me in the right direction, considering the instructions. A fair bit of the variables / functions were in Icelandic but I tried to repeat them as often as possible in English to avoid confusion.

New Topic/Question
Reply



MultiQuote





|