// Try to not put any special characters in your file names (+, spaces or such)
// For class files (header and cpp file), it is a good to give them the same name as the Class (here Account.h and Account.cpp)
// Think about ALWAYS putting the header code into these things, as it will protect your code from double/multi inclusion (VERY IMPORTANT!!! EVEN MORE SO IN BIGGER PROJECTS!!!)
#ifndef __ACCOUNT_HPP__
#define __ACCOUNT_HPP__
#include <iostream>
// BAD PRACTICE!!! AND ESPECIALLY NOT IN A HEADER PLEASE! There are A LOT of things in std, so if for example another library has a function with the same name, it can cause important problems!!
// See cpp file for an alternative form
//using namespace std;
// Always begins with a Capital Letter!!!
// Also, prefer to put your code in the appropriated cpp files, it is a lot cleaner, readable and maintainable
class Account {
private :
int cstNum; // cst = customer
int cstBal;//balance
int cstChrg;//charge
int cstCredit;
int cstCreditLimit;
// unused variable (even my IDE says it!)
int cstNewBalance;
public :
// Try to always define at least the constructor or destructor, even if they don't have anything in them (even though it is a good practive to initialize your variables there)
Account() {
cstNum = 0;
cstBal = 0;
cstChrg = 0;
cstCredit = 0;
cstCreditLimit = 0;
cstNewBalance = 0;
}
// Destructor (always called when deinitializing the object), I don't think we spoke about it in the class, so don't worry too much about it for now
~Account() {}
// camelCase is prefered, but that is just because it is in the general coding convention, so it is not so bad if you don't do it, but it is still better :P
void setNum(int num) {
cstNum = num;
}
// you have to take doubles as parameters, else you won't be able to get decimals values
void setBal(double bal) {
cstBal = static_cast<int>(bal * 100);
}
void setChrg(double chrg) {
cstChrg = static_cast<int>(chrg * 100);
}
void setCredit(double credit) {
cstCredit = static_cast<int>(credit * 100);
}
void setCreditLimit(double creditLimit) {
cstCreditLimit = static_cast<int>(creditLimit * 100);
}
// not a very good way to do it, just make it a getter in that case, because if the user forgots to set the variable (like you did), it is not gonna work correctly
// unused function (IDE says it!)
void setNewBalance() {
// gives out a warning: values of type double may not fit into an integer
cstNewBalance = (cstBal + cstCredit - cstChrg) * 0.01;
}
// in our case, a getter probably does the job too, and so we don't need to store it in a separate field.
double getNewBalance() const {
// wrong way to calculate it!! here corrected version:
return (cstBal + cstChrg - cstCredit) * 0.01;
}
// you can add const behind the function, as we don't really need to set anything in this function
void getExceed() const {
double newBalance = getNewBalance();
double creditLimit = cstCreditLimit / 100.0;
// be careful, you forgot to divide cstCreditLimit by 100 (as you multiplied it by 100 at the beginning), or to not divide cstNewBalance by 100
// here I just calculated once the new balance and got the result, and then calculated the double version of the creditLimit, you can also do it the other way around if you want
if (newBalance > creditLimit) {
std::cout << "new balance is" << newBalance << ", " << cstNum << "'s credit limit is exceed" << std::endl;
} else {
std::cout << "new balance is" << newBalance << std::endl;
}
// another to do it in another way, which one you want to use is up to you, I just wanted to show you it is possible
std::cout << "new balance is" << newBalance; // <-- no newline so we can continue to put stuff behind it
if (newBalance > creditLimit) {
std::cout << ", " << cstNum << "'s credit limit is exceed"; // <-- put something behind it
}
std::cout << std::endl; // <-- finish our line
}
};
#endif // __ACCOUNT_HPP__
// For class files (header and cpp file), it is a good to give them the same name as the Class (here Account.h and Account.cpp)
// Think about ALWAYS putting the header code into these things, as it will protect your code from double/multi inclusion (VERY IMPORTANT!!! EVEN MORE SO IN BIGGER PROJECTS!!!)
#ifndef __ACCOUNT_HPP__
#define __ACCOUNT_HPP__
#include <iostream>
// BAD PRACTICE!!! AND ESPECIALLY NOT IN A HEADER PLEASE! There are A LOT of things in std, so if for example another library has a function with the same name, it can cause important problems!!
// See cpp file for an alternative form
//using namespace std;
// Always begins with a Capital Letter!!!
// Also, prefer to put your code in the appropriated cpp files, it is a lot cleaner, readable and maintainable
class Account {
private :
int cstNum; // cst = customer
int cstBal;//balance
int cstChrg;//charge
int cstCredit;
int cstCreditLimit;
// unused variable (even my IDE says it!)
int cstNewBalance;
public :
// Try to always define at least the constructor or destructor, even if they don't have anything in them (even though it is a good practive to initialize your variables there)
Account() {
cstNum = 0;
cstBal = 0;
cstChrg = 0;
cstCredit = 0;
cstCreditLimit = 0;
cstNewBalance = 0;
}
// Destructor (always called when deinitializing the object), I don't think we spoke about it in the class, so don't worry too much about it for now
~Account() {}
// camelCase is prefered, but that is just because it is in the general coding convention, so it is not so bad if you don't do it, but it is still better :P
void setNum(int num) {
cstNum = num;
}
// you have to take doubles as parameters, else you won't be able to get decimals values
void setBal(double bal) {
cstBal = static_cast<int>(bal * 100);
}
void setChrg(double chrg) {
cstChrg = static_cast<int>(chrg * 100);
}
void setCredit(double credit) {
cstCredit = static_cast<int>(credit * 100);
}
void setCreditLimit(double creditLimit) {
cstCreditLimit = static_cast<int>(creditLimit * 100);
}
// not a very good way to do it, just make it a getter in that case, because if the user forgots to set the variable (like you did), it is not gonna work correctly
// unused function (IDE says it!)
void setNewBalance() {
// gives out a warning: values of type double may not fit into an integer
cstNewBalance = (cstBal + cstCredit - cstChrg) * 0.01;
}
// in our case, a getter probably does the job too, and so we don't need to store it in a separate field.
double getNewBalance() const {
// wrong way to calculate it!! here corrected version:
return (cstBal + cstChrg - cstCredit) * 0.01;
}
// you can add const behind the function, as we don't really need to set anything in this function
void getExceed() const {
double newBalance = getNewBalance();
double creditLimit = cstCreditLimit / 100.0;
// be careful, you forgot to divide cstCreditLimit by 100 (as you multiplied it by 100 at the beginning), or to not divide cstNewBalance by 100
// here I just calculated once the new balance and got the result, and then calculated the double version of the creditLimit, you can also do it the other way around if you want
if (newBalance > creditLimit) {
std::cout << "new balance is" << newBalance << ", " << cstNum << "'s credit limit is exceed" << std::endl;
} else {
std::cout << "new balance is" << newBalance << std::endl;
}
// another to do it in another way, which one you want to use is up to you, I just wanted to show you it is possible
std::cout << "new balance is" << newBalance; // <-- no newline so we can continue to put stuff behind it
if (newBalance > creditLimit) {
std::cout << ", " << cstNum << "'s credit limit is exceed"; // <-- put something behind it
}
std::cout << std::endl; // <-- finish our line
}
};
#endif // __ACCOUNT_HPP__
댓글
댓글 쓰기