|
|
C++ Learning
Last post 10-26-2009, 14:25 by sim656. 47 replies.
-
09-07-2008, 5:22 |
-
YourAngel
-
-
-
Joined on 08-24-2008
-
-
Junior Member
-
-
-
old karma : 0
-
|
Apparently it's still not working with using cin.get(); for me neither.
However, one more thing, i'm a bit confused about all this project stuff.
I've made a Project Named AngelHoofPP, everytime i open Visual C++ and click at my project, it launches the YourAge file (the program i'm working on). So i create a new file, FOR the AngelHoofPP project, i type in some HelloWorld code, and when i use debug, it launches the YourAge program instead!
This project business is confusing, very confusing! ![Surprise [:O]](/emoticons/emotion-3.gif)
 T's just a Flesh Wound!
|
|
-
09-07-2008, 7:22 |
|
|
I tend to find that system("pause") is more effective,but thats just me, it's also a lot cleaner to read, well I think so anyway. I know using Visual C++ is probably great, but I use an open source compiler, C++ has been standardised, so you basically get what you need in any ![G o o d [Good]](/emoticons/g_o_o_d.gif) compiler/IDE. C++ isn't that hard to pick up, I thoguht it was when I started, but to be honest, you just have to grasp the concepts of 'programming' (not specifically C++) and you'll start to understand the code. the best way to learn is to set yourself realisitc goals. if you've just started, your first goal might be to make something appear on the screen, goal two might be to add two numbers together then put them on the screen or something, and so on... You have to work at it in any case, but if you have a ![G o o d [Good]](/emoticons/g_o_o_d.gif) C++ reference (on paper or otherwise) and you can use your initiative, then you're all set! ![G o o d [Good]](/emoticons/g_o_o_d.gif) luck everyone (hope this was helpful)
|
|
-
09-07-2008, 7:59 |
-
09-07-2008, 11:36 |
-
Sarcross
A1MB0TT3R
-
-
-
Joined on 08-06-2008
-
Under your bed, behind that shoe no the other one.
-
Senior Member
-
-
-
old karma : 0
-
|
YourAngel:
Apparently it's still not working with using cin.get(); for me neither.
However, one more thing, i'm a bit confused about all this project stuff.
I've made a Project Named AngelHoofPP, everytime i open Visual C++ and click at my project, it launches the YourAge file (the program i'm working on). So i create a new file, FOR the AngelHoofPP project, i type in some HelloWorld code, and when i use debug, it launches the YourAge program instead!
This project business is confusing, very confusing! ![Surprise [:O]](/emoticons/emotion-3.gif)
I don't really know about creating new files inside of that project with another file already in it, so like the guy under the post I'm quoting said, use an open source compiler, it might be easier to do what you want, but I personally wouldn't trade my MS C++ for the world =) I have to try and see if I can figure something out.
|
|
-
09-08-2008, 9:45 |
-
09-08-2008, 10:42 |
-
Saucemasterpimp
The True Ninja Member
-
-
-
-
Shadows
-
Senior Member
-
-
-
old karma : 110
-
|
YourAngel:
Apparently it's still not working with using cin.get(); for me neither.
However, one more thing, i'm a bit confused about all this project stuff.
I've made a Project Named AngelHoofPP, everytime i open Visual C++ and click at my project, it launches the YourAge file (the program i'm working on). So i create a new file, FOR the AngelHoofPP project, i type in some HelloWorld code, and when i use debug, it launches the YourAge program instead!
This project business is confusing, very confusing! ![Surprise [:O]](/emoticons/emotion-3.gif)
For each .exe file, you'll probably want to create another project. You are compiling each time you make code changes, right?
The thing that is probably happening is that you have two int main() functions and the compiler is just choosing the original one.
If there are files in your original project that you need to put in your new project, simply copy and paste the original files into the new folder, then go to project>Add Existing files, then select the files you wish to include.
Philosophical Ninja.
|
|
-
09-08-2008, 15:57 |
|
|
no bother really, I think it's just a personal preference, also, I haven't really got deeply involved in using an Microsoft IDE, I mean I use visual basic in college, but you can hardly compare that to a C++ IDE, but I started with using an open source compiler, so it's just what i'm used to.
|
|
-
09-08-2008, 19:44 |
-
Sarcross
A1MB0TT3R
-
-
-
Joined on 08-06-2008
-
Under your bed, behind that shoe no the other one.
-
Senior Member
-
-
-
old karma : 0
-
|
I've got a couple of questions that I've been itching to ask.
1) How would you go about creating a header file? I've looked over the ostream header file and I'm completely lost to how you would know how to make it work or what anything in that code means.
2) Since you can only have 1 int main () per .exe how do you know to make a new file, and when you do, how do you make it work in conjuntion with the first file?
|
|
-
09-08-2008, 22:58 |
-
Saucemasterpimp
The True Ninja Member
-
-
-
-
Shadows
-
Senior Member
-
-
-
old karma : 110
-
|
Well, to create a header file, simply go to create new file and choose header (extension .h)
I primarily (and I think it is convention) use header files to define objects/classes/User-defined data types. For example, I could separate the definition of an object (its private variables, function declarations, helper functions) from its implementation (in a .cpp file, the actual code that defines how the object interacts with data) using a header file for the definition.
Header files are great for separating code and making stuff more readable, as well as allowing you to import your code into other files (like including your new object in your driver file (the one containing the int main function)).
This is quite a basic overview on Header files. I really reccommend a beginner's book on C++ to get started with the basics (not to discourage you from posting here anymore! I love to help!)
Philosophical Ninja.
|
|
-
09-09-2008, 9:31 |
-
Sarcross
A1MB0TT3R
-
-
-
Joined on 08-06-2008
-
Under your bed, behind that shoe no the other one.
-
Senior Member
-
-
-
old karma : 0
-
|
Saucemasterpimp:
Well, to create a header file, simply go to create new file and choose header (extension .h)
I primarily (and I think it is convention) use header files to define objects/classes/User-defined data types. For example, I could separate the definition of an object (its private variables, function declarations, helper functions) from its implementation (in a .cpp file, the actual code that defines how the object interacts with data) using a header file for the definition.
Header files are great for separating code and making stuff more readable, as well as allowing you to import your code into other files (like including your new object in your driver file (the one containing the int main function)).
This is quite a basic overview on Header files. I really reccommend a beginner's book on C++ to get started with the basics (not to discourage you from posting here anymore! I love to help!)
I'm discouraged now ![Sad [:(]](/emoticons/emotion-6.gif)
JK =) So how do you know what to put in them to make them work? And how do you do it.
|
|
-
09-09-2008, 20:59 |
-
Saucemasterpimp
The True Ninja Member
-
-
-
-
Shadows
-
Senior Member
-
-
-
old karma : 110
-
|
EDIT: The rest of this is coming soon; I've gotta eat!
Let's create a fictional object, MyObj. I will create the header file, .cpp file, and file with our int main function, and try to explain line by line what each part does.
All it will do is compute the sum of two integers and return that sum.
First, create a new project, and name it what you like. I'm using Visual Studio .NET 2005. To create a project Go to File > New > Project > Visual C++ > General >Empty Project. The compiler will set about creating the solution file and some other things you need not worry about. Next, let's define the object in our header file (.h)
Go to Project > Add New Item > Visual C++ > Code > Header File (.h) and name your header file MyObj
//this is a comment, signified by two backslashes
//myObj.h
#ifndef MYOBJ
#define MYOBJ
//these two statements tell the compiler how MYOBJ is defined.
//if two files in the same project include this header file, then
//without these lines it would throw an error because it would attempt
//to define the object twice.
//literally, "If MYOBJ is not defined, define MYOBJ"
//define MyObj as a class
class MyObj
{
//first let's declare our private variable, sum
//a private variable/function/etc. is something that cannot be accessed by a user or a child object
// i.e. it can only be used by the object containing it
private :
//let's give this object just one private variable, sum, which will hold the sum of two integers
int sum;
//next lets create our public functions
//public functions can be accessed by anyone, a client or child object included.
public :
//we're going to just put our function declarations here, no code
//so just put the first line of your functions in, followed by a semi-colon
MyObj();
//this is the default constructor
//when a new object of type MyObj is created, this is called
//if no parameters are supplied
MyObj( int firstNumber, int secondNumber);
//this is another constructor
//C++ allows us to overload functions by passing different variables to distinguish the two
// from each other, this constructor will take two integers, for reasons explained below
//our next function is something called a mutator, because it will change, or 'mutate'
//the value of a private variable, sum;
//a dead giveaway for a mutator function is usually the fact that it doesn't return a value
//hence the 'void' before the name of the function
void ComputeSum(int firstNumber, int secondNumber);
//pre-condition: parameters are the numbers to be summed up
//post-condition: sum contains value of parameter 1 + parameter 2
//I just listed something called pre-conditions, which help for readability of code,
//and allow one to quickly understand what sort of conditions must be met before calling
//the function, and what conditions must be met after calling the function
//our next function is something called an accessor
//because our variable sum is private, a user can't directly access it to get its value
//this is , because they also can't alter its value
//thus, they'd need to call this next function to 'access' its value
//a dead giveaway for an accessor is a function that returns a value and has
// no parameters
//the function will return a value of type of integer
int getSum();
//pre: none
//post: returns value contained in private variable sum
};
#endif
//these last two lines are crucial!
//because we called the #ifndef and #define at the start, it is sort of like blocking code up
//the #endif signgifies the end of this block
Philosophical Ninja.
|
|
-
09-09-2008, 21:29 |
-
Saucemasterpimp
The True Ninja Member
-
-
-
-
Shadows
-
Senior Member
-
-
-
old karma : 110
-
|
Now, for the .cpp file. Go to Project > Add New Item > C++ File (.cpp) and name it myObj.cpp the details are below
//myObj.cpp
//next let's implement the details of our header file
//to allow the .cpp file to import the definitions, include myObj.h as follows:
#include "myObj.h"
//next let's set about implementing the constructors
//the MyObj:: bit tells the compiler that the function being defined belongs to the
//MyObj class/object. :: is called the scoping operator (it tells the compiler the 'scope' of the function
//so let's create our default constructor, which just initializes our private variable to 0
MyObj::MyObj()
{
sum = 0;
}
//our overloaded object takes two ints, let's sum them up and set our private variable to it
MyObj::MyObj( int firstNumber, int secondNumber)
{
sum = firstNumber + secondNumber;
}
//now for the mutator
void MyObj::ComputeSum(int firstNumber, int secondNumber)
{
sum = firstNumber + secondNumber;
}
//... and the accessor, just returns sum
int MyObj::getSum()
{
return sum;
}
//and that's it. Implementing the objects function is just a matter of copying and pasting the definitions
//from the header file, removing the semicolon, and adding the details of the functions (while ALWAYS remembering
//to use the scoping operator to tell the compiler what object the function belongs to
//if you don't you may get some really strange compiling errors
Philosophical Ninja.
|
|
-
09-09-2008, 21:29 |
-
Saucemasterpimp
The True Ninja Member
-
-
-
-
Shadows
-
Senior Member
-
-
-
old karma : 110
-
|
Finally, let's define our driver file, the one that contains int main(). This should be the easy part. Project > New File > C++ File (.cpp) and name it driver.
//driver.cpp
//don't forget your includes!
//after 'using namespace std', we need to include our object definition so that we can use it
#include <iostream>
using namespace std;
#include "MyObj.h"
int main()
{
//declare our variables
int firstNumber,secondNumber;
//get their values from the user
cout << "Please input the first number: " << endl;
cin >> firstNumber;
cout << "Please input the second number: " << endl;
cin >> secondNumber;
//here I am invoking the second constructor, the one that takes parameters and sets sum
MyObj firstObject(firstNumber,secondNumber);
//output the sum
cout << "The sum of " << firstNumber << " and " << secondNumber << " is " << firstObject.getSum() << endl;
//get new numbers
cout << "Please input the first number again: " << endl;
cin >> firstNumber;
cout << "Please input the second number again: " << endl;
cin >> secondNumber;
//now I'm invoking the default constructor, which just initializes sum to 0
MyObj secondObject;
//lets set our sum now with our new values and then output
secondObject.ComputeSum(firstNumber,secondNumber);
cout << "The sum of " << firstNumber << " and " << secondNumber << " is " << secondObject.getSum() << endl;
return 0;
}
And that's that! Feel free to ask any questions about this. Obviously there are much more efficient ways to write these algorithms, but this way is much clearer and displays the concepts better. Cheers.
Philosophical Ninja.
|
|
-
09-10-2008, 19:30 |
-
09-10-2008, 19:49 |
-
09-10-2008, 23:57 |
-
09-11-2008, 10:03 |
-
Saucemasterpimp
The True Ninja Member
-
-
-
-
Shadows
-
Senior Member
-
-
-
old karma : 110
-
|
Sometimes project files include the same header files (for example, you might accidently type a #include<iostream> in two of your files. The compiler sees these as two different includes and will attempt to link to both of them as different objects (I believe). But because they define the same objects and functions, there will be many conflicts and it would crash if there wasn't something saying "Hey, you already defined this object, don't do it again."
Placing the #ifndef (If not defined) and #define (then define this object) does just that.
Philosophical Ninja.
|
|
-
09-11-2008, 20:00 |
-
Sarcross
A1MB0TT3R
-
-
-
Joined on 08-06-2008
-
Under your bed, behind that shoe no the other one.
-
Senior Member
-
-
-
old karma : 0
-
|
Saucemasterpimp:
Sometimes project files include the same header files (for example, you might accidently type a #include<iostream> in two of your files. The compiler sees these as two different includes and will attempt to link to both of them as different objects (I believe). But because they define the same objects and functions, there will be many conflicts and it would crash if there wasn't something saying "Hey, you already defined this object, don't do it again."
Placing the #ifndef (If not defined) and #define (then define this object) does just that.
OOOOOOOOOOOOOOOOOOOO That cleared that up, explanations =) you should be a teacher.
|
|
-
06-08-2009, 12:45 |
-
DrFisher
-
-
-
Joined on 06-08-2009
-
-
Rookie
-
-
-
-
|
Hello,
I just read this topic, and, i'll be honest with you guy, learning C++ programming at 13 years old is a little bit 'HOT' ! I recomande to start with some amazing softwares for creating games, it will be fun and easy, but also very educative, and, few years later, you'll find yourself ready to get into real hard core C++ stuff.
And, one point, i'm sorry for my bad English
Hope that i helped you
|
|
-
09-01-2009, 20:32 |
-
HobbeMaster776
Hobbe Heads Here!! Get your Hobbe Heads!Extra Extra!
-
-
-
Joined on 08-31-2009
-
-
Junior Member
-
-
-
old karma : 0
-
|
Well I've wanted to get into Coding but I uh...don't understand any of this ;;..[pookyydhsaja ]]~~ stuff...it's all Jibberish to me..so can anyone give recomendations for learning the basics and everything? A ![G o o d [Good]](/emoticons/g_o_o_d.gif) free program that I can screw around with a learn the ropes?

