Computer Science Canada A Start on C++ Some First Programs/Questions |
Author: | [Gandalf] [ Sun Jun 26, 2005 4:30 pm ] |
Post subject: | A Start on C++ Some First Programs/Questions |
Well, I have decided that my lofty goal for the summer is to learn C/C++ and VB.NET. Well, I'm hoping to get at least a basic knowledge of one of them, so I'm starting with C++ (I've already tried C). I will post some of the programs I make and questions I have over here till I give up (which will hopefully not happen). If I can't do C++ then I'll fall back to C, and if not that then I'll fall back to either VB.NET or Ruby. Well, alright, I think I have an alright understanding of cout and cin, and now I'm going into classes and objects. Then I will go to constructors/deconstructors, multiple objects of the same class, private members and friend functions, inheritance, virtual funtions, and pure virtual functions, then file objects and by then we'll see... If there are any tutorials you suggest on this site or on the internet then please suggest them since I need all the help I can get! I was looking at inline assembly, and heck - it doesn't seem too hard. Once you get past that barrier then inline machine code doesn't seem all that bad either. But then again, that's just me and my insane first impressions. Ok, I'll post some of my code when I reinstall my editor/compiler, but first I have a simple newb question. Can you declare the value of a variable inside a class in C++? I see how it's not neccessary, but I'm just not sure if its possible. Oh, and if you know of any good reference guides to C/C++ then I would be really happy too . *EDIT* Oh, and is it wise to use some C libraries while coding in C++? Or should you just use pure C++? Are some C libraries also considered C++ ones? *NOTE TO SELF* g++ name.cpp -o name.exe gcc name.c -o name.exe gcj name.java -o name.exe? |
Author: | Cinjection [ Sun Jun 26, 2005 4:40 pm ] | ||
Post subject: | Re: A Start on C++ Some First Programs/Questions | ||
[Gandalf] wrote: Can you declare the value of a variable inside a class in C++? I see how it's not neccessary, but I'm just not sure if its possible. no, but that's what constructers are for. [Gandalf] wrote: Oh, and if you know of any good reference guides to C/C++ then I would be really happy too . www.cprogramming.com [Gandalf] wrote: *EDIT* Oh, and is it wise to use some C libraries while coding in C++? Or should you just use pure C++? Are some C libraries also considered C++ ones? You can, but you can turn C libs into C++ libs by adding a c infornt and getting rid of the .h. So
That won't always work but in most cases it will. Need more help , just ask. |
Author: | [Gandalf] [ Sun Jun 26, 2005 4:58 pm ] | ||
Post subject: | |||
Thanks. What compiler/editor should I use? I had a good one for C, but it doesn't work with C++. Do you know of a good, easy to use editor with a built in compiler? Free, might I add. *EDIT* Oh man, I forgot so much, even about C. I took a basic "hello world" program just to test my C and it doesn't show hello world when compiled .
It just says return code, execution time, and location of program. |
Author: | 1of42 [ Sun Jun 26, 2005 5:18 pm ] |
Post subject: | |
http://www.bloodshed.net/devcpp.html Dev-C++ is a great IDE. |
Author: | Cinjection [ Sun Jun 26, 2005 5:19 pm ] |
Post subject: | |
I use MVC++. But you have to pay to get that one. If you want a good free compiler you can use Dev C++(googleafy)(<--crazy me making new words). |
Author: | Cinjection [ Sun Jun 26, 2005 5:22 pm ] | ||
Post subject: | |||
Oh and here's Hello World in C++:
|
Author: | [Gandalf] [ Sun Jun 26, 2005 5:45 pm ] | ||||
Post subject: | |||||
Thanks! Oh, and for Hello world, couldn't you just use this:
or wtd's way:
I'm installing it right now. |
Author: | Cinjection [ Sun Jun 26, 2005 5:49 pm ] |
Post subject: | |
Well ANSI standard says not to include the .h. You also need cin.get() to pause the program or else it will instently close. fflush() was thrown in there for good practice. |
Author: | [Gandalf] [ Sun Jun 26, 2005 6:08 pm ] | ||
Post subject: | |||
I see, thanks for the explanation. Tell me what the problem is with this:
It says that cout and cin have not been declared . *EDIT* I found the problem, you DO have to have the .h after iostream, at least in my case. |
Author: | wtd [ Sun Jun 26, 2005 6:13 pm ] | ||||||||
Post subject: | |||||||||
Cinjection wrote: Well ANSI standard says not to include the .h. You also need cin.get() to pause the program or else it will instently close. fflush() was thrown in there for good practice.
"fflush" and "stdin" are C, not C++, and is therefore extremely bad practice. Especially when you mix the two. They are different languages. Treat them as such. Instead use the std::ostream class' "flush" member function. std::cout is an instance of the std::ostream class.
Flushing a buffer ensures that what is in it is actually output. For instance, on some systems with some compilers, nothing will be output until the end of the line, so if I were to ask for a person's name and get input on the same line, say with:
What I would see on the screen would look like the following, assuming the name I entered was "wtd".
Flushing the std::cout buffer after prompting for the name ensures this won't happen.
|
Author: | md [ Sun Jun 26, 2005 6:33 pm ] | ||
Post subject: | |||
[Gandalf] wrote: I see, thanks for the explanation.
Tell me what the problem is with this:
It says that cout and cin have not been declared . *EDIT* I found the problem, you DO have to have the .h after iostream, at least in my case. I think if you add the .h the file puts everything into the global namespace; whereas without the .h it's just in "std::". Of course I could be completely wrong What IDE/compiler did you chose? |
Author: | wtd [ Sun Jun 26, 2005 6:35 pm ] |
Post subject: | |
You should not by any means install Dev-C++. It's a buggy product, and makes the whole process far more complex than it need be. Instead, install GCC directly via MinGW (MInimalist GNU for Windows). You will then need to edit your PATH environment variable to have command-line access to the GNU C++ compiler. http://vlaurie.com/computers2/Articles/environment.htm |
Author: | [Gandalf] [ Sun Jun 26, 2005 6:39 pm ] | ||||
Post subject: | |||||
Dang, and it was a 50mb install. Well, for the moment what's wrong with this? It doesn't output the answer.
Oh, and this is the Hello World program Dev-C++ gives:
Right now I'm using Dev-C++, but I will probably switch to what wtd said, if I can get it to work. |
Author: | wtd [ Sun Jun 26, 2005 7:07 pm ] | ||||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||||
[Gandalf] wrote: Dang, and it was a 50mb install.
It's a good lesson to learn early. [Gandalf] wrote: Well, for the moment what's wrong with this?
Several things. Let's take a look.
"iostream.h" is deprecated. Whgile most compilers still do support it, there's no guarantee they will. Use "iostream" instead.
Useless comment.
"main" should return "int".
You're mixing styles and using extraneous comments. I can gather from the following code that the user inputs values which get stored in those variables. If you're going to declare more than one variable in one statement, there's no point in putting the declaration of slope in its own statement. You can still have it on a separate line. However, you also don't need to declare slope here. You can declare it when you initialize it. C++ does not require declarations at the top of a function.
Nowhere have you specified that cout or cin have been brought into your namespace, from the std namespace where they dwell.
Also, that aside, nowhere do you check to make sure reasonable values were stored in the variables.
Hey look! We're finally putting a meaningful value in slope. Instead of simple assignment here, let's declare slope here too.
Now, let's use initialization syntax instead of assignment. Good practice to get into. You can only do this at declaration, though.
But there's still a likely problem. In C++, when you divide an int by an int, you get an int. You likely want to be able to get a floating point number, to represent slopes like 1/2. To do this our slope should have a type of double, and we'll have to cast at least one of the argument to double as well.
Again, specify that cout and cin live in the std namespace. Also, when you want a newline, do something like the following instead.
And that's all for now. |
Author: | 1of42 [ Sun Jun 26, 2005 7:12 pm ] | ||||||||||||||||||||
Post subject: | |||||||||||||||||||||
For starters, I would personally ignore wtd. His idea of easy is to have to use the command line to get to the directory containing your files, and to compile everything manually. While less obfuscated, it is in fact not easier. Personally, I think you're better off with an IDE - especially one like Dev-C++, where you can ignore projects and other crap until you're comfortable with the language itself. Just press F9 and you're set. On to your question:
That's bad. Remove the .h to make:
C++ header files do not require the .h at the end.
Should be:
Your program must return a status to the operating system at the end of execution. So add this at the end:
This indicates successful completion Now you must use your identifiers. Either:
or
or qualifying all your identifiers, like so:
Also, make slope a double so you get decimals:
Finally, after all that is corrected: Create a new variables and cin to it:
This will pause it. *edit* Damn, wtd got to it first Just to note, wtd, that still doesn't solve the problem of the program finishing execution too fast to see, nor does it return a value to the OS, making the program fail compilation. |
Author: | Cinjection [ Sun Jun 26, 2005 7:25 pm ] | ||
Post subject: | |||
wtd, the kid said he just started C++. Why are you getting into typecasting and initilizing in his like second program? Also for crist's sake USE namespcae std. At least if you don't to fully use it, go
Its annoying as hell, and is really not nessicery. |
Author: | wtd [ Sun Jun 26, 2005 7:34 pm ] | ||||
Post subject: | |||||
Cinjection wrote: wtd, the kid said he just started C++. Why are you getting into typecasting and initilizing in his like second program? Also for crist's sake USE namespcae std. At least if you don't to fully use it, go
Its annoying as hell, and is really not nessicery. For one thing, what you posted isn't even remotely valid code. I think you want:
And it's important for new programmers to see "std::" appended to things. It's vital to know where these things are coming from. Type-casting is a part of the C++ language, and an important part. How else would you propose he get division of two integers to return a (meaningful) floating-point value? Initialization vs. assignment is also a vital concept to understand. Of course, these little subtleties are a damn good reason why C++ makes a poor choice for a language one learns early in their career. Make sure you know what you're talking about before trying to give advice. |
Author: | Cinjection [ Sun Jun 26, 2005 7:36 pm ] | ||
Post subject: | |||
This is nice and easy and it works syntax wise. It sould also be correct in terms of logic, considering i wrote my equation of the line exam last wendsday .
EDIT: Sorry about the std::cout thing. Mind's in the gutter! |
Author: | wtd [ Sun Jun 26, 2005 7:41 pm ] | ||||
Post subject: | |||||
At the very leat make the above look like:
For the love of everything good and wholesome, use spaces around operators, people. |
Author: | [Gandalf] [ Sun Jun 26, 2005 7:56 pm ] |
Post subject: | |
First off, wow, thanks for all the help! Quote: His idea of easy is to have to use the command line to get to the directory containing your files, and to compile everything manually. While less obfuscated, it is in fact not easier. Personally, I think you're better off with an IDE - especially one like Dev-C++, where you can ignore projects and other crap until you're comfortable with the language itself. Just press F9 and you're set.
Well, personally, one of the bigger problems I had with DevC++ is that you need projects. Are you telling me that you don't? I don't know, I still have to make up my mind... I'll try comprehending all this while still learning new stuff. There's so much things, and yet I have things like class' and constructors ahead of me. |
Author: | Cinjection [ Sun Jun 26, 2005 8:06 pm ] |
Post subject: | |
wtd wrote: For the love of everything good and wholesome, use spaces around operators, people. Now to actually draw my line. Where'd I put my DirectX book..... |
Author: | wtd [ Sun Jun 26, 2005 8:15 pm ] |
Post subject: | |
Cinjection wrote: wtd wrote: For the love of everything good and wholesome, use spaces around operators, people. Now to actually draw my line. Where'd I put my DirectX book..... That explais your love of mixing C and C++. Microsoft has always had a hard time figuring out that C++ is an entirely new language that just happens to look a lot like C. |
Author: | Cinjection [ Sun Jun 26, 2005 8:27 pm ] |
Post subject: | |
wtd wrote: Cinjection wrote: wtd wrote: For the love of everything good and wholesome, use spaces around operators, people. Now to actually draw my line. Where'd I put my DirectX book..... That explais your love of mixing C and C++. Microsoft has always had a hard time figuring out that C++ is an entirely new language that just happens to look a lot like C. heh. I hope you're not one of those anti-windows nazis....are you? |
Author: | wtd [ Sun Jun 26, 2005 8:35 pm ] |
Post subject: | |
Cinjection wrote: wtd wrote: Cinjection wrote: wtd wrote: For the love of everything good and wholesome, use spaces around operators, people. Now to actually draw my line. Where'd I put my DirectX book..... That explais your love of mixing C and C++. Microsoft has always had a hard time figuring out that C++ is an entirely new language that just happens to look a lot like C. heh. I hope you're not one of those anti-windows nazis....are you? I am. Unlike a lot of people I hate it for the right reasons, though. That said, I care more about helping people learn programming than convincing them to switch OSes. Plus, GCC runs just fine on Windows. A testament to the power of OSS. |
Author: | 1of42 [ Sun Jun 26, 2005 9:40 pm ] |
Post subject: | |
[Gandalf] wrote: Well, personally, one of the bigger problems I had with DevC++ is that you need projects. Are you telling me that you don't? I don't know, I still have to make up my mind...
I'll try comprehending all this while still learning new stuff. There's so much things, and yet I have things like class' and constructors ahead of me. Nope, you don't need projects. Press Ctrl+N for a new source file, then save it as you like and press F9 to compile and run it. Projects are only really necessary when you have lots of different source fiels, and when you're creating WinAPI or OpenGL/DirectX apps, which need to have a project of type Windows Application to look right (no unnecessary console) |
Author: | md [ Sun Jun 26, 2005 10:08 pm ] |
Post subject: | |
Just a side note about declairing variables; while C++ allows you to declare a variable anywhere in your function it's still best to declare the variable at the top of the function. The reason? Say you go back and change some code, if you happen to use a variable of the same name in your new code as a variable that is declared later in your old code you'll get a compile error. While it's simple to fix it's still a pain to have to go back and fix that one thing because you couldn't keep track of your variables. |
Author: | [Gandalf] [ Sun Jun 26, 2005 10:11 pm ] | ||
Post subject: | |||
Cinjection wrote: wtd wrote: For the love of everything good and wholesome, use spaces around operators, people. Now to actually draw my line. Where'd I put my DirectX book..... Does C++ have any basic drawing functions? I know C allows you to draw simple shapes and stuff like that without using DirectX or OpenGL. Ok, here's my new program, the sum of my trials today. I don't understand some of the stuff in it yet, but I will make every effort to .
It should work, if it doesn't well... Let's hope its a result of my tiredness. Thanks again to all those who helped today and will later on as well! |
Author: | wtd [ Mon Jun 27, 2005 12:34 am ] |
Post subject: | |
Cornflake wrote: Just a side note about declairing variables; while C++ allows you to declare a variable anywhere in your function it's still best to declare the variable at the top of the function. The reason? Say you go back and change some code, if you happen to use a variable of the same name in your new code as a variable that is declared later in your old code you'll get a compile error. While it's simple to fix it's still a pain to have to go back and fix that one thing because you couldn't keep track of your variables.
Actually, declaring at the top of the function is not a good practice to get into. As you say, the compiler will track down errors for you. Use the compiler. It has no feelings, and doesn't mind hard, tedious work. The problem is that you should get into the habit of using "const" as much as possible. That is, variables should primarily be viewed as convenient names given to constant values. This way the compiler can track down any unintended changes in the variable's content. If you separately declare and initialize your variables, this becomes impossible. |
Author: | wtd [ Mon Jun 27, 2005 1:32 am ] | ||||||||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||||||||
Let's look at your class.
First, let's just deal with the class. We can create the "number" instance of the class later, in "main", like so: [syntax="cpp"]sum number;[/code] We're left with:
Now, we can have classes where all members (both data and function) are public. But a shortcut for that is to call the class a "struct". In C++ classes and structs are identical, except that members are public by default in structs.
But that's not right. The variable "current" shouldn't be accessible from outside the class, because that means we can change it in all kinds of uncouth ways. So we make the variable private, and the functions which manipulate it public. Oh, and the "value" passed to "add" can be a consant int.
Of course, now you ask, how do I initialize a sum object? For that, we use a constructor. We'll have both a default constructor that takes no arguments, and one which takes an initial value.
Now we'll define the constructors.
Now, when we declare "number" like so:
"current" gets set to zero. Or we could declare and initialize like so:
Here "current" is initialized to 42. Of course, we'll need a way to get at the current value. I know! How about a "current_value" member function?
Of course, we aren't changing anything about the object when we call the "current_value" function. So we tell the compiler that it's ok to call that function on a constant sum object.
Now, that's pretty good. What if we want to sum floating-point numbers? We could write a whole new class. It'd be mostly copy and paste. But, isn't there a better way? There is. We call them templates.
Now I can have "number" again by writing: [syntax="cpp"]sum<int> number;[/code] Of course, the next question is... why have an "add" function. That sounds and awful lot like the + operator.
Now, to rewrite your original program.
But we've got some really inefficient stuff like the code required to read in a value and put that in "current". As well, on output, calling current_value() looks bad. Let's streamline that.
|
Author: | wtd [ Mon Jun 27, 2005 1:36 am ] | ||||
Post subject: | |||||
Of course, if we put all of the class into a file named "sum.h".
Then our program can look like:
|
Author: | [Gandalf] [ Mon Jun 27, 2005 6:54 pm ] | ||
Post subject: | |||
Yes... ::blank face:: Thanks for fixing my program... Ok, now I spend the next 2 years trying to figure all that out. I got lost somewhere between defining constructors and the rest of the explanation. I'm learning from a book too, and I was just starting constructors. I think you just got to about the end of the book and beyond (its mostly about C, but it has some C++). Well, ok, erm... I guess I will try understanding all that. One step at a time. I was aware of private, public, and protected variables and member functions, but the book just left it at public till a bit later. Your final class is very confusing, I will read your post a few more times. Do you think I should leave classes till later? Or well, what do you suggest? Should I try and push myself to figure out everything you posted? From what I have right now, this is how I would edit the program.
Am i correct in assuming that when you do something like sum(void); and sum(); that they are the same? Also, I also realized that you can declare the object in three different kinds of ways (from what I know). Personally, I don't know the difference yet, but having it right after the class seems simplest to me. Thanks for the detailed help. I'll get back to this later. Oh, and importantly, the thing that got me lost at the constructors was that you were declaring it with arguments? So what would that do, allow you to set the initial value of "current"? |
Author: | wtd [ Mon Jun 27, 2005 9:03 pm ] | ||||||||
Post subject: | |||||||||
Yes, a constructor that takes arguments can be used to initialize member variables. Since your member variables are almost always going to be private or protected, this should be the only way you have available to initialize them. For instance, you have "current" being private, but then you try to access it externally:
The private qualifier means that "current" is inaccessible from outside the class. This prevents any bit of code from changing the internal state of an object in weird ways. One classic example is a car. Let's give our car four wheels. Let's also make everything public.
Now, maybe I'll add an increase speed function.
But, we can still individually access the wheels.
Now, what happens when we have three wheels going 40KPH, and one going 120KPH? Bad, bad things happen. If the wheels array was private, it wouldn't be a problem. |
Author: | [Gandalf] [ Tue Jun 28, 2005 3:28 pm ] | ||
Post subject: | |||
Ok, so how would I change "current" in my program, should I make a seperate member function in the class, or something else? This is what I was working on today, I think I got the while loop in proper C++ style.
|
Author: | wtd [ Tue Jun 28, 2005 4:24 pm ] | ||||
Post subject: | |||||
Well, there are several points I should make. General Theory First off, in object-oriented programming, we have both member variables, and member functions. Understanding the different roles of these is critical. Member variables exist to keep track of the internal state of an object. In the car example, we had a 4 element array of integers to represent the speed of each of the wheels on a car. Member functions exist to manipulate those variables in a controlled way. Because of this relationship, we want member variables generally to be private, and thus inaccessible from outside the class, and member functions to be public. On Constructors & Destructors Constructors and destructors should not print output. This makes it impossible to use them in places where you don't want that output. Constructors should always use initialization syntax unless it's not possible. Rather than:
I should use:
Destructors should only be used where you have some dynamically allocated member variable(s). Otherwise there's no need to create a destructor. Member Names Member functions names should fall into a few categories. Functions swhich return some value should have noun names. You may wish to prefix these with "get" in some way. Member functions which return a boolean value should have names starting with something like "is" or "has". Member functions that change something about the object should have verb names like "add", "subtract", etc. |
Author: | [Gandalf] [ Thu Jun 30, 2005 9:16 pm ] | ||
Post subject: | |||
Ok, I understand that, thanks. I'm not sure though. I can't make up my mind as to which language I should learn. Do you think I am getting a pretty good start on C++? Or should I try going down a step to C, without worrying about all the object-oriented stuff? I was also looking at Java, it seems a bit easier, and closer to Turing, which I already have some experience with. For example this:
Not sure, C++ seems useful, and I think I know most of what I've read, but it might get too complicated later on... *sigh* I have no idea... I might just try a little bit of everything and see which works best? I've gained a lot of programming knowledge since I last tried Java, and C as well. |
Author: | wtd [ Thu Jun 30, 2005 9:40 pm ] |
Post subject: | |
Well, C++ and Java may seem useful to you, but here's a handy tip: if it frustrates you to the point where you never get to the useful stuff... then it's not useful to you. I say keep going. You're doing well. Just take it one step at a time, and if you have questions... ask! |
Author: | [Gandalf] [ Fri Jul 01, 2005 7:35 pm ] |
Post subject: | |
Alright then. Something that came across me when writing my small answers to your questions was; the reason that my program doesn't output the numbers properly is probably because it is using the postfix form of incrementing. So, how would I use the prefix form when I'm not doing it by 1, but by a variable? The 'problem' i speak of is that it doesn't output the final value of the counter. Also, what does "static" mean? When you say something like static int 4; or something of that sort. I am having trouble changing everything from public in my previously posted program. Could you give an example of how it would work in this case? I'll have to work on it some more. Also, what is an "object stack"? Is it just a group of objects from the same class? What do "[]" those mean as opposed to "()"? |
Author: | wtd [ Fri Jul 01, 2005 7:55 pm ] | ||||||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||||||
[Gandalf] wrote: Alright then.
Something that came across me when writing my small answers to your questions was; the reason that my program doesn't output the numbers properly is probably because it is using the postfix form of incrementing. So, how would I use the prefix form when I'm not doing it by 1, but by a variable? The 'problem' i speak of is that it doesn't output the final value of the counter. I know this doesn't directly answer your question, but you should avoid use of global variables, as you did in the code you posted. Variables should be declared with functions, and then passed to other functions as arguments. Now this becomes slightly different when dealing with objects, where the object's state is maintained by variables declared outside of a function's scope. So, you want to write a counter class?
There's the basic framework. Now, we want a constructor which setsup the class with a default state.
And we'll want a constructor to setup the object with an initial value of our choosing.
We don't need to allocate any memory, so we don't need a destructor. We will want to be able to get the current value of the counter object, so we'll have a member function to retrieve that information.
Now, we want a way to increment the counter by one.
And we'll overload that member function's name so we can deal with incrementing by an arbitrary amount.
Now, let's implement this. But of course, we need some state to modify first.
Our default constructor, which sets value to zero:
Our other constructor:
The member function to retrieve the current value:
And the increment member functions, with one defined in terms of the other:
Now, taking this, we can declare a counter in our main function, with an initial value of 0, and loop until counter is 9, incrementing by 1 each time.
We could also put ths into for loop format:
|
Author: | [Gandalf] [ Sat Jul 02, 2005 4:06 pm ] | ||
Post subject: | |||
I'm pretty sure I got it. Still, I'm not really sure why and when to use classes, would it really be useful in that case? Here, tell me if I got the whole passing to other functions as arguments things right. This is the guessing game I was working on yesterday. It works, but it seems like more trouble than it should be, maybe because I overused functions?
I spent a long, long time on that . |
Author: | wtd [ Sat Jul 02, 2005 4:33 pm ] | ||||||
Post subject: | |||||||
Speaking of places where you might want to use classes... Notices how you pass along a min/max combination of numbers frequently? Well, what if we wrap that up into one package?
Of course, we can realize here that the state of the member variables will never change, so why not make them constant.
And if the member variables can't be changed after initialization, why not allow direct access to them?
|
Author: | [Gandalf] [ Wed Jul 06, 2005 9:32 pm ] | ||
Post subject: | |||
I see, so in this case it wouldn't matter that you can access the variables outside of the class/struct? The thing that really held me back from understanding your class/struct was the syntax you are using. A question, is there a more 'convenient' way of defining many things as one... This is what I mean, is there a way to change it?
|
Author: | 1of42 [ Wed Jul 06, 2005 9:54 pm ] | ||
Post subject: | |||
[Gandalf] wrote: I see, so in this case it wouldn't matter that you can access the variables outside of the class/struct? The thing that really held me back from understanding your class/struct was the syntax you are using.
A question, is there a more 'convenient' way of defining many things as one... This is what I mean, is there a way to change it?
Well, you could make a const array of strings, but jut for the record, your code there is not valid - the preprocessor will overwrite each successive definition after it's defined, only "y" and "n" will remain defined. |
Author: | wtd [ Wed Jul 06, 2005 10:10 pm ] | ||||
Post subject: | |||||
Please don't use preprocessor macros. As 1of42 alluded to, use an array, or some other kind of collection-ish thing. An array:
And then to check and see if we have a valid answer...
|
Author: | [Gandalf] [ Wed Jul 06, 2005 10:21 pm ] |
Post subject: | |
Thanks! I wasn't thinking 'outside the' little box I created for myself . Should I never use macros? or just not in this way? |
Author: | wtd [ Wed Jul 06, 2005 10:24 pm ] |
Post subject: | |
[Gandalf] wrote: Thanks! I wasn't thinking 'outside the' little box I created for myself . Should I never use macros? or just not in this way?
Try not to use them ever in C++. The biggest problem, as I see it, is that with macros you end up seeing a different piece of source code than the compiler. |
Author: | md [ Thu Jul 07, 2005 9:37 am ] |
Post subject: | |
One of the most annoying bugs I've ever run across was because some bright light as MS decided that instead of making teh GetObject function a real function (it's part of the GDI) they'd make it a marco to the function apropriate to if you were using a wide characters or not. So when I wrote my own GetObject function as part of one of my classes which used something in windows.h, which pulled in the GDI (bah!) it broke because of the stupid redefinition of some moron. So yeah... preprocessor macros == bad. |
Author: | [Gandalf] [ Fri Jul 08, 2005 10:26 pm ] | ||||
Post subject: | |||||
Ok, for now I'll hold of on them. I'm confused as to why some books start out with just a simple int main(); function, and why some do something weird (to me) like:
Also, I read that you can simple put the function above the main() one, and you don't have to declare it, like so:
I haven't been able to try compiling it (lack of access) but if it does work then which method is better? *edit* Oh, and I remember wtd mentioning that arrays are bad and one reason was because you can't tell the upper bounds (I think that was it). Can you just have a dynamic array and check that variable for the size? |
Author: | wtd [ Fri Jul 08, 2005 10:48 pm ] | ||||||||
Post subject: | |||||||||
[Gandalf] wrote: Ok, for now I'll hold of on them.
I'm confused as to why some books start out with just a simple int main(); function, and why some do something weird (to me) like:
You can pass arguments to a program when you start it. You've most likely already seen this. You run "g++ my_program.cpp" to compile your program. In this case "my_program.cpp" is an argument to the program. How do you get at these arguments? Well, they get passed as arguments to the main function, as an array of character arrays (C "strings"). Since C and C++ arrays don't keep track of their size, it also passes in the size of the array as "argc". [Gandalf] wrote: Also, I read that you can simple put the function above the main() one, and you don't have to declare it, like so:
I haven't been able to try compiling it (lack of access) but if it does work then which method is better? It will work, and for programs of this size, it's largely a question of style. You see, the compiler needs two pieces of information about a function before another function can call it. It needs to know what type of data the function takes as arguments, and it needs to know what type of data the function returns. With this information it can check to make sure types are being used appropriately. Using a forward declaration along the lines of:
Gives the compiler that information. [Gandalf] wrote: *edit*
Oh, and I remember wtd mentioning that arrays are bad and one reason was because you can't tell the upper bounds (I think that was it). Can you just have a dynamic array and check that variable for the size? The best solution is to use the std::vector class. The std::vector class is a templated one. That means there's no actual executable code, but rather a "pattern" of sorts. Based on the template parameters, the compiler then generates the appropriate code at compile-time. This means there doesn't have to be a std::int_vector class and a std::double_vector class, and a std::string_vector class. So, using a std::vector...
|
Author: | [Gandalf] [ Sun Jul 10, 2005 4:27 pm ] |
Post subject: | |
Thanks again, always a help! So, is there a way to add in lots of 'grades' without having to write the grades.push_back every time? Other than a for loop? |
Author: | wtd [ Sun Jul 10, 2005 4:41 pm ] | ||
Post subject: | |||
[Gandalf] wrote: Thanks again, always a help!
So, is there a way to add in lots of 'grades' without having to write the grades.push_back every time? Other than a for loop? Not really, but as you say, with a loop it isn't much trouble.
|
Author: | wtd [ Sun Jul 10, 2005 4:57 pm ] | ||
Post subject: | |||
Of course, if you don't like creating the temp variable, you could always create your own class which wraps a std::istream.
untested |
Author: | [Gandalf] [ Sun Jul 10, 2005 5:07 pm ] |
Post subject: | |
Wow . Considering the first was at the limit of my knowledge, the seconds seems a bit complicated. Maybe I'll look back in a little while and understand . |
Author: | wtd [ Sun Jul 10, 2005 8:16 pm ] | ||||
Post subject: | |||||
Well, if it works, you could create a my_istream variable which wraps std::cin.
Then get an int from std::cin like so:
|
Author: | jamonathin [ Sun Jul 10, 2005 8:45 pm ] |
Post subject: | |
I read through this entire post just now, and you remind me of, well me when I first started this Gandalf. This is about where I left off. I got distracted by the games being made in Turing, and I spent all of my programming time on making Games and whatnot, simply because I didn't have the know-how in C++. It's good that you're using other resources to teach yourself. One website the I used a lot was this. This may/may not help you, but It helped me a lot. It mostly helped me because After you read the chapter and learned from it, it gave you sample progarms to try, and full solutions. Also, it's a lot quicker than posting on CompSci, and I felt that I became a nussiance. Not to knock on wtd. I mean, I don't know anyone else on CompSci that responds to any question faster than him. And very Detailed mite i add. He's without a doubt one of the most important mods here. But yeah, check out that site. You'll soon/may already be finishing it, but yeah. Good-Luck on it. Stick to it man, I'm comin back to it . . . later. . |
Author: | wtd [ Sun Jul 10, 2005 9:01 pm ] | ||||||
Post subject: | |||||||
jamonathin wrote: It's good that you're using other resources to teach yourself. One website the I used a lot was this. This may/may not help you, but It helped me a lot.
Unfortunately that resource is quite outdated.
As for this, it shows that the author doesn't know what you don't have to declare variables ahead of time in C++. It also demonstrates automatic conversion of integral data types, rather than the more sane explicit cast.
|
Author: | wtd [ Sun Jul 10, 2005 9:04 pm ] |
Post subject: | |
Most horrifically that tutorial uses char arrays instead of proper strings. This is inexcusable. |
Author: | jamonathin [ Sun Jul 10, 2005 9:11 pm ] |
Post subject: | |
wtd wrote: This is inexcusable.
What was I thinking, lol. Ok ok, here's a better site. I mainly used taht site for programs. As in, "Ok, make <this> program", and so on . . |
Author: | [Gandalf] [ Sun Jul 10, 2005 9:18 pm ] |
Post subject: | |
Well... Thanks for the explanation wtd, and the site jamonathin, although it might be out of date, I may be able to learn from it a bit, and it was an effort . All of that is just because it's old C++, right? Before they made the standard? That's why all the books and everythign I am learning from, I made sure it was all newer than 2002. They probably don't use typecasts right away because they want the person to understand the basics before going into more details. Yep, I know what you mean jamonathin, I want to learn C++, but I also want to use graphics and the sort. One step at a time though. Still, I might make a game in Turing in between... Oh, and wtd, do you know how to use colours in the standard console output? It's probably not that important, I know, but it would make the output more legible in larger amounts. Thanks for all the help. *edit* yes, this site is the most helpful one out there. Especially with wtd here to help you out with your C++ needs. |
Author: | wtd [ Sun Jul 10, 2005 9:27 pm ] |
Post subject: | |
[Gandalf] wrote: Well... Thanks for the explanation wtd, and the site jamonathin, although it might be out of date, I may be able to learn from it a bit, and it was an effort .
You'll have to unlearn most of what you learn from it. [Gandalf] wrote: All of that is just because it's old C++, right?
Some of it, probably, but there's also the fact that a lot of C programmers like to pretend that they know C++, just because of the syntactic similarities. [Gandalf] wrote: Oh, and wtd, do you know how to use colours in the standard console output? It's probably not that important, I know, but it would make the output more legible in larger amounts. Thanks for all the help.
Certainly no portable way. On Windows you could use the Win32 API to achieve this. One post on the subject found by Google. |
Author: | [Gandalf] [ Sun Jul 10, 2005 9:47 pm ] |
Post subject: | |
Too bad, at least it's possible... Also, while looking at the source code for one of my favourite games, I saw that it uses printf(); even though it's coded in C++. Some of the books I am learning from use it, and other C-like functions as well. I think they might be a part of the 'standardized' library too. Why? |
Author: | wtd [ Sun Jul 10, 2005 10:21 pm ] |
Post subject: | |
[Gandalf] wrote: Too bad, at least it's possible...
Also, while looking at the source code for one of my favourite games, I saw that it uses printf(); even though it's coded in C++. Some of the books I am learning from use it, and other C-like functions as well. I think they might be a part of the 'standardized' library too. Why? Backwards compatibility. Windows XP will let you run apps written for the 16-bit Windows API. That doesn't mean you should write them. |
Author: | [Gandalf] [ Tue Jul 19, 2005 2:12 pm ] |
Post subject: | |
What are pointers for? I was looking at the very basics of them, and I think I understand how they work, but why use them? When you are say, showing a variable, why not show the actual thing instead of the pointer? |
Author: | wtd [ Tue Jul 19, 2005 2:18 pm ] |
Post subject: | |
[Gandalf] wrote: What are pointers for? I was looking at the very basics of them, and I think I understand how they work, but why use them?
A pointer is just a variable containing a number, which happens to be the location of some value in memory. This becomes incredibly valuable, primarily because your pointer can be null. It can refer to nothing. A normal variable has to hold some kind of value. If you declare an integer, it has to hold a value. Even if that value is zero, it's still a value. But a pointer that's zero, well that refers to nothing. Nad, zilch, squat. And, you can can determine if it is null or not at run-time. [Gandalf] wrote: When you are say, showing a variable, why not show the actual thing instead of the pointer?
Could you please post an exact example? |
Author: | wtd [ Tue Jul 19, 2005 4:30 pm ] | ||
Post subject: | |||
wtd wrote: [Gandalf] wrote: What are pointers for? I was looking at the very basics of them, and I think I understand how they work, but why use them?
A pointer is just a variable containing a number, which happens to be the location of some value in memory. This becomes incredibly valuable, primarily because your pointer can be null. It can refer to nothing. It should be noted that this use of pointers becomes unnecessary ina language with support for variant types.
|