On Suday the 11th of February, 2007 -after 3 and a half years on InvisionFree, we have moved! This old board remains as a read only archive of years past, and registration has been disabled here. All new and current members should register at http://www.cpplc.net/forum .


InvisionFree - Free Forum Hosting
Fully Featured & Customizable Free Forums

Learn More · Sign-up Now
Welcome to C++ Learning Community. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Name:   Password:

Please help out
Google

  Reply to this topicStart new topicStart Poll

> How Good Is It?
Shonoby
Posted: Feb 9 2007, 02:15 AM
Quote Post


...Philosopher...


Group: Members
Posts: 132
Member No.: 2,295
Joined: 23-November 06



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.gif


--------------------
Foolish Brother.
If You Wish To Kill Me, Hate Me, Detest Me...
And Survive In An Unsightly Way.
Run And Cling On To Life.


I HEART TEMPLATES

My Philosophies:
If you want to be great in life, defy it.
The best answers we find on our own.

It's not how a guitar looks, it's how you make it look.
A life of love; is eventually a life of hate.(Via Versa)
Don't hate the ( skate || key )board, hate yourself.

It's not a question of if death happens because:
All death is certain, but when it happens is the question.
PMEmail PosterUsers Website AOLYahooMSN
Top
adeyblue
Posted: Feb 9 2007, 03:52 AM
Quote Post


The Colin Mochrie of the Windows World


Group: Members
Posts: 28
Member No.: 2,013
Joined: 30-August 06



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 tongue.gif
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 smile.gif

So yeah, it looks a lot but it's only a few things.


--------------------
I don't write messy code, you're just not reading it properly
AudioPlayer - FileSplitter - Windows ACL and ACE Utilities
PMEmail PosterUsers Website
Top
Shonoby
Posted: Feb 9 2007, 04:28 AM
Quote Post


...Philosopher...


Group: Members
Posts: 132
Member No.: 2,295
Joined: 23-November 06



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:
  1. this operation
  2. member syntax initialization
  3. dynamic allocation and deallocation
  4. pointer and reference in classes
  5. pointer, array arithmetic
  6. constructor and destructor
  7. include <cstring>
  8. 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... dry.gif


--------------------
Foolish Brother.
If You Wish To Kill Me, Hate Me, Detest Me...
And Survive In An Unsightly Way.
Run And Cling On To Life.


I HEART TEMPLATES

My Philosophies:
If you want to be great in life, defy it.
The best answers we find on our own.

It's not how a guitar looks, it's how you make it look.
A life of love; is eventually a life of hate.(Via Versa)
Don't hate the ( skate || key )board, hate yourself.

It's not a question of if death happens because:
All death is certain, but when it happens is the question.
PMEmail PosterUsers Website AOLYahooMSN
Top
« Next Oldest | C++ Creations | Next Newest »
InvisionFree - Free Forum Hosting
Free Forums with no limits on posts or members.
Learn More · Register for Free

Topic Options Reply to this topicStart new topicStart Poll


Skin selector developed by XJONX. Skins created by various members of the IF Skin Zone and InvisionFree Skinning

Please help out
Hosted for free by InvisionFree* (Terms of Use: Updated 2/10/2010) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.0898 seconds | Archive