![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
Any Critisisms?
// enemy and hero class structures
// Bases are to be inherited by their respective class in main file.
// Functions should multiply base values by level to determine health,defense,and attack.
// By: Matthew Henry
// created on 1/7/06 7:04
// last edited on 1/7/06
// v. 1.0 Constructive Criticism phase
// all values must be PUBLIC!!!!
#pragma once
class Saucer
{
public:
int level;
int health;
int defense;
int attack;
bool s_alive;
Saucer()
{
attack=3;
defense=1;
health=10;
level=1;
s_alive=true;
}
~Saucer()
{
}
int levelup()
{
int level_rec=level;
level++;
attack=attack*2;
defense=defense*2;
health=health*2;
if (level==(level_rec+1))
return 0;
else
return 1;
}
};
class Hero
{
public:
int level;
int health;
int defense;
int attack;
bool h_alive;
Hero()
{
attack=5;
defense=1;
h_alive=true;
health=50;
level=1;
}
~Hero()
{
}
int levelup()
{
int level_rec=level;
level++;
attack=attack*2;
defense=defense*2;
health=health*2;
if (level==(level_rec+1))
return 0;
else
return 1;
}
};
class Mothership
{
public:
int level;
int health;
int defense;
int attack;
bool m_alive;
Mothership()
{
attack=25;
defense=1;
health=250;
level=1;
m_alive=true;
}
~Mothership()
{
}
int levelup()
{
int level_rec=level;
level++;
attack=attack*2;
defense=defense*2;
health=health*2;
if (level==(level_rec+1))
return 0;
else
return 1;
}
};Thank you very much I truly appreciate it. P.S. I used in a header that's why it has #pragma once.
__________________
Geeks may not be cool now but in the long run they prosper. Last edited by teencoder; Jan 7th, 2006 at 10:51 PM. |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
First of all, it is very bad practise to define functions before you prototype them. Especially when designing classes.
So, taking a simple example,
class T {
public:
T() {
//do stuff
}
void doSomething() {
//do stuff
}
};
//instead, do this
class T {
public:
T();
void doSomething();
};
T::T() {
//do stuff
}
void T::doSomething() {
//do stuff
}And, as I have learned also on this forum, method/function definitions should never be in header files. Only the prototypes should be included in the header. The definitions (where you say what each function does) should be in a seperate file. Also, instead of making your properties public, ( which is a fundementally bad idea, becuase I could do something like this in a main() function, which is a very, very bad thing. Saucer::health = 32003213213123; ) make them private, and provide getproperty methods. Making variables public instead of making them private tells me your are coding lazily. If you wan't them to be inherited, make them protected, which means that inherited methods will have access to them. For example,
int getHealth() const {
return health;
}Now, C# has something called a property, which I really like, but unfortunately, they aren't available in C++ ![]() PS: I hope I don't come off as too critical. Besides what I mentioned, everything looks fine. Keep at it and don't get discouraged. ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! Last edited by Jessehk; Jan 7th, 2006 at 11:09 PM. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
@Jessehk thanks I will seperate the method definition. I've just starting working with classes seriously.
P.S. methods were not originally in my plans at all I just saw how dandy it would be.
__________________
Geeks may not be cool now but in the long run they prosper. |
|
|
|
|
|
#4 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Quote:
Can I ask why your class members are all public? Maybe I can help you work around it. ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4
![]() |
why don't it make it easier for yourself by creating just one class and name it class Player. then you can create instances of the class for each player, rather than have repetitive code. then you can just do
Player Saucer; Player Hero; Player MotherShip; |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
Would it be acceptable to link a source file to it with the definitions? Also they have different requirements for the constructer and have slight variations in nameing in some areas I started trying it with inheritance maybe I'll do it next time. Saucer and Mothership are reserved for control by the computer they will eventually be upgraded to hold coordinates, firing info, and AI. This is just getting data management set up before the graphics.
__________________
Geeks may not be cool now but in the long run they prosper. Last edited by teencoder; Jan 7th, 2006 at 11:55 PM. |
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
I wanted to interface with them directly when necessary.
__________________
Geeks may not be cool now but in the long run they prosper. |
|
|
|
|
|
#9 | ||||||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
Quote:
Quote:
In the process of initially implementing a class, and getting it working right, it is often a good practice to have no implementation of functions in a header file. But, once that is done, it can turn out that judiciously inlining a few functions can help the compiler produce an executable that runs faster. In practice, it often turns out that setter and getter functions (functions that set or retrieve the value of a private variable) are good candidates for inlining, particularly if those functions only do minor error checking. And, of course, there are template classes or functions: their implementations are inline, more often than not. Quote:
The type of example you're looking for would be of the form; an_instance_of_saucer.health = 123; Quote:
I don't interpret making member variables public rather than private as a sign of laziness. It is, for example, essential if one is calling a function written in C from a C++ compiler. Your basic principle (make your members private, and then provide public setters and getters) is a good way to achieve encapsulation eg. ensuring that members cannot be changed in invalid ways. But encapsulation is a technique: it is up to the person implementing a class to decide if encapsulation is required. Quote:
an_object.SomeProperty = 42;
value = an_object.SomeProperty;an_object.SetSomeProperty(42); value = an_object.GetSomeProperty(); an_object.SomeProperty++; Quote:
Basic message: a lot of what you stated are good guidelines but, in some cases, it is necessary to break guidelines, not state them as absolute requirements. |
||||||
|
|
|
|
|
#10 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Quote:
)).
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|