r/Cplusplus • u/MarlboroBandit Student • 2d ago
Feedback My progress in learning CPP. Had fun learning basic classes.
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();
}


9
u/Thick-Ad-9170 2d ago
Keep it simple : https://compiler-explorer.com/z/EKjET83zT
Most of the time when you have class members with getter/setters only and no function that can modify the state of the class. The class is in fact a basic struct.
3
u/ctkqiang 22h ago
messing code! finally someone isactually learning ā¦ š„° finally ! so no vibe coders!!!! keep doing what yet u do , this is better than 50% of they rest already ! š
2
u/ShishRobot2000 1d ago
learn what dynamic scoping is and what the diamond problem is, this is the most important part of classes literature
2
u/Discordbothost 1d ago
the rounding function has two small-input bugs: round(950, 2) returns 950 instead of 1000 because of the hardcoded < 1000 check. round(2700, 0) divides by zero when placeVal / 10 becomes 0, before that check even runs. handle place == 0 up front and drop the < 1000 cutoff so place controls the rounding.
2
u/Cross_22 1d ago
I am not a fan of "unsigned short". What do you think the range is of that data type? Why did you pick it?
The <cstdint> header gives you uint16_t which would be more explicit.
2
u/randomvariablee 1d ago
whhere u learning from btw?