Programming with C++ - Day5 || Wong Edition || B.Tech Diaries

Lab #5 - Inheritance and Over-Riding

Question 1 - Company Hierarchy

In a Company Hierarchy consisting of Clerk, Manager and Director, the invoice approval limits are as follows:-

  • Clerk - $100
  • Manager - $500
  • Director - $1000

The UML Class Diagram for the Clerk is as shown.

Clerk
- name : String
- designation: String
* getName : String
* getDesignation : String
* introduce() : String
* approveInvoice(in invoiceAmount:double) : boolean
  • Depict the Company Hierarchy in a UML Class Diagram.
  • Implement the Company Hierarchy. Over-Ride the approveInvoice function to ensure the approval limits are adhered to. [Hint: create the main() loop in the Director class and create instances of Clerk, Manager and Director]

Solution:


#include <iostream>

/**
    * @brief Class Clerk
*/
class Clerk
{
private:
    std::string name, designation;

public:
    Clerk(std::string);
    Clerk(std::string, std::string);
    ~Clerk();
    std::string getName();
    std::string getDesignation();
    std::string introduce();
    bool approveInvoice(double);
};

Clerk::Clerk(std::string name)
{
    this->name = name;
    this->designation = "Clerk";
}

Clerk::Clerk(std::string name, std::string designation)
{
    this->name = name;
    this->designation = designation;
}

Clerk::~Clerk()
{
}

std::string Clerk::getName()
{
    return this->name;
}

std::string Clerk::getDesignation()
{
    return this->designation;
}

std::string Clerk::introduce()
{
    return "My name is " + this->name + " and I am a " + this->designation;
}

bool Clerk::approveInvoice(double amount)
{
    if (amount <= 100)
        return true;
    else
        return false;
}

/**
    * @brief Class Manager
*/

class Manager : public Clerk
{
private:
public:
    Manager(std::string);
    Manager(std::string, std::string);
    ~Manager();
    bool approveInvoice(double);
};

Manager::Manager(std::string name) : Clerk(name, "Manager")
{
}

Manager::Manager(std::string name, std::string designation) : Clerk(name, designation)
{
}

Manager::~Manager()
{
}

bool Manager::approveInvoice(double amount)
{
    if (amount <= 500)
        return true;
    else
        return false;
}

/**
    * @brief Class Manager
*/

class Director : public Manager
{
private:
public:
    Director(std::string);
    ~Director();
    bool approveInvoice(double);
};

Director::Director(std::string name) : Manager(name, "Director")
{
}

Director::~Director()
{
}

bool Director::approveInvoice(double amount)
{
    if (amount <= 1000)
        return true;
    else
        return false;
}

// program 1
int main(int argc, char const *argv[])
{
    Clerk clerk("John");
    Manager manager("Jane");
    Director director("Jack");

    std::cout << clerk.introduce() << std::endl;
    std::cout << manager.introduce() << std::endl;
    std::cout << director.introduce() << std::endl;

    std::cout << (clerk.approveInvoice(77) ? "True" : "False") << std::endl;
    std::cout << (clerk.approveInvoice(100) ? "True" : "False") << std::endl;
    std::cout << (clerk.approveInvoice(124) ? "True" : "False") << std::endl;

    std::cout << (manager.approveInvoice(427) ? "True" : "False") << std::endl;
    std::cout << (manager.approveInvoice(500) ? "True" : "False") << std::endl;
    std::cout << (manager.approveInvoice(584) ? "True" : "False") << std::endl;

    std::cout << (director.approveInvoice(927) ? "True" : "False") << std::endl;
    std::cout << (director.approveInvoice(1000) ? "True" : "False") << std::endl;
    std::cout << (director.approveInvoice(1084) ? "True" : "False") << std::endl;
    return 0;
}    

Output:


My name is John and I am a Clerk
My name is Jane and I am a Manager
My name is Jack and I am a Director
True
True
False
True
True
False
True
True
False

Question 2 - Company Hierarchy with Outsourcing

To cut costs, the Company decided to take on Contractors.

A Contractor has a Name in the company records, but has no formal designation. A Contractor cannot approveInvoice (but of course!).

Suppose that at a Company function, everyone has to introduce himself / herself:-

    Hello, my name is xxx.  My designation is XXX.
                          or
    Hello, my name is yyy.  I am a Contractor.
  • Design the new Company Hierarchy in a UML Class Diagram, to accommodate the Contractor. [Hint: pull up the common attributes / properties and behaviour into an Abstract Class.]
  • Implement the new Company Hierarchy. Keep the main() loop in the Director class.

Solution:


...
// program 2

class Contractor : protected Clerk
{
private:

public:
    Contractor(std::string);
    ~Contractor();
    std::string introduce();
    std::string getName();
};

Contractor::Contractor(std::string name) : Clerk(name)
{
}

Contractor::~Contractor()
{
}

std::string Contractor::introduce()
{
    return "My name is " + this->getName() + " and I am a Contractor";
}

std::string Contractor::getName()
{
    return Clerk::getName();
}

int main(int argc, char const *argv[])
{
    Contractor contractor("John");
    std::cout << contractor.introduce() << std::endl;
    std::cout << contractor.getName() << std::endl;

    return 0;
}


My name is John and I am a Contractor
John

Question 3 - Fraud!

Suppose a Director in the Company tries to impersonate his Clerk at the Company function by introducing himself as a Clerk. By using casting, show how that this is possible.


Clerk* cPtr = new Manager(...);
cPtr->introduce();   

Suppose a Clerk in the Company tries to impersonate his Manager at the Company function. Explain why this is not possible.


Manager* mPtr = new Clerk(...);
mPtr->introduce();

Solution:


...
// program 3
int main(int argc, char const *argv[])
{
    Clerk *cPtr = new Manager("John");
    std::cout << cPtr->introduce() << std::endl;

    try
    {
        Manager *mPtr = new Clerk("Jane");
        mPtr->introduce();
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << 'n';
    }

    return 0;
}


My name is John and I am a Manager

Share:

0 comments:

Post a Comment