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

Lab #4 - Overloading

Question 1 - Employee

Create an Employee class according to the given Class Diagram.


Employee
- name: String
- gender: int
- colour : string
- Employee()
* Employee(in_name:String, in_gender:boolean)
- getName(): String
- getGender(): boolean
- print(): String

The default constructor is declared private to prevent its use. boolean getGender() returns FALSE if male and TRUE if female. The print() function gives the following sample output:

    Hello, my name is Arif. I am male.

From 2014, India officially recognises a third gender. Modify your design (i.e. give a new UML Class Diagram) to provide

    an overloaded constructor to take in gender information as an integer,
    an overloaded getGender() function that returns 0 if male, 1 if female and -1 if transgender, and
    an overloaded print() function that handles the third gender. 

In your main() loop, create

    2 Employee instances, hired before 2014, and
    3 Employee instances, hired after 2014.

Solution:


#include <iostream>
#include <sstream>

class Employee
{
private:
    std::string name;
    int gender;
    Employee();

public:
    Employee(std::string, bool);
    Employee(std::string, int);
    ~Employee();
    std::string getName();
    bool getGender();
    int getGender(int);
    std::string print();
    std::string print(int);
};

Employee::Employee()
{
}

Employee::~Employee()
{
}

Employee::Employee(std::string name, bool gender)
{
    this->name = name;
    this->gender = gender;
}

std::string Employee::getName()
{
    return name;
}

bool Employee::getGender()
{
    return gender;
}

std::string Employee::print()
{
    std::stringstream ps;
    ps << "Hello, my name is " << name << ". I am " << (gender == 0 ? "male" : "female") << "." << std::endl;
    return ps.str();
}

/*
    After 2014 changes
*/

Employee::Employee(std::string name, int gender)
{
    this->name = name;
    this->gender = gender;
}

int Employee::getGender(int i)
{
    if (i)
        return this->getGender();
    return gender;
}

std::string Employee::print(int i)
{
    if (i)
        this->print();

    std::stringstream ps;
    ps << "Hello, my name is " << name << ". I am " << (gender == 0 ? "male" : (gender == 1 ? "female" : "transgender")) << "." << std::endl;
    return ps.str();
}

int main(int argc, char const *argv[])
{
    Employee e1("Alpha", false), e2("Beta", true);
    std::cout << e1.print(0);
    std::cout << e2.print(0);
    Employee em1("Delta", 1), em2("Gamma", 0), em3("Epsilon", -1);
    std::cout << em1.print(0);
    std::cout << em2.print(0);
    std::cout << em3.print(0);
    return 0;
}    

Output:


Hello, my name is Alpha. I am male.
Hello, my name is Beta. I am female.
Hello, my name is Delta. I am female.
Hello, my name is Gamma. I am male.
Hello, my name is Epsilon. I am transgender.

Question 2 - Operator Overloading

Implement a class named RealTime with representation HH:MM:SS. Design and implement appropriate constructors.

Overload the following operators:

    +	 Addition e.g. myRealTime + yourRealTime
    ++	 Post-Increment e.g. myRealTime++
    ++	 Pre-Increment e.g. ++RealTime 

Provide comments in your code and proper indentation, so that it is easy to read.

Solution:


#include <iostream>
#include <iomanip>

class RealTime
{
private:
    int hr, min, sec;

public:
    RealTime();
    RealTime(int, int, int);
    ~RealTime();
    RealTime operator+(RealTime const &);
    RealTime operator++();
    RealTime operator++(int);
    friend std::ostream &operator<<(std::ostream &, RealTime const &);
};

RealTime::RealTime()
{
    hr = min = sec = 0;
}

RealTime::RealTime(int hr, int min, int sec)
{
    this->hr = hr;
    this->min = min;
    this->sec = sec;
}

RealTime::~RealTime()
{
}

RealTime RealTime::operator+(RealTime const &rt)
{
    RealTime temp;
    temp.sec = this->sec + rt.sec;
    temp.min = this->min + rt.min + temp.sec / 60;
    temp.sec %= 60;
    temp.hr = this->hr + rt.hr + temp.min / 60;
    temp.min %= 60;
    return temp;
}

RealTime RealTime::operator++()
{
    ++sec;
    min = min + sec / 60;
    sec %= 60;
    hr = hr + min / 60;
    min %= 60;
    return *this;
}

RealTime RealTime::operator++(int)
{
    RealTime temp(hr, min, sec);
    sec++;
    min = min + sec / 60;
    sec %= 60;
    hr = hr + min / 60;
    min %= 60;
    return temp;
}

std::ostream &operator<<(std::ostream &os, RealTime const &rt)
{
    return os << std::setfill('0') << std::setw(2) << rt.hr << ":"
                << std::setfill('0') << std::setw(2) << rt.min << ":"
                << std::setfill('0') << std::setw(2) << rt.sec;
}

int main(int argc, char const *argv[])
{
    RealTime myRealTime(7, 2, 5), yourRealTime(12, 34, 56);
    RealTime resRealTime = myRealTime + yourRealTime;
    std::cout << "My Real Time = " << myRealTime << std::endl;
    std::cout << "Your Real Time = " << yourRealTime << std::endl;
    std::cout << "Total Real Time = " << resRealTime << std::endl;
    std::cout << "Prefix Add Real Time = " << ++myRealTime << std::endl;
    std::cout << "Postfix Add Real Time = " << yourRealTime++ << std::endl;
    std::cout << yourRealTime << std::endl;
    return 0;
}    

Output:


My Real Time = 07:02:05
Your Real Time = 12:34:56
Total Real Time = 19:37:01
Prefix Add Real Time = 07:02:06
Postfix Add Real Time = 12:34:56
12:34:57

Share:

0 comments:

Post a Comment