1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

My C++ Journey - Learning C++ as a noob.

Discussion in 'C, C++, C#' started by Migladon, Jan 12, 2010.

  1. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    This is the first real programming language im picking up, I've dabbled in Autohotkey a little but I know that is not even comparable to C++.

    Write now im following the online instructional course; "Teach Yourself C++ in 21 Days". I must say its going well im currently on day 4. Im going to be posting code that I create as I go on this jouney so hopefully myself and others can use it for reference and motivation.

    Thanks! :D

    Here is some code from today, I created a program that asks the user for three radiuses, and then it uses a function to calculate the circumference of each and display it. I also added an option to add all circumferences together afterwords.

    All suggestions and knowledge on C++ is very much appreciated in this thread.



    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        signed int radiusOne, radiusTwo, radiusThree;
        float circumferenceOne, circumferenceTwo, circumferenceThree;
        int input;
        
    
    cout << "Please type in 3 different radius." << endl;
    cin >> radiusOne;
    cin >> radiusTwo;
    cin >> radiusThree;
    cout << "Thank you" << endl;
    
    float circumference(unsigned int radius);
    
    circumferenceOne = circumference(radiusOne);
    circumferenceTwo = circumference(radiusTwo);
    circumferenceThree = circumference(radiusThree);
    
    cout << "Circumference for " << radiusOne << " is " << circumferenceOne << endl;
    cout << "Circumference for " << radiusTwo << " is " << circumferenceTwo << endl;
    cout << "Circumference for " << radiusThree << " is " << circumferenceThree << endl;
    
    cout << "Would you like to add these together? 1/2" << endl;
    cin >> input;
    
    if (input == 1)
    {
        cout << (circumferenceOne + circumferenceTwo + circumferenceThree) << endl;
    }
    if (input == 2)
    cout << "Thanks for using this program" << endl;
    if (input != 1 && input != 2)
    cout << "error, invalid key";
    
    return 0;
    }
     
    float circumference(unsigned int radius)
    {
        if (radius <= 100)
            return 3.14 * radius * 2;
        else 
            cout << "Radius must be less than 100 for" << radius;
            return -1;
    }
    
    
    
     
  2. thugkilla

    thugkilla BANNED BANNED

    Joined:
    Aug 27, 2009
    Messages:
    64
    Likes Received:
    6
    Word of advice, the sooner you learn this the better. Use classes, don't get used to this c oriented code. Object Oriented Programming is a must in big programs. Once you get stuck to c oriented code it becomes a hard habbit to kick
     
    • Thanks Thanks x 1
  3. minute80

    minute80 Regular Member

    Joined:
    Dec 3, 2008
    Messages:
    310
    Likes Received:
    81
    I don't know how much you know about programming, but if you have zero programming experience be prepared to sacrafice around a year to grasp c++, its principles, data structures, algorithms, templates and basic libraries. And that is if you are ready to learn each day.

    After that, up to 3 years will pass until you will be ready to develop applications without thinking about language synthax etc.

    Good luck!

     
    • Thanks Thanks x 1
  4. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    Thanks for all the help and kind remarks guys. I really appreciate it.

    Here is some code Ive completed today. I learned about functions today and tried to incorporate them into my code.

    I created two functions, one to calculate perimeter based on 4 side lengths that the user inputs and another one to determine circumference based on the radius a user inputs.


    Code:
    #include <iostream>
    using namespace std;
    
    int Perimeter(int sideone, int sidetwo, int sidethree, int sidefour);
    float Circumference(int radius);
    
    int main()
    {
    int number;
    int sideone;
    int sidetwo;
    int sidethree;
    int sidefour;
    int answerone;
    int radius;
    float answertwo;
    
    
        cout << "Press 1 to find perimeter of sides, 2 for circumference of circle" << endl;
        cin >> number;
    
            if (number == 1)
            {
            cout << "You Chose 1" << endl;
            cout << "Please Input up to Four Side Lengths" << endl;
            cin >> sideone;
            cin >> sidetwo;
            cin >> sidethree;
            cin >> sidefour;
            answerone = Perimeter(sideone, sidetwo, sidethree, sidefour);
            cout << "Perimeter is " << answerone << endl;
            }
    
            if (number == 2)
            {
            cout << "You Chose 2" << endl;
            cout << "Please Input radius" << endl;
            cin >> radius;
            answertwo = Circumference(radius);
            cout << "Circumference is " << answertwo << endl;
            }
    
        
    
    
    return main();
    }
    
    
    int Perimeter(int sideone, int sidetwo, int sidethree, int sidefour)
    {
        return (sideone + sidetwo + sidethree + sidefour);
    }
    
    
    float Circumference(int radius)
    {
        return (radius * 2 * 3.14);
    }
    
    
    MY PROGRESS : I am going chapter by chapter in the book/guide:
    Week 1 at a Glance
    Day 1 Getting Started

    Day 2 The Parts of a C++ Program

    Day 3 Variables and Constants

    Day 4 Expressions and Statements

    Day 5 Functions CURRENTLY HERE

    Day 6 Basic Classes

    Day 7 More Program Flow


    Week 1 in Review



    Week 2 at a Glance
    Day 8 Pointers

    Day 9 References

    Day 10 Advanced Functions

    Day 11 Arrays

    Day 12 Inheritance

    Day 13 Polymorphism

    Day 14 Special Classes and Functions


    Week 2 in Review



    Week 3 at a Glance
    Day 15 Advanced Inheritance

    Day 16 Streams

    Day 17 The Preprocessor

    Day 18 Object-Oriented Analysis and Design

    Day 19 Templates

    Day 20 Exceptions and Error Handling

    Day 21 Whats Next


    Week 3 in Review


     
  5. minute80

    minute80 Regular Member

    Joined:
    Dec 3, 2008
    Messages:
    310
    Likes Received:
    81
    I am not trying to discourage you, but I have read this book and it is not very good one. You would be better off with Bruce Eckel's Thinking in C++. Just my .02.
     
    • Thanks Thanks x 1
  6. divinci

    divinci Junior Member

    Joined:
    Sep 25, 2007
    Messages:
    111
    Likes Received:
    15
    definitely agree, the Day 8 pointers section of this book will also confuse you - its not that good.

    Why dont you just learn C first? It is easier to learn than C++ i would go to say!! Then once you have the C basics, i would skip C++ altogether and go for Java or C#

    Read this book :)
    http://en.wikipedia.org/wiki/File:Kr_c_prog_lang.jpg

    anyhoo i have had enough of giving my shite opinion for the day!... back to xrumaA
     
    • Thanks Thanks x 1
  7. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    Thanks for the advice guys. I have read the first few chapter of this "book" though and I must say I do like it.

    If anything is blurry or unclear to me I look up additional resources on google.

    I am probably going to have to look into pointers beyond what the book discusses.
     
  8. tacopalypse

    tacopalypse Executive VIP Jr. VIP Premium Member

    Joined:
    Nov 30, 2009
    Messages:
    980
    Likes Received:
    2,485
    Home Page:
    autohotkey is like 10x more useful than c++ IMO, especially for IM/automation purposes.

    if i were u i'd just stick with ahk instead and master it
     
  9. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    Erm, comparing C++ to AHK is like comparing a tomato to an atomic bomb.

    Yes, autohotkey is easier, but it lacks the power that C++ has.


    Anyway, Ive been working on some code today. Just learned about Classes (Day 6), and made a small program that declares a Class (Dog) and then creates two objects (Rover and Spot) of that class . I also made methods to Set and Get variables of the class (Dog).

    Any input is appreciated, here is the code-


    Code:
    #include <iostream> // allow cout and cin to work.
    using namespace std; // using c++ standard functions library; No need for std::cout
    
    class Dog
    {
    public:
        int GetAge();
        int GetWeight();
        int GetHeight();
        int GetLength();
            void SetAge(int age);
            void SetWeight(int weight);
            void SetHeight(int height);
            void SetLength(int length);
            void Bark();
    
    private:
        int itsAge;
        int itsWeight;
        int itsHeight;
        int itsLength;
    };
    
    
    
    // FUNCTION DEFINITIONS
    
    // GET FUNTIONS
    
    int Dog::GetAge() // The definition of the GetAge(); Declaration
      {
         return itsAge;
      }
    
    int Dog::GetWeight() // The definition of the GetAge(); Declaration
      {
         return itsWeight;
      }
    
    int Dog::GetHeight() // The definition of the GetAge(); Declaration
      {
         return itsHeight;
      }
    
    int Dog::GetLength() // The definition of the GetAge(); Declaration
      {
         return itsLength;
      }
    
    // SET FUNCTIONS
    
    void Dog::SetAge(int age)
    {
        itsAge = age;
    }
    
    void Dog::SetWeight(int weight)
    {
        itsWeight = weight;
    }
    
    void Dog::SetHeight(int height)
    {
        itsHeight = height;
    }
    
    void Dog::SetLength(int length)
    {
        itsLength = length;
    }
    
    
    // Methods
    
    void Dog::Bark()
    {
        cout << "\nBark!";
    }
    
    
    int main() // MAIN FUNCTION
    {
    
    // DECLARE DOG OBJECT ROVER
        Dog Rover; 
            Rover.SetAge(17);
            Rover.SetWeight(100);
            Rover.SetHeight(30);
            Rover.SetLength(50);
    
            cout << "\nRover is a dog who is " << Rover.GetAge() << " years old.";
            cout << "\nRover weighs " << Rover.GetWeight() << " pounds.";
            cout << "\nRover is " << Rover.GetHeight() << " inches tall.";
            cout << "\nRover is " << Rover.GetLength() << " inches long.";
            Rover.Bark();
    
    
    // DECLARE DOG OBJECT SPOT
        Dog Spot;
            Spot.SetAge(11);
            Spot.SetWeight(3);
            Spot.SetHeight(33);
            Spot.SetLength(90);
    
            cout << "\nSpot is a dog who is " << Spot.GetAge() << " years old.";
            cout << "\nSpot weighs " << Spot.GetWeight() << " pounds.";
            cout << "\nSpot is " << Spot.GetHeight() << " inches tall.";
            cout << "\nSpot is " << Spot.GetLength() << " inches long.";
            Spot.Bark();
    
    return 0;
    }
    
     
    Last edited: Jan 14, 2010
  10. samsherrok

    samsherrok Newbie

    Joined:
    Aug 14, 2009
    Messages:
    9
    Likes Received:
    0
    i have a whole subject of c++ in my engg coourse and tommarow is my exam on that..i havent attended its classes ..and i just know some basics of it...now lets see what happens tomorrow..i will tell you guys if i succeed to give a good test!!...and if anyone could tell me importance of C++ i ll really appreciate it..as it will help me a lot to motivate on subject and concentrate while studying!!
     
  11. radi2k

    radi2k Junior Member

    Joined:
    Nov 29, 2009
    Messages:
    117
    Likes Received:
    34
    Location:
    Germany
    my advise for you: set yourself a target to reach. any project or something like that. it neednt be complicated nor complex. just something useful and get to that target. it needs hard work to accomplish this task but its worth it.

    i learned C++ and other languages that way some years ago. right now i'm programming apps very fast and easily. i dont have to think much about everything. i can even think in code (a bit strange...).

    books are good to get into topic but the real stuff you need is still outa there! you cant conquer the world after the book just remember that :)

    nevertheless: good luck! C++ and programming at all is really helpful
     
    • Thanks Thanks x 1
  12. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    Thanks!, and trust me I know i'm not even going to have scratched the surface of C++ after this book.

    I haven't planned any large goals at the moment but all those code blocks that I posted above I wrote 100%. That's a start I suppose.

    Any ways, Thanks for the advice and hopefully I'll be able to post some more code up here later today. Learning about pointers next :D
     
    Last edited: Jan 14, 2010
  13. radi2k

    radi2k Junior Member

    Joined:
    Nov 29, 2009
    Messages:
    117
    Likes Received:
    34
    Location:
    Germany
    pointers are funny and can confuse a alot :) but if you get deeper into it you will see the power of its concept. without pointers or references (like in java) no system would be as dynamic as you know it today. i was really confused by pointers when i began to learn C++.

    but please dont stick on "cout" thats old fashioned! try to get into "prinft" instead. its more comfortable and has more power like output formatting.

    just try something like:
    Code:
    float x = 0.378347634f;
    printf("%2.0d", x);
    
    or
    Code:
    char y[] = "test phrase";
    printf("%s", y);
    
    you can do some much with it! you can even 'redirect' the output into another string with "sprintf". i hope the book covers it since its really relevant as i think! if not check it out on your own! :)
     
    • Thanks Thanks x 1
    Last edited: Jan 14, 2010
  14. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    What is the %2.0d in the first code block and what is the %s in the 2nd? Also, brackets [] are not needed after char y are they?

    Thanks for the examples!, just trying to understand it clearly. :)

     
  15. radi2k

    radi2k Junior Member

    Joined:
    Nov 29, 2009
    Messages:
    117
    Likes Received:
    34
    Location:
    Germany
    ah yes of course ;)

    %2.0d will take the first given parameter (here the float value) and will output it on the console. you can even write %d only but then there will ne not formatting. everything between the percent charcter and the alphanumeric character influences the format and the output.

    %s is the special character for strings, here the array of char's.

    you can even combine that:
    Code:
    float x = 3.141289;
    char y[] = "test test";
    
    printf("Pi is %d and my test string is '%s'\n", x, y);
    
    the first string represents the format and parameters after it will be taken to replace the special characters by its data. really useful!

    more details can be found here:
    http://www.cplusplus.com/reference/clibrary/cstdio/printf/

    brackets arent needed but you can use them if you want. you can even write
    Code:
    char *bla = "";
    
    or
    Code:
    char* bla = "";
    
    the only difference is that the compiler see's that its an array or an pointer only. but in machine code there is no difference in the behaviour. strange explanation i know but i cant explain better at the moment ;)
     
    • Thanks Thanks x 1
  16. aiden_young

    aiden_young Newbie

    Joined:
    Apr 7, 2008
    Messages:
    9
    Likes Received:
    9
    Before you go down the C++ road, you need to be clear on what you are trying to achieve.

    - do you want to learn how to program in C++ or
    - do you want to program in an Object Oriented manner using C++.

    The two are *not* the same as the examples you have shown so far demonstrate it.

    Unfortunately most books and even so called "professionals" have absolutely no idea what proper object oriented programming is and how to use C++ properly.

    I've found that the best object oriented practices have been implemented in Java. I'm not keen on Java myself, for various reason, however, the fact remains that most good examples are in Java.

    My favourite book is Allen Holub's:

    Holub on Patterns:
    Learning Design Patterns by Looking at Code

    Again, it is using Java, however, if you can put yourself through it and understand it all then your way of thinking about programming will be changed completely.

    If you want to look at actual code that is using "proper" object orientation then you might want to look at the "naked object" framework.

    Functions like get/set_xyz() [ like get/set age ] are an obvious giveaway that whoever "designed" the program, has absolutely no idea about programming.

    Books teaching this sort of programming are only useful for doorstops.

    For one thing, age should be "automatical" in the sense that as soon as an object has been created it'll "know" about it's own age - so it need not be set...

    In any case, learning to program, in any language, will take a considerable time. If you only want to do quick utilities or program in a "non-professional" manner, C++, may not be the best language to start with. Neither is Java, of course, they both need years of commitment.

    And if you thinking of learning object oriented programming, in particular, then be prepared to change the way you "look" at things in general.

    Again, Holub's book should be a great start for you, certainly better than the book you are reading now...

    Also keep in mind that object oriented programming is not specific to any programming language - though some make it easier than others.

    It is a way of thinking about the problem at hand and then structuring your code accordingly.

    A.
     
    • Thanks Thanks x 1
  17. challeng

    challeng Newbie

    Joined:
    Oct 28, 2009
    Messages:
    5
    Likes Received:
    4
    hey man if you ever need help, hit me up, i just took a course on c++ and am currently in higher level programming

    -challeng
     
    • Thanks Thanks x 1
  18. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    being lazy tonight, Friday
     
  19. Migladon

    Migladon Regular Member

    Joined:
    May 16, 2009
    Messages:
    229
    Likes Received:
    135
    New goal: create a poker bot, specifically hold'em