0% found this document useful (0 votes)
17 views17 pages

Lab Manual11 M Taha Majeed 198

Uploaded by

babarkhan79310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views17 pages

Lab Manual11 M Taha Majeed 198

Uploaded by

babarkhan79310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Object Oriented Programming

LAB 11

Lab Instructor: Jawad Hassan


Department of Artificial Intelligence
Email: [email protected]

SESSION: Spring-2024
SEMESTER: 2nd
Outlines
Lab - 1
Revised Programming Fundamentals
Lab – 2
 OOP Introduction
 Class
 Object
 Data Members
 Member Member Functions
Lab - 3
Unified Modeling Language to Code
 Class
 Object
 Data Members
 Member Member Functions
 Constructor
 Defualt
 Parametrize
Lab - 4
Code to the given Scenarios
Lab – 5
Code Practice
 Constructor
 Defualt
Parametrize
 Destructor
 Practice Task
 Quiz – 1
 Home Task
Lab – 6
Practice Entire Class
Lab – 7
 Passing Object into Function
 passing object by value
 passing object by reference
 Friend Function
Lab – 8
Class Relationships
 Association
 Aggregation
 Composition
 Inheritance
Lab – 9
MidTerm Examination
Lab – 10
 Mid-Term Review
 Object’s Array
Lab – 11
Inheritence
 Single Inheritance
 Multiple Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance

Inheritance
Inheritance in programming is like a child getting traits from their parents. It allows one class (child)
to use the properties and methods of another class (parent). This helps in reusing code and making
programs easier to manage.

There are several types of inheritance:

1. Single Inheritance: A child class inherits from one parent class.


- Example: A `Dog` class inherits from an `Animal` class.
2. Multiple Inheritance: A child class inherits from more than one parent class.
- Example: A `FlyingCar` class inherits from both `Car` and `Airplane` classes.

3. Multilevel Inheritance: A class inherits from a parent class, which in turn inherits from another
class.
- Example: A `Puppy` class inherits from `Dog`, and `Dog` inherits from `Animal`.

4.Hierarchical Inheritance: Multiple child classes inherit from the same parent class.
- Example: `Cat` and `Dog` classes both inherit from `Animal`.

5. Hybrid Inheritance: A combination of two or more types of inheritance.


- Example: A class structure that uses both multiple and multilevel inheritance.

Inheritance helps in organizing code and creating a clear structure, making it easier to add new
features and maintain the code.

Single Inheritance
A child class inherits from one parent class.
- Example: A `Dog` class inherits from an `Animal` class.

Code:
// Base class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};

// Derived class
class Dog : public Animal {
private:
string name;

public:
// Constructor to initialize the dog's name
Dog(string dogName) : name(dogName) {}

void bark() {
cout << name << " barks." << endl;
}

// Method to get the dog's name


string getName() {
return name;
}
};

Multiple Inheritance
A child class inherits from more than one parent class.
- Example: A `FlyingCar` class inherits from both `Car` and `Airplane` classes.

Code:
class Car {
public:
void drive() {
cout << "The car is driving on the road." << endl;
}
};
class Airplane {
public:
void fly() {
cout << "The airplane is flying in the sky." << endl;
}
};
class FlyingCar : public Car, public Airplane {
private:
string identifier;

public:

FlyingCar(string id) : identifier(id) {}


string getIdentifier() {
return identifier;
}

void display() {
cout << "FlyingCar ID: " << identifier << endl;
}
};

Multilevel Inheritance
A class inherits from a parent class, which in turn inherits from another class.
- Example: A `Puppy` class inherits from `Dog`, and `Dog` inherits from `Animal`.

Code:
class Animal {
protected:
string species;
public:
Animal(string sp) : species(sp) {}
void displaySpecies() {
cout << "Species: " << species << endl;}
};
class Dog : public Animal {
protected:
string breed;
public:
Dog(string sp, string br) : Animal(sp), breed(br) {}
void displayBreed() {
cout << "Breed: " << breed << endl;}
};
class Puppy : public Dog {
private:
string name;
public:
Puppy(string sp, string br, string nm) : Dog(sp, br), name(nm)
{}
void displayName() {
cout << "Name: " << name << endl;}
};

Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
- Example: `Cat` and `Dog` classes both inherit from `Animal`.

