in
Welcome to Lionhead Community Sign in to Windows Live ID | Help

C++ Learning

Last post 10-26-2009, 14:25 by sim656. 47 replies.
Page 2 of 2 (48 items)   < Previous 1 2
Sort Posts: Previous Next
  •  09-07-2008, 5:22 3054265 in reply to 3053645

    Re: C++ Learning

    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]






    T's just a Flesh Wound!
  •  09-07-2008, 7:22 3054350 in reply to 3053645

    Re: C++ Learning

    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] 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] C++ reference (on paper or otherwise) and you can use your initiative, then you're all set!

    G o o d [Good] luck everyone (hope this was helpful)
  •  09-07-2008, 7:59 3054377 in reply to 3054350

    Re: C++ Learning

    Just as a side note, i have heard lots and lots about system pause. And supposedly it's to be used as little as possible. Never using it at all is a plus.




    T's just a Flesh Wound!
  •  09-07-2008, 11:36 3054534 in reply to 3054265

    • Sarcross is not online. Last active: 11-01-2009, 14:25 Sarcross
      A1MB0TT3R
    • Top 500 Contributor
    • Joined on 08-06-2008
    • Under your bed, behind that shoe no the other one.
    • Senior Member
    • old karma : 0

    Re: C++ Learning

    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]

    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 3056248 in reply to 3054534

    Re: C++ Learning

    In the long run i bet it is more flexible, and better to use MS C++ but it's just a matter of figuring out, thanks Sarcross Big Smile [:D]




    T's just a Flesh Wound!
  •  09-08-2008, 10:42 3056283 in reply to 3054265

    Re: C++ Learning

    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]

    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 3056655 in reply to 3054534

    Re: C++ Learning

    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 3057045 in reply to 3056655

    • Sarcross is not online. Last active: 11-01-2009, 14:25 Sarcross
      A1MB0TT3R
    • Top 500 Contributor
    • Joined on 08-06-2008
    • Under your bed, behind that shoe no the other one.
    • Senior Member
    • old karma : 0

    Re: C++ Learning

    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 3057326 in reply to 3057045

    Re: C++ Learning

    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 3057791 in reply to 3057326

    • Sarcross is not online. Last active: 11-01-2009, 14:25 Sarcross
      A1MB0TT3R
    • Top 500 Contributor
    • Joined on 08-06-2008
    • Under your bed, behind that shoe no the other one.
    • Senior Member
    • old karma : 0

    Re: C++ Learning

    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 [:(]

    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 3058467 in reply to 3057791

    Re: C++ Learning

    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 G o o d [Good], 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 3058507 in reply to 3058467

    Re: C++ Learning

    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 3058509 in reply to 3058507

    Re: C++ Learning

    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 3059784 in reply to 3058509

    • Sarcross is not online. Last active: 11-01-2009, 14:25 Sarcross
      A1MB0TT3R
    • Top 500 Contributor
    • Joined on 08-06-2008
    • Under your bed, behind that shoe no the other one.
    • Senior Member
    • old karma : 0

    Re: C++ Learning

    The 1st .cpp file(middle post) Is for what? its a l ittle unclear to me, but other than that, seems like a simple but complicated thing to do.
  •  09-10-2008, 19:49 3059801 in reply to 3059784

    Re: C++ Learning

    It does all the work of the functions. The Header file only contains our function declarations. For example, it declares the ComputeSum() method, and that's it. The actual computing of the sum goes in the .cpp file. When functions start growing in size, this sort of separation makes sense; one can look at the .h file to quickly read what the program does (from the pre and post-conditions) without having to slog through their implementation.
    Philosophical Ninja.
  •  09-10-2008, 23:57 3059985 in reply to 3059801

    • Sarcross is not online. Last active: 11-01-2009, 14:25 Sarcross
      A1MB0TT3R
    • Top 500 Contributor
    • Joined on 08-06-2008
    • Under your bed, behind that shoe no the other one.
    • Senior Member
    • old karma : 0

    Re: C++ Learning

    What does the #indef mean again, is it telling it to define something? I've used the #define before when it didn't seem to work as an int, I've used if and else statements but havent come across the #indef and #endif before.
  •  09-11-2008, 10:03 3060342 in reply to 3059985

    Re: C++ Learning

    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 3060934 in reply to 3060342

    • Sarcross is not online. Last active: 11-01-2009, 14:25 Sarcross
      A1MB0TT3R
    • Top 500 Contributor
    • Joined on 08-06-2008
    • Under your bed, behind that shoe no the other one.
    • Senior Member
    • old karma : 0

    Re: C++ Learning

    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, G o o d [Good] explanations =) you should be a teacher.

  •  06-08-2009, 12:45 3363215 in reply to 3053645

    Re: C++ Learning

    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 Stick out tongue [:P]
    Hope that i helped you Smily [:)]
  •  09-01-2009, 20:32 3398877 in reply to 3053645

    Re: C++ Learning

    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] free program that I can screw around with a learn the ropes?




    Take the Role-Playing Stereotype quiz.
  •  10-10-2009, 5:09 3411022 in reply to 3398877

    Re: C++ Learning

    learning the basics is always G o o d [Good], 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] places to start out.  Especially the latter 3.

  •  10-23-2009, 14:10 3414071 in reply to 3411022

    • XALAN is not online. Last active: 11-22-2009, 13:58 XALAN
      cake eater
    • Not Ranked
    • Minnesota, USA
    • Member
    • old karma : 10

    Re: C++ Learning

    I started in Java, then C#, and then C++.

    Either Java or C# is a G o o d [Good] 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 3414660 in reply to 3414071

    Re: C++ Learning

    My problem with learning has always been I read the material, don't take it in and give up due to lack of motivation, so for me too learn I've got a mate to teach me

    PM me if you want an invite to lockerz!
Page 2 of 2 (48 items)   < Previous 1 2
View as RSS news feed in XML