Jump to content
  • Advertisement
Sign in to follow this  
Silicon Viking

C & OOP

This topic is 3913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

C isn't an object-oriented language, but I can do this:
struct troll
{
    char name[50];
    int hunger, strength, intellect;
}

struct troll jim = {"Jim", 0, 10, 3};

jim.name = "Larry";

(Please correct my coding if necessary.) It looks a lot like OOP. There's no classes or inheritance, so is that why it's not OOP? So what gives? If I can do that, can I stick functions in there?

Share this post


Link to post
Share on other sites
Advertisement
There are 4 major principles that make a language object-oriented: Encapsulation, Data Abstraction, Polymorphism and Inheritance.

What you have there does not even satisfy one of those.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by Silicon Viking
If I can do that, can I stick functions in there?


No. If you could, it would be called a method :D

Share this post


Link to post
Share on other sites
Real object oriented programming involves several principles. The important ones are the Dependency Inversion Principle (DIP), the Interface Segregation Principle (ISP), the Liskov Substitution Principle (LSP), the Open-Closed Principle (OCP) and the Single Responsibility Principle (SRP). A design utilizing all of these principles can be implemented in C. However, the amount of code necessary is often quite large. For example, Liskov Substitution Principle basically says that if you have a function that works with a pointer to an object of a base class must be able to work with a pointer to an object of a derived class without knowing it. To do so in C essentially involves writing, by hand, the a virtual function mechanism for each and every one of your object types. It can be done. It just isn't pretty or particularly time efficient.

Share this post


Link to post
Share on other sites
Quote:
Original post by Silicon Viking
I'm trying to create a roguelike and I'm new to C. I'm not sure how I'm going to go about defining the objects in the game without OOP.

Good God. Why in the world would you use C? Or C++, for that matter?

Share this post


Link to post
Share on other sites
First, why C?

Second, why OO? Structured programming is perfectly valid. Some crazy people even prefer it to OO. If you're trying to learn pure C for whatever reason, doing pseudo-OO isn't of much benefit. Round pegs and square holes and all.

For more details on why a C struct is just a collection of data and has nothing to do with OOP, feel free to peruse Wikipedia.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Silicon Viking
I'm trying to create a roguelike and I'm new to C. I'm not sure how I'm going to go about defining the objects in the game without OOP.

Good God. Why in the world would you use C? Or C++, for that matter?


Tradition. Rogue was programmed in C. If not C or C++, what then would you suggest?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!