Take the Role-Playing Stereotype quiz.
|
|
-
10-10-2009, 5:09 |
-
BugbiteSwords
Fire Warrior
-
-
-
Joined on 07-11-2008
-
New Zealand
-
Rookie
-
-
-
old karma : 0
-
|
learning the basics is always ![G o o d [Good]](/emoticons/g_o_o_d.gif) , and the example header looked like it was well documented to tell you exactly what it was doing. As for actually being able to read the code, that can take some time to get used to. Some languages flow like English, however many do not. Personally, before I started, I had just been messing around with some HTML, turns out it helps, A LOT. As always C++ is liked, and in some cases necessary for the development world, however other languages like Python, C#, VB and java are all ![G o o d [Good]](/emoticons/g_o_o_d.gif) places to start out. Especially the latter 3.
|
|
-
10-23-2009, 14:10 |
-
XALAN
cake eater
-
-
-
-
Minnesota, USA
-
Member
-
-
-
old karma : 10
-
|
I started in Java, then C#, and then C++.
Either Java or C# is a ![G o o d [Good]](/emoticons/g_o_o_d.gif) place to start. I personally would stay away from VB.Net. Get used to the C++ syntax up front by starting with C# or Java.
http://thnogg.mybrute.com
|
|
-
10-26-2009, 14:25 |
Page 2 of 2 (48 items)
2
|
|