0% found this document useful (0 votes)
24 views8 pages

Lab Manual 8

The document outlines a lab session on Object-Oriented Programming (OOP) focusing on concepts such as class relationships, including association, aggregation, composition, and inheritance. It includes practical coding examples for each relationship type and tasks for students to design class hierarchies for various systems like a car dealership and a library. The lab is conducted by Jawad Hassan in the Spring 2024 semester at the Department of Artificial Intelligence.

Uploaded by

sm1385442
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)
24 views8 pages

Lab Manual 8

The document outlines a lab session on Object-Oriented Programming (OOP) focusing on concepts such as class relationships, including association, aggregation, composition, and inheritance. It includes practical coding examples for each relationship type and tasks for students to design class hierarchies for various systems like a car dealership and a library. The lab is conducted by Jawad Hassan in the Spring 2024 semester at the Department of Artificial Intelligence.

Uploaded by

sm1385442
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/ 8

OOP – |Passing Object into Function|Friend Function|

Object Oriented Programming


LAB 08

Lab Instructor: Jawad Hassan


Department of Artificial Intelligence
Email: [email protected]

SESSION: Spring-2024
SEMESTER: 2nd
OOP – Class | Object | Data Members |Member Functions |
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
OOP – Class | Object | Data Members |Member Functions |
Class Relationships
 Association
 Aggregation
 Composition
 Inheritance
OOP – Class | Object | Data Members |Member Functions |
Class Relationships
In Object-Oriented Programming (OOP), the concept of class relationships refers to how classes
interact and relate to each other. There are several types of relationships commonly seen in OOP.

Association
Association represents a relationship between two or more classes where objects of one class are
related to objects of another class. This relationship can be one-to-one, one-to-many, or many-to-
many.

Code:
class B {
public:
B() {}
void someFunction() {
cout << "Function in class B" << endl;
}
};

class A {
private:
OOP – Class | Object | Data Members |Member Functions |
B bInstance; // Instance of class B
public:
A()
{
}

void someFunctionOfB() {
bInstance.someFunction();
}
};

Aggregation
Aggregation is a specialized form of association where objects have a "has-a" relationship. It
represents a whole-part or a part-of relationship. In aggregation, one class is composed of one or
more classes, but the contained classes can exist independently.

Code:
class Engine {
private:
string type;

public:
Engine()
{
type = " ";
}
Engine(string engineType)
{
type = engineType;
}

void start() {
cout << "Starting " << type << " engine..." << endl;
}
};

class Car {
private:
Engine carEngine;
OOP – Class | Object | Data Members |Member Functions |
public:
Car(string engineType) {
carEngine = Engine(engineType);
}

void start() {
carEngine.start();
cout << "Car started." << endl;
}
};

Composition
Composition is a stronger form of aggregation where one class is composed of one or more classes,
and the composed classes have a strong lifecycle dependency on the container class. If the container
class is destroyed, all the contained classes are also destroyed.

Code:
class Heart {
private:
string heartbeat;
public:
Heart()
{
heartbeat = "normal";
}

void beat() {
cout << "Heart beating " << heartbeat << "." << endl;
}
};

class Human {
private:
Heart humanHeart;
public:
Human() {}
OOP – Class | Object | Data Members |Member Functions |
void heartbeat() {
humanHeart.beat();
}
};

Inheritance
Inheritance is a mechanism where a new class (subclass or derived class) is derived from an existing
class (superclass or base class). The subclass inherits the properties and behaviors (methods) of the
superclass and can also have its own additional properties and behaviors.

Practice Task
1. Design a class hierarchy representing a car dealership inventory system. Create classes for Car
and Dealership. Each Dealership can have multiple Cars. Implement methods to add cars to the
dealership and display all cars in the inventory.

2. Create two classes representing a Library and a Book. The Library class should have a method
OOP – Class | Object | Data Members |Member Functions |
to add books, and the Book class should have attributes like title, author, and publication year.
Implement a method in the Library class to display all the books it contains.

3. Design a class hierarchy representing an employee management system. Create classes for
Company and Employee. Each Company can have multiple Employees. Implement methods to
add employees to the company and display all employees in the company.

4. Define two classes representing a Student and a Course. Each Student can enroll in multiple
courses, and each Course can have multiple students enrolled. Implement methods to add
students to courses and display all students enrolled in a particular course.

5. Design a class called House, which contains multiple Rooms. Each Room can have multiple
pieces of Furniture. Implement methods to add furniture to rooms and display all furniture for a
given room.

You might also like