|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
C Oop?
Hello all. I am fairly new to C and am wanting to get my feet wet by recreating a popular game I used to play on my calculator. HickQuest.
I am coming to C from PHP and was just starting to get into OOP and using classes more and more often. I am fearing that I may have to use C++ for this project of mine instead of C, which is what I want to use. Only C. I am just learning to use structs (which seem much like classes to me) and am having a hard time figuring out how to link everything to gether much like you would a class. Sadly, this is all I have come up with before having to post here. Code:
#include <stdio.h>
#define deadLife 0
void Pause(void){
char *c;
printf("Press Enter to continue . . .\n");
while((c = getchar()) != '\n'){ }
free(c);
}
typedef struct {
char *name;
int minPower, maxPower;
int maxLife, currentLife;
char *currWeapon, *currShield;
} Enemies;
int main(void){
printf("Hello.\n");
Pause();
return 0;
}
Should I be looking into ObjC? TIA. |
|
#2
|
||||
|
||||
|
A class (without all the bells and whistles of polymorphism and inheritance) is just a C-structure that has assoicated member functions. Then the class itself is merely just a collection of member data, same as a C-struct. In C++ the functions assoicated with a class are only accessable by calling them on a class instance, so if you were to make normal functions that operate on a structure you can get the same effect.
For example, in C++ a class may be defined as: Code:
class square {
public:
square(int h, int w);
int area();
private:
int height;
int width;
}
square::square(int h, int w) {
this->height = h;
this->width = w;
}
int square::area() {
return (this->height * this->width);
}
int main() {
square s(10, 10);
std::cout << "Area is " << s.area() << "\n";
return 0;
}
In C this could be implemented as: Code:
struct square {
int height;
int width;
}
void square_init(struct square* s, int h, int w) {
s->height = h;
s->width = w;
}
int square_area(struct square* s) {
return (s->height * s->width);
}
int main() {
struct square s;
square_init(&s, 10, 10);
printf("Area is %i\n", square_area(&s);
return 0;
}
I would normally put all the square related functions in square.c and prefix them all with "square_", but that's just me. In C++, class member functions are implemented in the same way as the above C code, where there is a hidden "this" parameter that is passed. For example Code:
square::square(int h, int w) becomes Code:
square::square(square* this, int h, int w) The "this" pointer is a pointer to the object, in s.area() it's &s, or s->area() s. Last edited by para45 : June 28th, 2006 at 07:15 PM. |
|
#3
|
|||
|
|||
|
Quote:
All that bolded text made something click. I am a lot better off from where I was before, but still havent coded anything with this new information. Thanks a lot. It really was a good explanation. The relations between C and C++ helped alot. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Game Development > C Oop? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|