Game Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
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  
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: 107 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: 21 h 54 m 30 sec
Reputation Power: 13
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: 261 para45 User rank is Sergeant Major (2000 - 5000 Reputation Level)para45 User rank is Sergeant Major (2000 - 5000 Reputation Level)para45 User rank is Sergeant Major (2000 - 5000 Reputation Level)para45 User rank is Sergeant Major (2000 - 5000 Reputation Level)para45 User rank is Sergeant Major (2000 - 5000 Reputation Level)para45 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 6 Days 1 h 23 m 59 sec
Reputation Power: 43
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: 107 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: 21 h 54 m 30 sec
Reputation Power: 13
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?


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway