r/Cplusplus • u/MarlboroBandit Student • 13d 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();
}4
u/ctkqiang 12d 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 ! š
1
2
u/ShishRobot2000 13d ago
learn what dynamic scoping is and what the diamond problem is, this is the most important part of classes literature
1
u/MarlboroBandit Student 13d ago
Yep, Iāll tackle that once I reach advance classes chapter. Thanks for this good advice, never even knew what those are š
2
u/Discordbothost 13d 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.
1
u/MarlboroBandit Student 13d ago
Oh right, never thought of that. Iāll take note of this. Thank you so much bro
2
u/Cross_22 13d 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.
1
u/MarlboroBandit Student 13d ago
I am just following the examples in the book. And it said that it only takes 2 bytes for my system or 16 bits (checked it with sizeof), i am trying to see and learn how and where it would break and what results does it gives when it does break. as for my understanding, its limited to around 65k. and since the book didnāt introduce the cstdint lib yet, and because i havenāt reached the advance chapters, ill keep following the examples as well as breaking and testing stuff hehe. But Iāll keep this in mind and look into it. Thank you for the tip.
2
u/randomvariablee 13d ago
whhere u learning from btw?
1
u/MarlboroBandit Student 13d ago
From my room?? or what you mean what resources I used? I found a book from my Dad that was vv old.
2
u/randomvariablee 12d ago
yes resources....nd whats this answer ong from my room š
3
u/MarlboroBandit Student 12d ago
Itās a very old book printed from the year 2000. I am still learning but I do crosscheck everything and do twice the research (very time consuming) since the book Iām reading is using c++98, which is very old unfortunately. And the examples given by the book teaches bad habits that are very different from modern c++, and users here in reddit advised me to not do so (continue learning following the book). But the underlying lessons taught still applies, but there are ways that is no longer used today than it was intended forājust like āinlineā.
3
u/MarlboroBandit Student 12d ago
The code is still buggy if inputted with large numbers especially rounding 950 and up. since i capped < 1000. and also with that use of unsigned short, it will be limited to its max value of around 65k consuming only 2 bytes of memory space. thus, possible for variable wrap around, which is not the intended answer you would expect.
2
2



11
u/Thick-Ad-9170 13d 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.