CPP OOP: Difference between revisions
Jump to navigation
Jump to search
Line 158: | Line 158: | ||
==References== | ==References== | ||
* [https://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/ Returning values by value, reference, and address] | |||
* [https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in Pointer <code>*</code> vs Reference <code>&</code>] | * [https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in Pointer <code>*</code> vs Reference <code>&</code>] | ||
* [https://www.learncpp.com/cpp-tutorial/812-static-member-functions/ Static Member Functions] | * [https://www.learncpp.com/cpp-tutorial/812-static-member-functions/ Static Member Functions] |
Revision as of 18:56, 22 March 2020
Headers
Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <string>
using namespace std;
namespace Chorke {
namespace Academia {
namespace Core {
class Employee;
}
}
}
namespace Core = Chorke::Academia::Core;
class Core::Employee {
public:
Employee();
Employee(string name, int age);
virtual ~Employee();
string greeting();
const string& Name() const;
const Employee& Name(const string& name);
const int& Age() const;
const Employee& Age(const int& age);
protected:
private:
string m_name;
int m_age;
};
#endif // EMPLOYEE_H
Doctor.h
#ifndef DOCTOR_H
#define DOCTOR_H
#include <employee.h>
#include <iostream>
#include <string>
using namespace std;
namespace Chorke {
namespace Academia {
namespace Core {
class Doctor;
}
}
}
namespace Core = Chorke::Academia::Core;
class Core::Doctor {
public:
Doctor(const Employee& emp);
virtual ~Doctor();
string greeting();
protected:
private:
Employee m_emp;
};
#endif // DOCTOR_H
Source
Employee.cpp
#include "Employee.h"
Core::Employee::Employee() {
m_name = "";
m_age = 0;
}
Core::Employee::Employee(string name, int age) {
m_name = name;
m_age = age;
}
const string& Core::Employee::Name() const {
return m_name;
}
const Core::Employee& Core::Employee::Name(const string& name) {
m_name = name;
return *this;
}
const int& Core::Employee::Age() const {
return m_age;
}
const Core::Employee& Core::Employee::Age(const int& age) {
m_age = age;
return *this;
}
string Core::Employee::greeting() {
string hello = "Hello, Mr. ";
return hello + m_name;
}
Core::Employee::~Employee() {
//delete &m_name;
//delete &m_age;
}
Doctor.cpp
#include "Doctor.h"
Core::Doctor::Doctor(const Employee& emp) {
m_emp = emp;
}
string Core::Doctor::greeting() {
string hello = "Hello, Dr. ";
return hello + m_emp.Name();
}
Core::Doctor::~Doctor() {
//delete &m_emp;
}
Main.cpp
#include <Employee.h>
#include <Doctor.h>
#include <iostream>
#include <string>
using namespace std;
using namespace Chorke::Academia::Core;
int main() {
Employee emp("Academian", 30);
Doctor doc(emp);
cout << "Hello, Academian!" << endl;
cout << "" + emp.greeting() << endl;
cout << "" + doc.greeting() << endl;
return 0;
}