Learning C++

 

A simple C++ program:

 

// File simple.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. ";

 

     return 0;

}

 

The above program results in the following message:

 

Welcome to CSC126

 

Parts of a program:

C++ programs are made up of functions.  Functions get inputs or arguments and sometimes return a value. 

 

The above program has only one function titled "main".  The main function serves as the point at which C++ programs begin their execution. 

 

Parts of a function:

 

int - type of information the function tells the user

main - name of the function.  Functions are named using standard C++ rules for naming identifiers. 

 

Rules:

1.      The first character must be a letter or underscore.

2.      Rest only letters, digits, or underscores, no special characters such as ?, !, # etc.

3.      No blank spaces

4.      No keywords needed by C++  ex: return, int, void (more complete list in text)

5.      C++ is case sensitive so that dog, Dog, and DOG represent three different identifiers.

6.      identifiers should be meaningful.  Ex. An identifier that represents an average should be called average.

 

Valid identifiers:  dog  dog3  tree TREE   dog_tree   _hello

 

Invalid identifiers:  return  3dogs     boy s

 

The cout Object:

 

Stands for Console Output  - sends output to the standard output device (monitor)

 

displays the data sent to the cout object.  Welcome to CSC126. gets displayed.  Not the quotes.  

 

Data that is enclosed in double quotes is called a character string.  cout displays the characters inside the quotes.   << are the insertion symbol.  What follows the <<  is inserted onto the output stream.

 

The lines of the program that start with // are called comments.  These are informational and are not executed by the C++ compiler.

 

#include <iostream>

 

preprocessor command.  All preprocessor commands start with #.  These commands cause some action to be taken before the compiler translates the program code. The above causes the code in the file iostream.h to be included for compilation along with the user written code.  iostream.h consists of code for input and output. of data.

 

Proper program format:

 

Order as above.  preprocessor, main, C++ statements. All C++ statements end with a ;

 

// File simple2.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. ";

     cout << "Hope you enjoy the course.";

 

     return 0;

}

output:

Welcome to CSC126. Hope you enjoy the course.

 

Notice the space left between sentences and that cout places the output on one line, even though there were two cout statements.  What if you wanted the output on two lines.

 

// File simple3.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. \nHope you enjoy the course.";

 

     return 0;

}

 

Output:

Welcome to CSC126.

Hope you enjoy the course.

 

 

\n is an escape sequence that means move down to the next line.

 

// File simple4.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. \n\nHope you enjoy the course.";

 

     return 0;

}

 

Output:

Welcome to CSC126.

 

Hope you enjoy the course.

 


What is the output of simple5.cpp?

 

// File simple5.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

    

cout << "Welcome to \nCSC126. \n\nHope you enjoy the course.";

 

     return 0;

}

 

Other escape sequences:

 

Escape Sequence

Meaning

\b

Move back one space

\n

Move to next line

\r

Carriage return

\t

Move to next tab

\\

Print backslash character

\'

Print single quote

\"

Double quote

 

 

// File simple6.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. Hope you enjoy the course.\n";

     cout << "Welcome to CSC126. \rHope you enjoy the course.";

 

     return 0;

}


Output:

Welcome to CSC126. Hope you enjoy the course.

Hope you enjoy the course.

 

// File simple7.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. Hope you enjoy the course.\n";

     cout << "Welcome to CSC126. \bHope you enjoy t\bhe course.";

 

     return 0;

}

 

Output:

Welcome to CSC126. Hope you enjoy the course.

Welcome to CSC126.Hope you enjoy he course.

 

 

// File simple8.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. Hope you enjoy the course.\n";

     cout << "Welcome\tto\tCSC126.\tHope\tyou\tenjoy\tthe\tcourse.";

    

     return 0;

}

 

Output:

Welcome to CSC126. Hope you enjoy the course.

Welcome to      CSC126. Hope    you     enjoy   the     course.

 

Note tab default is every 9 spaces.


// File simple9.cpp

// Description:  Displays Welcome to CSC126

// Programmer:  S. Imberman

// Date:1/29/01

 

#include <iostream>

using namespace std;

 

int

main()

{

     cout << "Welcome to CSC126. Hope you enjoy the course.\n";

     cout << "Bob\'s toy.\n";

     cout << "Shakespeare said, \"To be or not to be.\" \n";

     cout << "Print a backslash \\";

    

     return 0;

}

 

 

Output:

Welcome to CSC126. Hope you enjoy the course.

Bob's toy.

Shakespeare said, "To be or not to be."

Print a backslash \

 

 

 /* File simple10.cpp

   Description:  Displays Welcome to CSC126

   Programmer:  S. Imberman

   Date:1/29/01 */

 

#include <iostream>

using namespace std;

 

int

main()

{

     //displays message

cout << "Welcome to CSC126. Hope you enjoy the course.";

 

     return 0; //send 0 value to operating system

}

 

Comments: // for single line comments.  Can be on its own line or next to program statement.

/*   stuff inside */   for multiple line comments.

 

Comments used to explain program code.  Comments can be placed anywhere in a program and have no effect on program execution.