Code:

class Animal {
protected:
string name;
public:
Animal(string _name) : name(_name) {}
void makeSound() {
cout << "Some generic animal sound" << endl;
}
};

class Cat : public Animal {


public:
Cat(string _name) : Animal(_name) {}
void makeSound() {
cout << name << " says Meow!" << endl;
}
};

class Dog : public Animal {


public:
Dog(string _name) : Animal(_name) {}
void makeSound() {
cout << name << " says Woof!" << endl;
}
};

Hybrid Inheritance
A combination of two or more types of inheritance.
- Example: A class structure that uses both multiple and single inheritance.

Code:
class Animal {
protected:
string name;
public:
Animal(string _name) : name(_name) {}
void eat() {cout << name << " is eating." << endl;}
};
class Pet {
protected:
string owner;
public:
Pet(string _owner) : owner(_owner) {}
void play() {cout << "Playing with " << owner << "'s pet." <<
endl;}
};
class Cat : public Animal, public Pet {
public:
Cat(string _name,string _owner) : Animal(_name), Pet(_owner) {}
void purr() {cout << name << " is purring." <<endl;}
};

class Dog : public Animal {


public:
Dog(string _name) : Animal(_name) {}
void bark() {
cout << name << " is barking." << endl;}
};

Practice Task
Tasks for practicing different types of inheritance:

1. Single Inheritance:
Create a base class `Shape` with member functions `getArea()` and `getPerimeter()`. Derive a class
`Rectangle` from `Shape` and implement these functions to calculate the area and perimeter of a
rectangle.
Code:
#include<iostream>
using namespace std;
class Shape{
public:
double getArea(){
return 0;
}
double getParameter(){
return 0;
}
};
class Rectangle:public Shape{
private:
double length;
double width;
public:
Rectangle(double l,double w){
length=l;
width=w;
}
getArea(){
return length*width;
}
getParameter(){
return 2*(length+width);
}
};
int main(){
Rectangle r1(4,5);
cout<<"Area is :"<<r1.getArea()<<endl;
cout<<"Parameter is :"<<r1.getParameter();
return 0;
}

2. Multiple Inheritance:
Create two base classes `Employee` and `Student`. Derive a class `Assistant` from both `Employee`
and `Student`, combining attributes and behaviors from both classes.
Code:
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string empName;
int empID;
public:
Employee(string name, int id) : empName(name), empID(id) {}
void displayEmployeeInfo() {
cout << "Employee Name: " << empName << endl;
cout << "Employee ID: " << empID << endl;
}
};
class Student {
protected:
string studentName;
int studentID;
public:
Student(string name, int id) : studentName(name), studentID(id) {}
void displayStudentInfo() {
cout << "Student Name: " << studentName << endl;
cout << "Student ID: " << studentID << endl;
}
};

class Assistant : public Employee, public Student {


private:
string department;
public:
Assistant(string empName, int empID, string studentName, int studentID, string dept)
: Employee(empName, empID), Student(studentName, studentID), department(dept) {}

void displayAssistantInfo() {
displayEmployeeInfo();
displayStudentInfo();
cout << "Department: " << department << endl;
}
};

int main() {
Assistant assistant("Shahwar", 1001, "Rameen", 2021, "Computer Science");
assistant.displayAssistantInfo();

return 0;
}

3. Multilevel Inheritance:
Create a base class `Vehicle` with member functions `start()` and `stop()`. Derive a class `Car` from
`Vehicle`, which further serves as the base class for `SportsCar`. Implement `start()` and `stop()`
functions for both `Car` and `SportsCar`.
Code:
#include <iostream>
using namespace std;

class Vehicle {
public:
void start() {
cout << "Vehicle started." << endl;
}
void stop() {
cout << "Vehicle stopped." << endl;
}
};

class Car : public Vehicle {


public:
void carStart() {
start();
cout << "Car started." << endl;
}
void carStop() {
stop();
cout << "Car stopped." << endl;
}
};

