What are these...

Started by
7 comments, last by Maledict 18 years, 8 months ago
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?
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
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]
#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.
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.
my siteGenius is 1% inspiration and 99% perspiration
So, a class is kind of like a struct?
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
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).
structs and classes are the same except classes can do more
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.
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 definitionclass TestClass{public:   TestClass();  // constructor run automatically when class instanced   ~TestClass(); // destructor NOTE: never takes arguments   void randomMemberFunc(int z); // function prototypeprivate:   // can only be seen by member functions of classes, hence 'private'   int a;   int b;   int c;}; // end class definition// constructor function definitionTestClass::TestClass(){   cout << "Welcome to the constructor!" << endl;}// destructor function definitionTestClass::~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

This topic is closed to new replies.

Advertisement