consolidate all repos to one for archive
This commit is contained in:
27
semester_2/programiranje_2/primeri/Example10/Company.h
Normal file
27
semester_2/programiranje_2/primeri/Example10/Company.h
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
#ifndef EXAMPLE10_COMPANY_H
|
||||
#define EXAMPLE10_COMPANY_H
|
||||
|
||||
#include <iostream>
|
||||
#include "Employee.h"
|
||||
|
||||
class Company {
|
||||
private:
|
||||
std::string name;
|
||||
Employee* ptrEmployee; // aggregation
|
||||
public:
|
||||
Company(std::string cname, Employee* ename) : name(cname), ptrEmployee(ename) {
|
||||
std::cout << "Company::constructor" << std::endl;
|
||||
}
|
||||
virtual ~Company() {
|
||||
std::cout << "Company::destructor" << std::endl;
|
||||
}
|
||||
virtual const std::string& toString() const {
|
||||
return name;
|
||||
};
|
||||
virtual void employed() const {
|
||||
std::cout << ptrEmployee->toString();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE10_COMPANY_H
|
22
semester_2/programiranje_2/primeri/Example10/Employee.h
Normal file
22
semester_2/programiranje_2/primeri/Example10/Employee.h
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
#ifndef EXAMPLE10_EMPLOYEE_H
|
||||
#define EXAMPLE10_EMPLOYEE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Employee {
|
||||
private:
|
||||
std::string name;
|
||||
public:
|
||||
Employee(std::string name) : name(name) {
|
||||
std::cout << "Employee::constructor" << std::endl;
|
||||
}
|
||||
virtual ~Employee() {
|
||||
std::cout << "Employee::destructor" << std::endl;
|
||||
}
|
||||
virtual const std::string& toString() const {
|
||||
return name;
|
||||
};
|
||||
};
|
||||
|
||||
#endif //EXAMPLE10_EMPLOYEE_H
|
25
semester_2/programiranje_2/primeri/Example10/main.cpp
Normal file
25
semester_2/programiranje_2/primeri/Example10/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include "Employee.h"
|
||||
#include "Company.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Aggregation example" << std::endl;
|
||||
{
|
||||
std::cout << "----------- nested block 1 begin -----------" << std::endl;
|
||||
Employee* ptrEmp = new Employee("John");
|
||||
{
|
||||
std::cout << "----------- nested block 2 begin -----------" << std::endl;
|
||||
Company c("SmartCo", ptrEmp);
|
||||
std::cout << "Employee ";
|
||||
c.employed();
|
||||
std::cout << " works for company " << c.toString() << std::endl;
|
||||
std::cout << "----------- nested block 2 end -----------" << std::endl;
|
||||
}
|
||||
std::cout << "Company doesn't exist anymore" << std::endl;
|
||||
std::cout << "But, employee " << ptrEmp->toString();
|
||||
std::cout << " still exists!" << std::endl;
|
||||
std::cout << "----------- nested block 1 end -----------" << std::endl;
|
||||
delete ptrEmp;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user