class SportsCar : public Car {


public:
void sportsCarStart() {
carStart();
cout << "Sports car started." << endl;
}
void sportsCarStop() {
carStop();
cout << "Sports car stopped." << endl;
}
};

int main() {
SportsCar sportsCar1;
sportsCar1.sportsCarStart();
sportsCar1.sportsCarStop();
return 0;
}

4. Hierarchical Inheritance:
Create a base class `BankAccount` with member functions `deposit()` and `withdraw()`. Derive
two classes `CheckingAccount` and `SavingsAccount` from `BankAccount`. Implement specific
functionalities like overdraft protection for `CheckingAccount` and interest calculation for
`SavingsAccount`.
Code:
#include <iostream>
using namespace std;

class BankAccount {
protected:
double balance;
public:
BankAccount(){
balance=0;
}

void deposit(double amount) {


balance += amount;
cout << amount <<" Successfully deposited."<< endl;
cout << "Current balance: $" << balance << endl;
}

void withdraw(double amount) {


if (balance >= amount) {
balance -= amount;
cout <<amount << " Withdrawed successfully." << endl;
cout << "Current balance: $" << balance<< endl;
} else {
cout << "Insufficient funds!" << endl;
}
}
};

class CheckingAccount : public BankAccount {


private:
double overdraftLimit;
public:
CheckingAccount(double limit){
overdraftLimit=limit;
}

void withdraw(double amount) {


if (balance + overdraftLimit >= amount) {
balance -= amount;
cout << "Withdrawn: $" << amount << endl;
cout << "current balance: $" << balance << endl;
} else {
cout << "Withdrawal amount exceeds overdraft limit!" << endl;
}
}
};
class SavingsAccount : public BankAccount {
private:
double interestRate;
public:
SavingsAccount(double rate) {
interestRate=rate;
}

void addInterest() {
double interest = balance * (interestRate / 100);
balance += interest;
cout << "Interest added: $" << interest << endl;
cout << "current balance: $" << balance << endl;
}
};

int main() {
CheckingAccount c1(500);
SavingsAccount s1(2.5);
c1.deposit(1000);
c1.withdraw(800);
c1.withdraw(800);
s1.deposit(2000);
s1.addInterest();

return 0;
}

5. Hybrid Inheritance:
Design a class hierarchy for a school system. Create a base class `Person` with attributes like
`name`, `age`, and member functions `displayInfo()`. Derive classes `Teacher` and `Student` from
`Person`. Then, derive a class `Monitor` from both `Teacher` and `Student`, adding specific
functionalities like managing classroom activities and attendance.
Code:
#include <iostream>
#include <string>
using namespace std;

class Person {
protected:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}

void displayInfo() {
cout << "Name: " << name << "\nAge: " << age << endl;
}
};

class Teacher : virtual public Person {


private:
string subject;
public:
Teacher(string n, int a, string s) : Person(n, a), subject(s) {}

string getSubject() const {


return subject;
}

void displayInfo() {
Person::displayInfo();
cout << "Subject: " << subject << endl;
}
};
class Student : virtual public Person {
private:
string studentID;
public:
Student(string n, int a, string id) : Person(n, a), studentID(id) {}

string getStudentID() {
return studentID;
}

void displayInfo() {
Person::displayInfo();
cout << "Student ID: " << studentID << endl;
}
};

class Monitor : public Teacher, public Student {


private:
string responsibility;
public:
Monitor(string n, int a, string s, string id, string res)
: Person(n, a), Teacher(n, a, s), Student(n, a, id), responsibility(res) {}

void displayInfo() {
Person::displayInfo();
cout << "Subject: " << getSubject() << endl;
cout << "Student ID: " << getStudentID() << endl;
cout << "Responsibility: " << responsibility << endl;
}

void manageClassroomActivities() {
cout << name << " is managing classroom activities." << endl;
}

void manageAttendance() {
cout << name << " is managing attendance." << endl;
}
};

int main() {
Monitor monitor1("Hamza", 21, "Computer", "S2023", "Class Representative");
monitor1.displayInfo();
monitor1.manageClassroomActivities();
monitor1.manageAttendance();
return 0;
}

You might also like