Discuss C Oop? in the Game Development forum on Dev Shed. C Oop? Game Development forum covering non language specific programming - game creation, design, modding, theories and math. A place for developers and gamers of all levels to discuss and debate all things involved in game creation and modding.
Posts: 170
Time spent in forums: 1 Day 12 h 42 m 8 sec
Reputation Power: 18
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;
}
Posts: 264
Time spent in forums: 6 Days 2 h 28 m 19 sec
Reputation Power: 123
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.