Well, i have been playing around with my programming skills trying to develop my skill and making efficient algos. and stuff. I need to know just how effiecient this is not including how i created the dynamic memory ( i.e... i just began to learn them ). When i say efficent i mean thing like:
Hiding info from client
Readability ( no comments... )
and so on, u know things of that nature or more specify if it accomplishes the programmers goals in making codes. You should know them i guess.
Well here they are codes:
Header File:
| CODE |
// Intrest Header File.h // By: Darwin Aguirre #ifndef INTREST_H #define INTREST_H
class Intrest {
public: Intrest( const char *, const char *, int ); ~Intrest();
static double getIntrest();
Intrest &setYear( int ); Intrest &setBalance( int );
void setIntrest();
void calculateIntrest();
void getBalance() const; void getTrueBalance(); int getYear() const;
const char *getFirstName() const; const char *getLastName() const;
private: char *firstName; char *lastName;
int BalanceB; int BalanceTrue; int Money; int Year;
static double intrest; };
#endif
|
Class Definition:
| CODE |
// Intrest CPP.cpp // By: Darwin Aguirre #include <iostream>
using std::cout; using std::endl;
#include <new> #include <cstring>
#include "intrest.h"
Intrest::Intrest( const char *first, const char *last, int a ) : Money( a ) { firstName = new char[ strlen( first ) + 1 ]; strcpy( firstName, first );
lastName = new char[ strlen( last ) + 1 ]; strcpy( lastName, last );
cout << "Constructor for " << getFirstName() << " " << getLastName() << " with " << Money << "$ has been called.\n"; }
Intrest::~Intrest() { cout << "Destructorr for " << getFirstName() << " " << getLastName() << " with " << BalanceTrue << "$ has been called.\n";
delete [] firstName; delete [] lastName; }
double Intrest::intrest = .03;
double Intrest::getIntrest() { return intrest; }
Intrest &Intrest::setYear( int y ) { Year = ( y <= 5 && y > 0 ) ? y : 1; setIntrest();
return *this; }
Intrest &Intrest::setBalance( int b ) { BalanceB = b; calculateIntrest();
return *this; }
void Intrest::setIntrest() { if ( Year == 1 || 2 || 3 ) intrest += .1; else if ( Year == 4 || 5 ) intrest += .7; }
void Intrest::calculateIntrest() { BalanceTrue = ( Money * intrest ) + BalanceB; getBalance(); }
void Intrest::getBalance() const { cout << getFirstName() << " " << getLastName() << "\'s balance is " << BalanceTrue << "$\n"; }
void Intrest::getTrueBalance() { BalanceTrue = Money;
cout << getFirstName() << " " << getLastName() << "\'s balance is " << BalanceTrue << "$\n"; }
int Intrest::getYear() const { return Year; }
const char *Intrest::getFirstName() const { return firstName; }
const char *Intrest::getLastName() const { return lastName; }
|
Using The Class ( i.e Main ):
| CODE |
// Test Of 'Intrest' Class.cpp // By: Darwin Aguirre #include <iostream>
using std::cout; using std::endl;
#include <new>
#include "intrest.h"
int main() { Intrest *Person1 = new Intrest( "John", "Adams", 2000 ); Intrest *Person2 = new Intrest( "Sarah", "Kage", 7200 );
cout << "\nCurrent account belongs to: " << Person1->getFirstName() << " " << Person1->getLastName() << endl; cout << "Current year is: "; Person1->setYear( 1 ); cout << Person1->getYear() << endl; cout << "Current intrest: " << Person1->getIntrest() << '%' << endl; cout << "Balance befor intrest: "; Person1->getTrueBalance(); cout << "Balance after intrest: "; Person1->setBalance( 2000 );
cout << "\nCurrent account belongs to: " << Person2->getFirstName() << " " << Person2->getLastName() << endl; cout << "Current year is: "; Person2->setYear( 4 ); cout << Person2->getYear() << endl; cout << "Current intrest: " << Person1->getIntrest() << '%' << endl; cout << "Balance befor intrest: "; Person2->getTrueBalance(); cout << "Balance after intrest: "; Person2->setBalance( 7200 ); cout << endl;
delete Person1; Person1 = 0; delete Person2; Person2 = 0;
return 0; }
|
So how is it?? :huh:
Ahh, reminds me of a Java assignment I've just done for uni.
Anyhoo couple of things:
Interest is a bad name for this class since it also deals with names and balances. Account would be better.
You don't have to include <new> here as you're not using placement new or std::nothrow.
You're inconsistent with the set function return types. They should all be void here but you've got some returning *this for some reason.
This may be just me but I'd expect setter functions to do just that, and nothing else. For example, I wouldn't expect a function called setBalance to possibly change the interest as well. Same with the get functions, I'd expect them to return variables but you've got two of them as void. These should probably be renamed print* or display* or similar.
Include <string> and use std::string instead of char*. Then you don't have to bother with new, delete, strlen, and strcpy when dealing with the names, as they do it internally.
| QUOTE |
| if ( Year == 1 || 2 || 3 ) |
This doesn't do what you think it does, year is only checked against 1; not 1, 2, and 3. Here, if year doesn't equal 1, 2 is evaluated. Since 2 is never false (it's not 0) the if part will always happen and the else has as much chance as happening as the Rockies winning the World Series. You need to do
| CODE |
| if ( Year == 1 || Year == 2 || Year == 3 ) |
to get desired behaviour.
And the nitpicky stuff :P
Doesn't the dollar sign come before the amount of money, not after? (assuming you're from North America)
oh and it's spelled interest :)
So yeah, it looks a lot but it's only a few things.
Well, u have written exactly what i would expect some1 to write. For several reason, one i really made this code joking to see if any1 could see through many of its irregularities. Also i was purposely miss naming and miss treating my data and member function to see how some1 ( i.e u for now ) would react or even study this code. This code was made for another reason so i could study the the way a experience or not experience programmer would decipher this code. Because i study the human mind, so i was really manipulating the code on purpose so that i could further increase my data on the mind. I know it sounds weird but i am a person who deciphers many thing so i am what u call a philosopher.
But i know what u mean about the weird data names and member functions. Also my skills at programming are really not all that great; the reason i decided to use these weird methods of creating this code was to test my skill also. This code like i said before was a joking code ( made it like so on purpose ), but also about the std::string is that i have not learned every operation, keyword in C++ i use the one available to my skill. Since i am still learning my skill is still growing but i am only aware of so much, but currently i needed to improve my skill in:
- this operation
- member syntax initialization
- dynamic allocation and deallocation
- pointer and reference in classes
- pointer, array arithmetic
- constructor and destructor
- include <cstring>
- efficient algos
But anyways thxz for the comment just needed to know a couple things. Ohh and with the:
| CODE |
if ( year == 1 || 2 || 3 );
|
Usually when i do that i use and operation:
| CODE |
if ( year == 1 && 2 && 3 );
|
it now checks to see all the values.
What u said about now having to include <new>, well u have to include new so that the compiler know what new is. On my previous codes i have not used <new> header file and even though no error came up, the char name would not display.
Also the interest thing LOL woops, well i am not the best speller in the world... <_<