r2 - 12 Aug 2010 - 08:51:07 - RajMathurYou are here: TWiki >  Main Web > ProgrammingIntroduction > 2010-08-09Monday

Topics Covered on Monday, 2010-08-09

  • Objects in C++ may be declared or defined.
  • Every object declared must also be defined somewhere.
  • A declaration describes the object without actually creating it. For variables, it describes the type of the variable. For functions, it describes the return type and the arguments of the function.
  • A definition actually creates the object. For variables, space is allocated. For functions, the function body is compiled.

Data Types in C++

  • String constants: enclosed in "double quotes", need one extra byte of storage to terminate string with byte with value 0.
  • Integer types: include int (typically 32 bits), long int (typically 64 bits), short int (typically 16 bits) and char (typically 8 bits).
    • Integers may be signed or unsigned.
    • Integers can store fixed ranges of values.
  • Floating-point types: include float (typically 32 bits) and double (typically 64 bits).
    • Stored as mantissa and exponent.
    • Range is very high, but number of significant digits stored varies.

Sample program (hello.cpp)

1  // hello.cpp
2  // Print "Hello, world" on the display
3  // Copyright (C) 2010, Raj Mathur
4  #include <iostream>
5  #include <cstdlib>
6  using namespace std;
7  int main(int argc, char *argv[])
8  {
9     cout << "Hello, world" << endl;
10   exit(0);
11 }

  • Line 5 is used for declarations of common functions (exit in our program).
  • Lines 8 and 11 are used to begin and end the function definition.
  • Line 6 is a C++ statement that applies the operator << to the object cout (which is declared in iostream). The argument to the operator is the string "Hello, world".
  • After that, line 16 applies the << to the cout object again with the parameter endl. endl is a manipulator that causes a new line to be printed on the output.
  • Line 10 calls the function exit (declared in cstdlib), which terminates the program and passes the argument 0 to the operating system.

Sample program (add.cpp)

1  // add.cpp
2  // Prompt user for two numbers and print out the sum.
3  // Copyright (C) 2010, Raj Mathur
4  #include <iostream>
5  #include <cstdlib>
6  using namespace std;
7  int main(int argc, char *argv[])
8  {
9    int
10     number_1;
11   int number_2, result;
12   number_1 = number_2 = result = 0;
13   cout << "What is the first number? ";
14   cin >> number_1;
15   cout << "What is the second number? ";
16   cin >> number_2;
17   result = number_1 + number_2;
18   cout << "The sum is " << result << endl;
19   exit(0);
20 }

  • Lines 9 and 10 define an int variable named number_1.
  • Line 12 defines two int variables named number_2 and result.
  • Line 13 sets result to 0, then sets number_2 and then number_1 to the same value.
  • Lines 14 and 16 read the values from the user and store them in number_1 and number_2.
  • Line 17 assigns the result of the addition to the variable result.
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions
Main.2010-08-09Monday moved from Main.2010-08-08Monday on 12 Aug 2010 - 03:20 by RajMathur - put it back

tip TWiki Tip of the Day
File attachments
One can attach files to any topic. The action of attaching a file to a topic is similar to attaching ... Read on Read more

 
Kandalaya
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback