Jump to content
  • Advertisement
Sign in to follow this  
orcfan32

What are these...

This topic is 4842 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

I was wandering about something that I never understood. (Never wanted to understood for that matter.) What does #pragma do and what is a class?

Share this post


Link to post
Share on other sites
Advertisement
#pragma is a preprocessor directive which allows you to invoke compiler-specific commands. Hence, it's use is often discouraged due to portability issues.

Classes are more complex and difficult to explain simply. They are essentially the expression of a syndiffeonic relation between data and operations. That is, they relate data to operations to be performed on said data in an abstract sense.

More trivially, they can be interpreted as merely collections of (hopefully) related data. They serve common use in abstracting the representation of various entities in programming.

For example, a point in Euclidean 2-space is typically represented as having two Cartesian coordinates. Rather than type out and keep track of a pair of variables for every occasion upon which you need to store the data for a point, you can declare a class, here trivially interpreted as the aforementioned collection of data, which contains the x and y coordinates.

Instanciating said class then instanciates a copy of each variable contained within, and those variables are said to "belong" to that particular instance of the class.

Perhaps later you come to the realization that you often are computing the distance between two points. Rather than make a global function which takes as arguments two point classes, you can place this function in the point class itself. This reduces the parameter list to taking only the other point to which distance will be measured and makes more clear it's relevance.

You have now extended your point class from the trivial collection of data to a syndiffeonic relation of data and operations to be performed on said data, thereby abstracting the concept of point to a reusable data type much like the built-in types (int, float, etc.).

For detailed information, maybe look here.

[Edited by - nilkn on August 9, 2005 9:37:55 PM]

Share this post


Link to post
Share on other sites
#pragma is preprocessor directive that alters the way the compiler works. A fairly trivial example of its use is to turn off a specific compiler warning because you don't care about it.

A class is an abstract data type. It is the basic building block of an object oriented program.

Share this post


Link to post
Share on other sites
A class is kind of like, on the programming level, a variable that represents a bundle of variables. I think of it as a container for different variables. It also has functions that usually act on the variables that it stores. I think of it as like a sort of specialized way to store and manipulate something. Say you took the idea of a "Cat". What makes up a cat, that you care about for your program? Well, maybe its name, age, breed, hair color, and weight. Well, your "Cat" class would store all these variables. Each time you wanted to store information about a cat, instead of making all these unrelated variables, you just make a single cat. In your code, you are easily able to see how they are all related and how to manipulate them, and it is next to impossible to get the age of one cat confused with the age of another.

Imagine trying to store information on the contents of an entire pound by using separate variables, even if you were just storing, say, their age and weight. It is much easier if you can store 1000 animals, rather than 1000 animal ages and 1000 animal weights while trying to keep track of weight goes with which, and how to access them all correctly.

Share this post


Link to post
Share on other sites
Quote:
Original post by orcfan32
So, a class is kind of like a struct?


Exactly. In fact, for the most part a class is a struct. The difference lies in default member visibility. For a struct it is public, whereas in a class it is private.

I noticed in another of your posts that you use std::string. This is a class. You can trivially think of a class as a collection of data and related methods (functions).

Share this post


Link to post
Share on other sites
Quote:
Original post by theadamSGT
structs and classes are the same except classes can do more


No, structs and classes are the same except how you spell them and structs have default public members and inheritance and classes have default private members and inheritance.

Share this post


Link to post
Share on other sites
Quote:
Original post by orcfan32
I was wandering about something that I never understood. (Never wanted to understood for that matter.)

What does #pragma do and what is a class?


A class really is just an advanced structure.

Here's an example in code:


#include <iostream>
using namespace std;
// class definition
class TestClass
{
public:
TestClass(); // constructor run automatically when class instanced
~TestClass(); // destructor NOTE: never takes arguments
void randomMemberFunc(int z); // function prototype

private:
// can only be seen by member functions of classes, hence 'private'
int a;
int b;
int c;

}; // end class definition

// constructor function definition
TestClass::TestClass()
{
cout << "Welcome to the constructor!" << endl;
}

// destructor function definition
TestClass::~TestClass()
{
}

void TestClass::randomMemberFunc(int z)
{
a = 5; // we can use the data members
b = 6;
c = a * b;
z = c * 5;

cout << "c = " << c << endl;
cout << "z = " << z << endl;

}





int main()
{
TestClass myTestClass; // an 'instance' of the class which allows you to
// access member functions and data members

myTestClass.randomMemberFunc(6); // we can call a function from a classes
// instance using dot notation

cin.get();


}





Again, a class is essentially a struct, except we add in the ability to add functionality, as well as data. And we instance it just like a struct, and use dot notation to access stuff in the class instance.

There's also inheritance and things like that, which allow you to derive classes from other classes, but it's quite late here, and any explanation I might give, may not be of the best quality! :-)

There may be errors in the code, but I think it gets the basics across! (EDIT: I whipped it up as I was posting, but it seems to compile for me!)

If you have any questions, I'd be glad to help you out, just PM me, and I'll do what I can! :-)

Hope this helped,


Maledict

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!