OOPS Assignment
Q1. Based on Class, Object, Constructor, Methods
<?php
class Employee {
public $Eno;
public $Ename;
public $Designation;
public function __construct($Eno, $Ename, $Designation) {
$this->Eno = $Eno;
$this->Ename = $Ename;
$this->Designation = $Designation;
}
public function displayDetails() {
echo "Employee Number: " . $this->Eno . "\n";
echo "Employee Name: " . $this->Ename . "\n";
echo "Designation: " . $this->Designation . "\n\n";
}
}
$emp1 = new Employee(101, "Jagbir Singh", "Software Developer");
$emp2 = new Employee(102, “Jaspreet Singh", "Project Manager");
$emp1->displayDetails();
$emp2->displayDetails();
?>
Q2. Based on Inheritance
<?php
class Person {
public $Name;
public $Age;
}
class Faculty extends Person {
public $Subject;
public $Salary;
public function __construct($Name, $Age, $Subject, $Salary) {
$this->Name = $Name;
$this->Age = $Age;
$this->Subject = $Subject;
$this->Salary = $Salary;
}
public function showDetails() {
echo "Name: " . $this->Name . "\n";
echo "Age: " . $this->Age . "\n";
echo "Subject: " . $this->Subject . "\n";
echo "Salary: ₹" . $this->Salary . "\n\n";
}
}
$faculty1 = new Faculty("Jagbir Singh", 22, "Computer Science", 75000);
$faculty2 = new Faculty("Jaspreet Singh", 21, "Information Technology", 72000);
$faculty1->showDetails();
$faculty2->showDetails();
?>
Q3. Based on Polymorphism (Method Overriding)
<?php
class Shape {
public function area() {
return 0;
}
}
class Circle extends Shape {
private $radius;
public function __construct($r) {
$this->radius = $r;
}
public function area() {
return 3.14 * $this->radius * $this->radius;
}
}
class Rectangle extends Shape {
private $length;
private $breadth;
public function __construct($l, $b) {
$this->length = $l;
$this->breadth = $b;
}
public function area() {
return $this->length * $this->breadth;
}
}
$circle = new Circle(5);
$rectangle = new Rectangle(4, 6);
echo "Area of Circle: " . $circle->area() . "<br>";
echo "Area of Rectangle: " . $rectangle->area();
?>
Q4. Based on Encapsulation, Access Modifiers
<?php
class BankAccount {
private $accountNumber;
private $balance;
public function setAccountNumber($num) {
$this->accountNumber = $num;
}
public function getAccountNumber() {
return $this->accountNumber;
}
public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
}
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount();
$account->setAccountNumber("123456789");
$account->deposit(5000);
echo "Account Number: " . $account->getAccountNumber() . "<br>";
echo "Balance: " . $account->getBalance();
?>
Q5. Based on Abstract Class and Interface
<?php
abstract class Vehicle {
abstract public function start();
}
interface Mileage {
public function showMileage();
}
class Car extends Vehicle implements Mileage {
public function start() {
echo "Car started\n";
}
public function showMileage() {
echo "Car mileage: 18 km/l\n";
}
}
class Bike extends Vehicle implements Mileage {
public function start() {
echo "Bike started\n";
}
public function showMileage() {
echo "Bike mileage: 45 km/l\n";
}
}
$car = new Car();
$bike = new Bike();
$car->start();
$car->showMileage();
$bike->start();
$bike->showMileage();
?>