Results 1 to 3 of 3

Thread: High Level C++ (for intro to C++ Students)

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    420

    High Level C++ (for intro to C++ Students)

    Hi All,

    I attended Temple University where I received a degree in Computer and Information Science. While I was there I was a lab instructor for an introduction to C++ class (CIS 81/67 [BA/BS]). I put up http://snowhite.cis.temple.edu/~dspeidel/ for my students. I cannot connect to the snowhite machine unless I come through the Temple University network. Temple University closed my Thunder account (general email access for all students) -but left my Comp Sci accounts (different departments). One link is broken and I cannot fix I apploize for that. The Temple Comp Sci deparment encourage students to use pico (the editor which comes with pine) and not vi. The info below is copie from the site (the email referenced is not accessable). I only copied over the pico and C++ references -not much else there to view but feel free to check the site out (the website has better formating but the content is the same). Many of my students where not computer savvy so some topics on the site do not apply to the general AO community.


    Using pico
    Pico is a basic text editor included with UNIX.
    To start pico from the command line type in pico filename.ext
    If the file being edited is a new file the pico screen opens with a large blank area with a series of commands listed across the bottom. Editing an existing file fills the blank area with the code being modified.
    Pico Commands
    The common pico commands are listed across the bottom of the screen. To activate a command hold down the control key (ctrl) and hit the associated letter (for example ctrl-x is exit)
    ^X is the exit command if you modified the program it will ask if you want to save changes
    ^O writes the document to the disk. This is useful when working with 2 separate windows one with code shown and the other for compling and executing
    ^R Reads a file into the current buffer
    When using pico the file being edited is written into memory. The changes you make do not appear in the file until it is saved
    ^W is the search function. This feature lets you find a word or string
    ^C Tells you which line the cursor is sitting on.
    ^Y Jumps to the previous page in the buffer
    ^V Jumps to the next line in the buffer

    Cursor Movements
    ^F move Forward a character.
    ^B move Backward a character.
    ^P move to the Previous line.
    ^N move to the Next line.
    ^A move to the beginning of the current line.
    ^E move to the End of the current line.
    ^D Delete the character at the cursor position.
    ^K Cut the text on the current line
    ^U Uncut the previously cut text to the cursor location

    After compling a program it is very common to receive an error the compiler gives you the line number where it thinks the problem occured (see compilers for more information).
    Example cc: Error: foo.c, line 114: Missing ;
    To jump to the line with the error enter:
    pico +114 foo.c


    The C++ compiler
    On the command line you enter cxx filename.cpp or cxx filename.c to compile the program. Typically the .c files are programs written in C and .cpp files are programs written in C++. The C++ compiler checks for syntax errors and if it finds none it links the object code into an executable file with the name a.out.

    To run the program enter a.out on the command line and hit enter.

    C++ Syntax C++ is a sequential structured programming language. It has the following general form


    Header file includes such as iostream.h and iomanip.h. These header files contain pre-compiled library functions and redirections such as cout<< (a redirection) and cin.geline (a function)

    Global Function Prototypes and Variables These are user defined functions such as
    //A function can be called by main or by another function
    int GetX(); //This function gets the value of x and returns it to the caller
    int GetY(); //This function gets the value of y and returns it to the caller
    int Sum(int x, int y); //This function takes 2 arguments and returns their sum to the caller
    void Display(int x, int y, int sum); //This function displays the sum of x+y

    //program has no global variables
    //Global variables are variables defined outside of main. These variables can be accessed
    //anywhere in the program. Unlike local variables which can only be accessed in
    //the section of the program where they were created

    The main program
    int main()
    {
    //The main program can be of type int or type void if the program is
    //of type int it must return a value when it exits.
    //If it is void it does not return a value.

    Variable declaration
    int x, y, sum; //x and y are two numbers that will be added together and stores in sum

    The program's activities
    x=GetX(); //Calls the function GetX() and stores the value returned in x
    y=GetY(); //Calls the function GetY() and stores the result returned in y
    sum=Sum(x,y); //Calls the function Sum(int x, int y) and stores the reult in sum
    Display(x,y, sum); //Calls the function display.
    // This function is of type void and does not return a result


    return 0; //Tells the operarting system the program had a ssuccessful execution
    } //Closes the main program

    Function Definition(s)
    int GetX()
    {
    int x; //This is a variable local to GetX() when GetX() is finished
    //This variable no longer exists
    cout<<"Please enter the first value >"; //Prompts user for input
    cin>>x; //Stores the value entered in x

    return x; //Returns the value of x to the caller
    } //Closes the GetX() function

    int GetY()
    {
    int y; //This is a variable local to GetY() when GetY() is finished
    //This variable no longer exists
    cout<<"Please enter the first value > "; //Prompts user for input
    cin>>y; //Stores the value entered in y

    return y; //Returns the value of y to the caller
    } //Closes the GetY() function

    int Sum(int x, int y)
    { return (x + y); //Returns the result of x+ y
    } //Closes Sum(int x, int y)

    void Display(int x, int y, int sum)
    {
    cout << x <<" + " << y <<" = " << sum << endl; //prints the results of x + y
    } //Closes Display function
    //The program Add.cpp is now complete

    Other things to keep in mind . . . Most statements end with semi-colons
    The exceptions being if's, includes and function definitions (however, function prototypes
    do need semi-colons)
    When writing a non-void function it must return a value. The value should be captured by the caller
    (i.e. sum=Sum(x,y); )

    Definitions
    Function Prototype: Appears near the top of the program. It tells the compiler what type
    (void, int, char, float) of function to expect and what the function arguments will be.
    Function Definition: Appears below main. It tells the compiler what to do.
    It looks like the function prototype with variables explicitly defined
    Function prototype = int Sum(int, int);
    Function Definition =int Sum(int x, int y) {
    if statement: A logical expression that is either true or false.

    Logical operands
    x == y Are the 2 variables equal ?
    x < y Is x less then y ?
    x > y Is x greater then y ?
    x < = y Is x less then or equal y ?
    x > = y Is x greater then or equal y?
    Basic structure of an if statement:
    if ( x == y)
    {
    //Execute those statements in the curly brackets
    }
    else {
    //Execute these statements
    }

    Please note that you can have ifs that check 2 or more conditions
    example:
    if ( (x>y) && (sum<100) )
    {
    //Do Something
    }
    If you spend more on coffee than on IT security, you will be hacked. What\'s more, you deserve to be hacked.
    -- former White House cybersecurity adviser Richard Clarke

  2. #2
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350
    Do you know of anyway to alter BIOS via C++? Curious question, I'd like to know how to do this.
    Geek isn't just a four-letter word; it's a six-figure income.

  3. #3
    Junior Member
    Join Date
    Nov 2004
    Posts
    4

    Cool

    Sometimes I get pico to work - and sometimes I can't.

    Do you have any suggestions? ?

    Great tut though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •