Login Register
MoHH2 Legendz

Legends of Medal of honor:Heores 2.. Never forgotten RiP in piece


    You are not connected. Please login or register

    View previous topic View next topic Go down  Message [Page 1 of 1]

    Post: #1Sun Sep 23, 2012 4:04 pm

    ordo ab chao

    ordo ab chao

    Member


    This will be my first post in this subsection this month, and it will be about the implementation of Strings and Classes.

    Strings:

    Strings are a type of variable that can be very useful while programming any kind of application that requires user input, because it is better to use than a char variable and it will help if you are reading info from a text file. Here is some code for a basic implementation of a string variable and its use in an application:
    Code:
    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
        //makes the string, which is named "username"
        string username;
        //prints a question to the console
        cout << "Hello, what is your name? ";
        //receives input to change the string username's value
        cin >> username;
        //prints text, then the string's value, then more text, then ends the line
        cout << "Hello, " << username << "! Have a nice stay in hell." << endl;
        // returns 0 if program ran successfully
        return 0;
    }

    This is a pretty simple program, but the uses for strings are almost endless in coding applications. Next up are classes and how to initialize them.

    Classes:

    Classes in C++ are very useful, especially if you want to eventually make a game or a program that keeps track of employees, people you need to kill, local fruit fuckers, etc.

    Code:
    #include <iostream>
    #include <string>

    using namespace std;

    //makes the class "Person"
    class Person {
    //everything under public is accesible by the program
    public:
          //the start of the variables
          Person(string name, int age) {
                this->name = name;
                this->age = age;
          }
          string getName() {
                return name;
          }
          int getAge() {
                return age;
          }
    //everything under private is accessible only through the get<name>() commands, if initialized in the public section
    private:
          string name;
          int age;
    };

    int main() {
          cout << "Creating a person..." << endl;
          //makes an instance of the class "Person"
          Person johnDoe("John Doe", 25);
          cout << "Person's name: " << johnDoe.getName() << endl;
          cout << "Person's age: " << johnDoe.getAge() << endl;
          http://johnDoe.getName and getAge are able to access the private variables
          return 0;
    }

    that wraps up this post, I hope you got something out of it, have a good day Smile


    Post: #2Sun Sep 23, 2012 4:17 pm

    avatar

    [space]

    Lion Heart


    Useful.


    Post: #3Fri Oct 05, 2012 1:41 pm

    BlackHawk

    BlackHawk

    Member


    I LOVE YOU. <3 Finally I got what classes are...


    Post: #4Fri Oct 05, 2012 3:33 pm

    ordo ab chao

    ordo ab chao

    Member


    glad to help


    Post: #5

    Sponsored content





    View previous topic View next topic Back to top  Message [Page 1 of 1]

    Permissions in this forum:
    You cannot reply to topics in this forum