Game Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesGame Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 28th, 2006, 06:23 PM
Spechal Spechal is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Overland Park
Posts: 170 Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level) 
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;
}


Should I be looking into ObjC?

TIA.

Reply With Quote
  #2  
Old June 28th, 2006, 07:12 PM
para45's Avatar
para45 para45 is offline
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 264 para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
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.
Comments on this post
pumpkin head agrees: a very good explanation

Last edited by para45 : June 28th, 2006 at 07:15 PM.

Reply With Quote
  #3  
Old June 28th, 2006, 07:47 PM
Spechal Spechal is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Overland Park
Posts: 170 Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level)Spechal User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 12 h 42 m 8 sec
Reputation Power: 18
Quote:
Originally Posted by para45
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.


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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesGame Development > C Oop?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap