CS253

CS253: Software Development with C++

Spring 2017

Basic Syntax

See this page as a slide show

Basic Syntax

CS253 Basic Syntax

The main function

Here’s a complete C++ program:

    int main() {
        return 0;
    }

That’s it!

main() is a function, not a method. Methods are functions inside of classes. Unlike Java, C++ doesn’t have to have a class, so main() is a function.

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[])

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

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;
cout << sides << ": ";
switch (sides) {
  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";
}
2: 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, 

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.

Modified: 2017-01-28T22:19

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building