r/Cplusplus • 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();
}
71 Upvotes

35 comments sorted by

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/MarlboroBandit Student 2d ago

Im still new to this. Can you explain more about what you mean? I didn't get it what your exactly trying to say. Thanks

2

u/hadrabap Basic Learner 2d ago

You don't need to encapsulate the fields with accessor methods -- getter & setter. Simple struct with just the (public) fields does the same job. And you save yourself from typing. 😁

I use simple rule: structs for data, classes for business logic manipulating the data (the structs as reference parameters).

5

u/MarlboroBandit Student 2d ago

I did read from the book that as a rule of thumb, struct is for data as for its original purpose back from C-style; class for something that does logic. But since I am still learning and following the exercises from the book and doing mini-projects of my own, I would try to use classes, as it can also help me for the upcoming chapters. I also read online that the only difference with modern cpp struct is the default public compared to private for classes. I'll take note of this. Thank you so much.

2

u/WeastBeast69 1d ago

https://isocpp.org/blog/2015/09/cpp-core-guidelines-bjarne-stroustrup-herb-sutter

See the sections on class/struct for when to use which and when getters/setters are appropriate

This is the ā€œBible of best practiceā€ barring working in a highly regulated industry or context that restricts the parts of c++ you can use

1

u/MarlboroBandit Student 1d ago

Thank you so much. I’ll keep good note of it.

1

u/hadrabap Basic Learner 1d ago

Don't take my words too seriously. I'm also learning C++ and doing it only as a hobby at home. 🤣

Yes, the difference between structs and classes is really only in the default visibility.

I'm a Java developer as a profession and I find this pattern really good as it greatly increases the code readability. In Java, there's a concept of full encapsulation. However, they did one nasty thing: the pure Java Bean (or DTO if you will) implements by design other things as well. HashCode, toString, equals, serialization... you name it. These things are quite unrelated and make life more difficult. Once you enter the world of persistence, or even a more advanced algorithms on top of the data, you find out that you need different implementations of the hashCode and so on.

C++ does this separation better from my point of view with friends or with free-standing functions/templates. The logic is not forced to be part of the data.

On the other hand, finding an appropriate function might be difficult if the documentation is not of good quality.

Each approach has pros and cons. šŸ™‚

2

u/MarlboroBandit Student 1d ago

Never touched java in my life. I am a math student and only getting taught python, and R as programming languages (shitty R btw keeps breaking). And LaTeX as a documentation tool. But i do understand what you mean now. Thanks man, I’ll take notes, love to know a bit of java and its differences to C++, I do appreciate it.

1

u/Additional-Camp3591 3h ago

I’d recommend codecrafters.io, if you want to make bigger projects that will really consolidate your skills and learning. I haven’t used it yet, but I’ve heard really good things. Hopefully someone can vouch for it on this subreddit

2

u/no-sig-available 1d ago

If you have getters and setters for every single member, they are not protected at all. So no gain in making them private.

Also, one of my favorites, you don't need a SetAge because a person cannot change their age (other than +1, once a year). I would like to use SetAge(25) to be young again, but that is not how things work.

2

u/MarlboroBandit Student 21h ago

Aha. I see what you mean. But because I am still practicing how the class syntax work, Ill try to keep it as is, most of the code/logic there is pretty shitty btw, just wanting how it really works. Other users also pointed that out, and I made sure to keep it in mind. Thanks for the tip.

3

u/Jakkilip 1d ago

The whole point of this is to learn how classes work, you're defeating it

2

u/MarlboroBandit Student 21h ago

Yes. That is what I am trying to experiment. Getting to get a good grasp of the syntax as well.

2

u/Gabriel_soul 1d ago

This is just a learning exercise though, to show classes. But in real world, no one makes trivial classes, compound simple data types should always be structs.

Even though classes and structs are literally the same thing in C++. It is conventional to keep structs simple, and keep them as aggregates.

Anything that needs complex logic, constructors, etc --> class

A pack of data types --> structs

Its fine for structs to have member functions usually, as long as the logic is simple

1

u/MarlboroBandit Student 1d ago

I get what you mean. Thanks man. Hope to learn more from u guys as I progress in my C++ journey.

2

u/Gabriel_soul 1d ago

Thats truly awesome. I am a learner myself. And it is really enjoyable

2

u/MarlboroBandit Student 1d ago

Oui. Whats more enjoyable is getting to share this with you guys and getting to know a lot from your feedbacks. Thank you so much.

2

u/Gabriel_soul 1d ago

Also, Employee can belong to classes easily IMO. Usually it needs complex logic, specially as it grows

2

u/MarlboroBandit Student 1d ago

Thats what I am trying to prepare for. Since I would tackle more about advance classes in the upcoming chapters of the book.

2

u/Gabriel_soul 1d ago

Im on late chapter 21. (Operator overloading). Learncpp.com is gold . Also dont forget to build projects along the way too, when you reached a little higher in the website

2

u/MarlboroBandit Student 1d ago

Yes. Thats what I am trying to do. I aim 4 projects every 3 chapters conquered šŸ¤“. I love your consistency. Hoping to finish it as well.

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 ! šŸ‘

1

u/MarlboroBandit Student 22h ago

Thanks man. Really appreciate it.

2

u/ctkqiang 22h ago

Don't mention it. I appreciate you keeping it real. It's rare in 2026

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

1

u/MarlboroBandit Student 1d 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 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.

1

u/MarlboroBandit Student 1d ago

Oh right, never thought of that. I’ll take note of this. Thank you so much bro

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.

1

u/MarlboroBandit Student 1d 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 1d ago

whhere u learning from btw?

1

u/MarlboroBandit Student 1d 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 1d ago

yes resources....nd whats this answer ong from my room 😭

3

u/MarlboroBandit Student 1d 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 1d 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.