CS253: Software Development with C++

Spring 2018

Basic Syntax

See this page as a slide show

CS253 Basic Syntax

The main function

int main() {
    return 0;
}

Slightly More

Here’s a complete C++ program that creates some output:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, world!\n";
    return 0;
}
Hello, world!

How Examples Work

Many examples in these slides are just a snippet of code:

cout << "How do you do?" << '\n';
How do you do?

The main function

Here’s the other valid definition of main():

int main(int argc, char *argv[]) {
    // Display all arguments, including program name.
    for (int i=0; i<argc; i++)
        cout << "argv[" << i << "]: \"" << argv[i] << "\"\n";
    return 0;
}
argv[0]: "./a.out"

That is all

Don’t even ask about void main().

It does not exist.

⚠ ☢ ☣ ☠

Return value

main() returns an int, a success/failure code to the invoker.

Arguments

    int main(int argc, char *argv[])

Arguments example

% cat ~cs253/Examples/show-args.cc
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    cout << "argc: " << argc << '\n';
    for (int i=0; i<argc; i++)
	cout << "argv[" << i << "]: " << argv[i] << '\n';
    return 0;
}
% g++ -Wall ~cs253/Examples/show-args.cc -o foobar
% ./foobar This class is "CS253: best class ever!"
argc: 5
argv[0]: ./foobar
argv[1]: This
argv[2]: class
argv[3]: is
argv[4]: CS253: best class ever!

argv[0] contains the real program name, as executed.

Compatibility

Basic types

C++ has a number of built-in types:

Sizes

Qualifiers

Qualifiers such as const, constexpr, and static modify existing types.

    const long id = get_id_number();
    constexpr double PI = 3.1415926;
    static const char *program_name;

Derived types

Via pointers & arrays, a vast number of derived types exist:

    int a[10];		// 10 ints
    int *b;		// A pointer to any number of ints
    int *c[10];		// An array of 10 pointers-to-ints
    int (*d)[10];	// A pointer to an array of 10 ints

If you find complex types confusing, build intermediate types with typedef:

    typedef float cash; // cash is now a synonym for float (note the order)
    typedef int *intp;	// New type intp, a pointer to ints
    intp e[10];		// An array of 10 such pointers

Aliases via typedef

With typedef, you can create an alias for an existing type. It does not create a new type.

typedef int counter;    // typedef old new;
counter c = 42;
cout << c << '\n';
42

To use typedef, think of it in two steps:

  1. Declare a variable: int counter;
  2. Slap typedef in front of it: typedef int counter;

Aliases via using

We’re familiar with using for using namespace std;, but it can also be used to make aliases for types:

using counter = int;    // using new = old;
counter c = 43;
cout << c << '\n';
43

Lazy Programmer’s Declaration

Or, you can just declare variables with auto:

auto i=4;           // an int
auto r=3.45;        // a double
cout << i+r << '\n';
7.45

But you must initialize the variable, so that the compiler knows what type to make it:

auto foo;
foo = 'x';
c.cc: In function 'int main()':
c.cc:1:6: error: declaration of 'auto foo' has no initializer

Control Flow

if

int x = 42;
if (x < 100)
    cout << "This is true\n";
This is true
double pi = 3.14159;
if (pi*pi < 10) {
    cout << "Good--mathematics has not changed\n";
}
else
    cout << "Can’t even trust math any more‽\n";
Good--mathematics has not changed

switch

srand(time(nullptr));
int sides = rand() % 5 + 1;
cout << sides << ": ";
switch (sides) {
  case 2: cout << "A line?\n";    break;
  case 3: cout << "Triangle\n";   break;
  case 4: cout << "Square\n";     break;
  case 5: cout << "Pentagon\n";   break;
  default: cout << "Can’t deal with that!\n";
}
1: Can’t deal with that!

while

int i=4;
while (i<10)
    cout << i++ << ' ';
4 5 6 7 8 9 

do … while

int i=4;
do {
    i += 2;
    cout << i << ' ';
} while (i<15);
6 8 10 12 14 16 

for

for (int i=0; i<5; i++)
    cout << i;
01234

range-based for

int a[] = {11,22,33};
for (int v : a)
    cout << v << ", ";
11, 22, 33, 
int b[] = {101, 202, 303, 404};
for (int &n : b)
    n *= 3;
for (const int n : b)
    cout << n << ", ";
303, 606, 909, 1212, 
for (auto q : {12, 34, 56, 78})
    cout << q << ", ";
12, 34, 56, 78, 

Variable declaration

The condition of an if, switch, or while can be a declaration.

const char show[] = "Star Trek";
if (const char *p = strchr(show, 'T'))
    cout << "Found it at location " << p-show << '\n';
Found it at location 5

false, 0, and a null pointer are false, everything else is true.

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-05-13T10:47

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building