Practices
  Home arrow Practices arrow Page 13 - Basic Data Types and Calculations
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
PRACTICES

Basic Data Types and Calculations
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Dell PowerEdge Servers

    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 variableunluckyas typeintand 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 areshort,int, andlong. These store signed integers by default, but you can also use the type modifierunsignedpreceding any of these type names to produce a type that occupies the same number of bytes but only stores unsigned integers.

       

    • A variable of typecharcan store a single character and occupies 1 byte. The typecharmay besignedorunsignedby default, depending on your compiler. You can also use variables of the typessigned charandunsigned charto store integers.

       

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

       

    • The floating-point data types arefloat,double, andlong 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 modifierconst. The compiler will check for any attempts within the program source file to modify a variable declared asconst.

       

    • An lvalue is an object or expression that can appear on the left side of an assignment. Non-constvariables 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


     

    Buy this book now. This article was taken from chapter two of the book Beginning ANSI C++: The Complete Language, by Ivor Horton (Apress, 2004; ISBN: 1590592271). Check it out at your favorite bookstore today. Buy this book now.

       

    PRACTICES ARTICLES

    - 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
    - Choosing the Right Team
    - Trees
    - Basic Array Searching in C++

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway