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

C++ Tutorial I

Discussion in 'C++' started by GreenGrass, Aug 2, 2008.

  1. GreenGrass

    GreenGrass New Member

    Joined:
    Jul 5, 2008
    Messages:
    123
    Likes Received:
    8
    Trophy Points:
    0
    Location:
    Norway
    Here i am going to show you some basic C++ Programming... If you have any questions just ask. :)


    Code:
    #include <cstdlib> // This must be used if you are going to be a MS-DOS Application.
    #include <iostream> // This must be included in the header file if you are going to use "Cout" and "Cin"
    using namespace std;
    
    int MittSvar(int tall1, int tall2, char stykke); // Here we make an Function so is going to calculate for us.
    void Manual(); // This is user Manual so is going to tell us how to use the Program.
    
    int main(int argc, char *argv[]) // int main() is the program file.
    { 
    	// starting program. (Starting Block)
    	// we are defind the inputs so you are going to use so the program know how to handle them.
    	int tall1; // int = Integer = This will say that only input so will be accepted is integer.
    	int tall2; // int = Integer = This will say that only input so will be accepted is integer.
    	char stykke; // char = Character = This will say that only input so will be accepted is letters. ect +
    
    	// The Function void does't need any return Value.
    	Manual(); // Here will the User Manaul be Displayed.
    	system("PAUSE >nul"); // Press an Key to Continue (Hidden).
    	system("cls"); // Clear Screen.
    	cout<<"Type: "<<endl; // Prints "Type" on the screen.
    	cin>>tall1>>stykke>>tall2; // Exampel 13 + 13. Remember to press Enter between them.
    	system("cls"); // Clear Screen.
    	int svar = MittSvar(tall1, tall2, stykke); // Svar is the return Value of the Function.
    	cout<<tall1<<stykke<<tall2<<"="<<svar; // Prints: tall1+tall2=svar (Exampel 20+20=40)
    	system("PAUSE >nul"); // Press an Key to Continue (Hidden)
    	return 0; // Exit Program.
    } // End Program (End Block) 
    
    int MittSvar( // Start of the Calc Function.
    int tall1,
    int tall2,
    char stykke)
    { // (Starting Block)
    	int svar; // Answer of the Calc Value is going to be saved in.
    	// "if" command is used to true or false. IF (stykke == '+') means if the "char stykke" is + it.
    	// int Svar will give answer in +. Exampel (13+13=26) 13 = tall1 , + = stykke , 13 = tall2;
    	if (stykke == '+') svar = tall1 + tall2; // Here are we showing the Function how it should act when you put in the Values.
    	if (stykke == '-') svar = tall1 - tall2; // Here are we showing the Function how it should act when you put in the Values.
    	if (stykke == '*') svar = tall1 * tall2; // Here are we showing the Function how it should act when you put in the Values.
    	if (stykke == '/') svar = tall1 / tall2; // Here are we showing the Function how it should act when you put in the Values.
    } // (End Block)
    
    void Manual() //Starting Void Function.
    { // (Starting Block)
    	cout<<"This is a calc."; // Here is what so should be displayed in the Manual Function.
    	cout<<"Writing in what you want to calculate."<<endl; // Here is what so should be displayed in the Manual Function.
    	cout<<"Example 20 + 20 = 40. Allowed characters are "; // Here is what so should be displayed in the Manual Function.
    	cout<<"'+' . '-' . '/' . '*'"; // Here is what so should be displayed in the Manual Function.
    } // (End Block)
    
    
     
  2. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    Good comments! But you should explain in detail how you would put this together as opposed to just showing us. It would help the newbs a lot.
     
  3. seangtz

    seangtz New Member

    Joined:
    Jun 6, 2008
    Messages:
    126
    Likes Received:
    3
    Trophy Points:
    0
    Thx for sharing........ useful information.
     
  4. GreenGrass

    GreenGrass New Member

    Joined:
    Jul 5, 2008
    Messages:
    123
    Likes Received:
    8
    Trophy Points:
    0
    Location:
    Norway
    thanks for the comment hanleyhansen i will try remember that to next time.. :)
     
  5. Echo

    Echo New Member

    Joined:
    Aug 17, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Not bad but, here you define the function as an int rather then a void yet it returns nothing. Why?
    Code:
    int MittSvar()
    Code:
    if (stykke == '+') svar = tall1 + tall2; // Here are we showing the Function how it should act when you put in the Values.
        if (stykke == '-') svar = tall1 - tall2; // Here are we showing the Function how it should act when you put in the Values.
        if (stykke == '*') svar = tall1 * tall2; // Here are we showing the Function how it should act when you put in the Values.
        if (stykke == '/') svar = tall1 / tall2; // Here are we showing the Function how it should act when you put in the Values.
    Badly made since a switch/case is superior here.

    Code:
    system("PAUSE >nul");
    Never use system calls for this, since you use C++ do cin.getline().

    Otherwise great stuff.
     
  6. GreenGrass

    GreenGrass New Member

    Joined:
    Jul 5, 2008
    Messages:
    123
    Likes Received:
    8
    Trophy Points:
    0
    Location:
    Norway
    thanks for your comment Echo :)
    Well i am not so good in C++ i am pretty new to it...
     
  7. Echo

    Echo New Member

    Joined:
    Aug 17, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    No problem.
    Keep it up ;)
     
  8. cyberex06

    cyberex06 New Member

    Joined:
    Sep 9, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    guyz... please help me... send me a software of c++ and examples of this programs to my e-mail address: glrex06@yahoo.com.ph. Thank you in advance...
     
  9. mybestjavascript.com

    mybestjavascript.com New Member

    Joined:
    Sep 15, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    good nice and keep going
     
  10. Unknown

    Unknown New Member

    Joined:
    Sep 15, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    nice example
     
  11. GreenGrass

    GreenGrass New Member

    Joined:
    Jul 5, 2008
    Messages:
    123
    Likes Received:
    8
    Trophy Points:
    0
    Location:
    Norway
    Thanks guys...
     
  12. victor2008

    victor2008 New Member

    Joined:
    Oct 10, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    C++ is an object-oriented version of C that has been widely used to develop enterprise and commercial applications. C++ became popular because it combined traditional C programming with object-oriented programming features. Smalltalk and other OOP languages did not provide the familiar structures of conventional languages such as C and Pascal. Microsoft's Visual C++ is the most widely used C++ language.

    An example:

    Code:
    #include <iostream>
     
    class Bird                 // the "generic" base class
    {
    public:
      virtual void OutputName() {std::cout << "a bird";}
      virtual ~Bird() {}
    };
     
    class Swan : public Bird   // Swan derives from Bird
    { 
    public:
      void OutputName() {std::cout << "a swan";} // overrides virtual function
    };
     
    int main()
    {
      Swan mySwan;             // Creates a swan.
     
      Bird* myBird = &mySwan;  // Declares a pointer to a generic Bird,
                               // and sets it pointing to a newly created Swan.
     
      myBird->OutputName();    // This will output "a swan", not "a bird".
     
      return 0;
    }
     
    Last edited by a moderator: Oct 11, 2008
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    The biggest misconception of the century I guess. C++ is totally different from C semantically but supports the syntax of C
     
  14. damoonica

    damoonica New Member

    Joined:
    Oct 17, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    What do you want me to look there?
     
  16. puma_runner

    puma_runner New Member

    Joined:
    Nov 14, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student / Ammatuer MMA fighter
    Location:
    Rabat, Morocco / Orlando, Florida
    Home Page:
    I don't completely understand the purpose of C++ I really need someone to teach me from scratch :(
     
  17. ruby20

    ruby20 New Member

    Joined:
    Dec 4, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Its very good thing to explain how to work in a language.its very useful for newly learner languages.good going keep it up.
     
  18. skp819

    skp819 New Member

    Joined:
    Dec 8, 2008
    Messages:
    89
    Likes Received:
    3
    Trophy Points:
    0
    it is nice.
    thanks for help.
    it is good for beginners like me. It is helps me.
    thanks once more
     
  19. Stenna

    Stenna New Member

    Joined:
    Feb 3, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for share us with a good example of c++. :shy:
     
  20. new_en_it

    new_en_it New Member

    Joined:
    Nov 17, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0

Share This Page