0% found this document useful (0 votes)
5 views3 pages

Example

Uploaded by

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

Example

Uploaded by

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

#include <iostream>

#include <string>
using namespace std;

class Employee {
private:
int id;
string name;

public:
Employee(int id, string name) {
this->id = id;
this->name = name;
}

int getId() { return id; }


string getName() { return name; }

void setId(int id) { this->id = id; }


void setName(string name) { this->name = name; }

void displayInfo() {
cout << "Employee ID: " << id << ", Name: " << name << endl;
}
};

class University {
private:
int id;
string name;
Employee* employee; // Aggregation

public:
University(int id, string name, Employee* emp) {
this->id = id;
this->name = name;
this->employee = emp;
}

int getId() { return id; }


string getName() { return name; }
void setId(int id) { this->id = id; }
void setName(string name) { this->name = name; }

void displayInfo() {
cout << "University ID: " << id << ", Name: " << name << endl;
cout << "Associated Employee: ";
employee->displayInfo();
}
};

int main() {
Employee emp(1, "Alice");
University uni(101, "OpenAI University", &emp);

uni.displayInfo();

// Update values using setters


emp.setName("Alice Smith");
uni.setName("AI Institute");

cout << "\nAfter updates:\n";


uni.displayInfo();

return 0;
}

Terminate the scope of UNI

#include <iostream>
#include <string>
using namespace std;

class Employee {
private:
int id;
string name;

public:
Employee(int id, string name) {
this->id = id;
this->name = name;
}

int getId() { return id; }


string getName() { return name; }

void setId(int id) { this->id = id; }


void setName(string name) { this->name = name; }

void displayInfo() {
cout << "Employee ID: " << id << ", Name: " << name << endl;
}
};

class University {
private:
int id;
string name;
Employee* employee; // Aggregation

public:
University(int id, string name, Employee* emp) {
this->id = id;
this->name = name;
this->employee = emp;
}

int getId() { return id; }


string getName() { return name; }
void setId(int id) { this->id = id; }
void setName(string name) { this->name = name; }

void displayInfo() {
cout << "University ID: " << id << ", Name: " << name << endl;
cout << "Associated Employee: ";
employee->displayInfo();
}
~University() {
cout << "University object '" << name << "' is destroyed." << endl;
}
};

int main() {
Employee emp(1, "Alice");

{
// Begin scoped block
University uni(101, "OpenAI University", &emp);
uni.displayInfo();

// Update values using setters


emp.setName("Alice Smith");
uni.setName("AI Institute");

cout << "\nAfter updates:\n";


uni.displayInfo();
} // <-- `uni` goes out of scope here and its destructor is called

cout << "\nAfter university scope ends:\n";


emp.displayInfo(); // Employee still exists

return 0;
}
Imagine you hire a teacher (the Employee) and temporarily assign them to a
university (University) for a project.

The teacher (employee) exists on their own — they don’t depend on any one
university to exist.

The university (uni) is created inside a limited-time project (a block of code).

Once the project ends, the university closes down — it goes out of scope and is
destroyed.

But the teacher is still there! They weren’t destroyed because they weren't owned
by the university — they just worked there temporarily.

That’s aggregation: the university knows about the employee and uses them, but it
doesn’t own or manage their life. So when the university is destroyed, the employee
stays alive.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy