CPP OOP: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 10: Line 10:
using namespace std;
using namespace std;


namespace Chorke{
namespace Chorke {
     namespace Academia {
     namespace Academia {
         namespace Core {
         namespace Core {
Line 80: Line 80:
#include "Employee.h"
#include "Employee.h"


Core::Employee::Employee(){
Core::Employee::Employee() {
     m_name = "";
     m_name = "";
     m_age = 0;
     m_age = 0;
}
}


Core::Employee::Employee(string name, int age){
Core::Employee::Employee(string name, int age) {
     m_name = name;
     m_name = name;
     m_age = age;
     m_age = age;
Line 108: Line 108:
}
}


string Core::Employee::greeting(){
string Core::Employee::greeting() {
     string hello = "Hello, Mr. ";
     string hello = "Hello, Mr. ";
     return hello + m_name;
     return hello + m_name;
}
}


Core::Employee::~Employee(){
Core::Employee::~Employee() {
     //delete &m_name;
     //delete &m_name;
     //delete &m_age;
     //delete &m_age;
Line 123: Line 123:
#include "Doctor.h"
#include "Doctor.h"


Core::Doctor::Doctor(const Employee& emp){
Core::Doctor::Doctor(const Employee& emp) {
     m_emp = emp;
     m_emp = emp;
}
}


string Core::Doctor::greeting(){
string Core::Doctor::greeting() {
     string hello = "Hello, Dr. ";
     string hello = "Hello, Dr. ";
    //cout << "" + m_emp.greeting() << endl;
    //cout << "Age:" << m_emp.Age() << endl;
     return hello + m_emp.Name();
     return hello + m_emp.Name();
}
}


Core::Doctor::~Doctor(){
Core::Doctor::~Doctor() {
     //delete &m_emp;
     //delete &m_emp;
}
}
Line 149: Line 147:
using namespace Chorke::Academia::Core;
using namespace Chorke::Academia::Core;


int main(){
int main() {
     Employee emp("Academian", 30);
     Employee emp("Academian", 30);
     Doctor doc(emp);
     Doctor doc(emp);
Line 158: Line 156:
}
}
</source>
</source>
==References==
* [https://stackoverflow.com/questions/57483 Differences between a pointer variable and a reference variable]
* [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://www.learncpp.com/cpp-tutorial/812-static-member-functions/ Static Member Functions]
* [[Conan Client]]

Latest revision as of 21:18, 24 July 2021

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;
}

References