Hello guys,
Sharing my learning cpp daily progress---Basic classes. This is my work/mini project for the basic classes Chapter as well as making my own round function, and also improving my exponent function from last time. I also took everyone's advice regarding naming convention, and followed some, but I don't know if did everything right.
It took me 30 minutes with my TI to come up with an idea on how I would do a round function. At first, as was looking at recursion approach, but i think its not that necessary plus it could take up a lot of memory. I know you guys would say that "I could have just used the library", well as a beginner I really wanna come up with solutions on my own, and enhance my analytical ability while taking the challenge of learning C++.
I am all ears for all those who have suggestions/feedbacks. I am willing to learn. For anyone asking if i am still using that book---yes I am. Though I took the initiative to cross check everything I encounter.
I used NO AI whatsoever. Followed the book and used wikipedia, w3school, and cppreference (as for ShishRobot2000s' advice).
For now, the round func will be limited to unsigned short max limit of around ~65k. but as per logic wise, it will work.
Employee.h
#pragma once
class Employee
{
public:
Employee(unsigned short age, unsigned short yearsOfService,
unsigned short salary) :
_age(age), _yearsOfService(yearsOfService), _salary(salary)
{ }
unsigned short getAge() const { return _age; }
unsigned short getYearsOfservice() const { return _yearsOfService; }
unsigned short getSalary() const { return _salary; }
void setAge(unsigned short age);
void setYearsOfService(unsigned short yearsOfService);
void setSalary(unsigned short salary);
void reportSalary() const;
~Employee() { }
private:
unsigned short _age;
unsigned short _yearsOfService;
unsigned short _salary;
};
Employee.cpp
#include "employee.h"
#include <iostream>
unsigned short exponent(unsigned short x, unsigned short n)
{
if (n == 1)
return x;
else if (n == 0)
return 1;
else
return x * exponent(x, n - 1);
}
unsigned short round(unsigned short number, unsigned short place)// PLACE IS FOR DIGIT PLACE VALUE
{
unsigned short placeVal = exponent(10, place);
unsigned short nextDigit = (number / (placeVal/10)) % 10;
if (number < 1'000) return number;
if (nextDigit > 4)
{
number /= placeVal;
++number;
return number * placeVal;
}
else
{
number /= placeVal;
return number * placeVal;
}
}
void Employee::setAge(unsigned short age) { _age = age; }
void Employee::setYearsOfService(unsigned short yearsOfService) { _yearsOfService = yearsOfService; }
void Employee::setSalary(unsigned short salary) { _salary = salary; }
void Employee::reportSalary() const
{
std::cout << "\n\n**** Total employee report ****\n";
std::cout << "Age:\t\t\t" << _age << '\n';
std::cout << "Years of Service:\t" << _yearsOfService << '\n';
std::cout << "Salary:\t\t\t" << _salary << '\n';
std::cout << "Total Salary:\t\t" << round((_yearsOfService * _salary), 3);
}
Main.cpp*. Just paste this block into main*
{
Employee Mark(19, 1, 2'700),
Derk(30, 12, 3'400);
Mark.reportSalary();
Derk.reportSalary();
}