Practices
  Home arrow Practices arrow Page 13 - Basic Data Types and Calculations
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PRACTICES

Basic Data Types and Calculations
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2005-09-08


    Table of Contents:
  • Basic Data Types and Calculations
  • Performing Simple Calculations
  • Try It Out: Integer Arithmetic in Action
  • Try It Out: Fixing the Appearance of the Output
  • Try It Out: Using Integer Variables
  • The Assignment Operator
  • Incrementing and Decrementing Integers
  • Numerical Functions for Integers
  • Floating-Point Operations
  • Try It Out: Floating-Point Arithmetic
  • Try It Out: Yet More Output Manipulators
  • Working with Characters
  • Functional Notation for Initial Values
  • Exercises

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Basic Data Types and Calculations - Functional Notation for Initial Values
    ( Page 13 of 14 )

    An alternative notation for specifying the initial value for a variable when you declare it is called functional notation. The term stems from the fact that you put the initial value between parentheses after the variable name, so it looks like a function call, as you’ll discover later on.

    An alternative notation for specifying the initial value for a variable when you declare it is called . The term stems from the fact that you put the initial value between parentheses after the variable name, so it looks like a function call, as you’ll discover later on.

    Let’s look at an example. Instead of writing a declaration as

    int unlucky = 13;

    you have the option to write the statement as

    int unlucky(13);

    Both statements achieve exactly the same result: they declare the variable unlucky as type int and give it an initial value of 13.

    You can initialize other types of variables using functional notation. For instance, you could declare and initialize a variable to store a character with this statement:

    char letter('A');

    However, functional notation for initializing variables is primarily used for the initialization of variables of a data type that you’ve defined. In this case, it really does involve calling a function. The initialization of variables of the fundamental types in C++ normally uses the approach you have taken up to now. You’ll have to wait until Chapter 11 to find out about creating your own types and how those kinds of variables get initialized!

    Summary

    In this chapter, I covered the basics of computation in C++. You learned about most of the fundamental types of data that are provided for in the language. The essentials of what I’ve discussed up to now are as follows:

    • Numeric and character constants are called literals.
    • You can define integer literals as decimal, hexadecimal, or octal values.
    • A floating-point literal must contain a decimal point, or an exponent, or both.
    • Named objects in C++, such as variables, can have names that consist of a sequence of letters and digits, the first of which is a letter, and where an underscore is considered to be a letter. Upper- and lowercase letters are distinguished.

       

    • Names that begin with an underscore followed by a capital letter, and names that contain two successive underscores, are reserved for use within the standard library, so you shouldn’t use them for names of your own variables.

       

    • All literals and variables in C++ are of a given type.

       

    • The basic types that can store integers are short , int , and long . These store signed integers by default, but you can also use the type modifier unsigned preceding any of these type names to produce a type that occupies the same number of bytes but only stores unsigned integers.

       

    • A variable of type char can store a single character and occupies 1 byte. The type char may be signed or unsigned by default, depending on your compiler. You can also use variables of the types signed char and unsigned char to store integers.

       

    • The type wchar_t can store a wide character and occupies either 2 or 4 bytes, depending on your compiler.

       

    • The floating-point data types are float , double , and long double .

       

    • The name and type of a variable appear in a declaration statement ending with a semicolon. A declaration for a variable that results in memory being allocated is also a definition of the variable.

       

    • Variables may be given initial values when they’re declared, and it’s good programming practice to do so.

       

    • You can protect the value of a “variable” of a basic type by using the modifier const . The compiler will check for any attempts within the program source file to modify a variable declared as const .

       

    • An lvalue is an object or expression that can appear on the left side of an assignment. Non- const variables are examples of lvalues.

       

    Although I discussed quite a few basic types in this chapter, don’t be misled into thinking that’s all there are. There are some other basic types, as well as more complex types based on the basic set, as you’ll see, and eventually you’ll be creating original types of your own.



     
     
    >>> More Practices Articles          >>> More By Apress Publishing
     

       

    PRACTICES ARTICLES

    - More Techniques for Finding Things
    - Finding Things
    - Finishing the System`s Outlines
    - The System in So Many Words
    - Basic Data Types and Calculations
    - What`s the Address? Pointers
    - Design with ArgoUML
    - Pragmatic Guidelines: Diagrams That Work
    - Five-Step UML: OOAD for Short Attention Span...
    - Five-Step UML: OOAD for Short Attention Span...
    - Introducing UML: Object-Oriented Analysis an...
    - Class and Object Diagrams
    - Class Relationships
    - Classes
    - Basic Ideas





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek