The C++ Data Types Fundamental Data Types

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
Data Types.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
C Tokens Identifiers Keywords Constants Operators Special symbols.
The C++ Programming Language Declarations and Constant H.J. Kim.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Data Types Declarations Expressions Data storage C++ Basics.
Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char.
POINTERS.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
Concordia TAV 2002 Comp5421_411 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (1) Tianxiang Shen Summer 2002 Department of Computer.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Object Oriented Programming Lecture 2: BallWorld.
Variables and Types. Primitive Built-in Type Type Meaning Minimum Size bool boolean NA char character 8 bits wchar_t wide character 16 bits char16_t Unicode.
C++ Lesson 1.
Dynamic Storage Allocation
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Learning Objectives Pointers Pointer in function call
C++, OBJECT ORIENTED PROGRAMMING
Pointers and Memory Overview
CISC/CMPE320 - Prof. McLeod
Pointers and Pointer-Based Strings
Student Book An Introduction
C Basics.
8 Pointers.
DATA HANDLING.
Pointers and References
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Lecture 8.
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Pointers.
Pointers Pointers point to memory locations
Pointers and Pointer-Based Strings
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Java Programming Language
Java Basics Data Types in Java.
CS410 – Software Engineering Lecture #5: C++ Basics III
Pointers and References
Pointers, Dynamic Data, and Reference Types
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

The C++ Data Types Fundamental Data Types Consists of the following: 1) Integers of different sizes Type Size(bytes) Type Size char 1 int 2 or 4 short 2 long 4 or 8 The above can be preceded by the key words signed or unsigned to specify signed or unsigned integers, e.g.., signed char (defines integer from -127 to 128) unsigned long (defines integers from 0 to (2^64-1) 2) Floating point numbers type Size in bytes float 4 double 8 long double 16 The integer and floating point types can be preceded by the key word const to specify constants, e.g., const int Max=10; // constants must be initialized when declared.

The C++ Data Types Type Conversion Implicit type conversion exists but it is a potential source of errors, e.g., int I; float a=10.5; I = a; // I equals 10 unsigned long x=1000000000000; I = x; // I is undefined Explicit type conversion using casting is done as follows: int I=2, j=5; float c; c = (float) I / (float) j; // floating point divide unsigned long x, int z = 10000; x = (unsigned long) z*z; Type conversion from a wider data type to narrower data type is unsafe. Derived Types Types can be derived using the following declaration operators: 1. * used to specify pointer type objects 2. & used to define references 3. [ ] used to defined arrays 4. () used to define functions other Derived Types can be defined using structs, classes, and enum

The C++ Data Types Pointers are defined using: type_name * object_name; // object_name is a pointer type // object which can be used to point to objects of the particular type examples: int *pi; // data object pi is a pointer to an integer char **pc; // pc is a pointer to a pointer to a character float *v[10]; // v is an array of 10 pointers to float type objects int (*fp)(char, char*); // fp can be used to point to any function which // takes two arguments of type char, and char*, and returns a data value of type int Initializing pointers int* pi, I=10; // Watch out, I is only an integer, whereas pi is a pointer pi = & I; // pi now points to I. & is called the address of operator The above is the same as: int I=10, *pi=&I; // here pi is declared and initialized Objects can be referenced by their pointers as follows: *pi = I + 100; // same as I = I + 100; // *pi refers to object I which pi points to

The C++ Data Types References are defined using: type &object_name= object_name; // object_name can become an alternative name or an alias for objects of the particular type examples: int I = 1; int &r = I; // I and r refer to the same integer type object The use of references is often found in specifying the types of arguments and return values for functions and overloaded operators. The following examples show the use of references as types of arguments or and return values of functions. Example 1: void f(double &A) { A += 3.14;} // f() updates the value of an object //of type double referenced by A, and does not return any values double d = 0.0; // declare and initialize object d f(d); // call f and pass it the reference of d //Compare the above with code using a pointer instead of a reference

The C++ Data Types . // function g() returns the reference of v[I] Example 2: int v[20]; . // function g() returns the reference of v[I] int& g(int I) { return v[I];} . . main(){ // Assume that function main can not directly access the // elements of v[ ], it can do that thr’ calling g() g(3) = 7; // same as v[3] = 7 // Notice that a function call can be placed on the left side of an // assignment operator // the expression g(3) is called an lvalue since g() returns a // reference

The C++ Data Types Constant Pointers, Pointers to Constants, and References to Constants A constant pointer: int I; int *const cp = &I; // cp is initialized to point to I // cp can not point to any other object A pointer to a constant: const int ci = 10; const int* pc = &ci; // pc is variable pointer to a constant integer *pc = 15 // this gives an error, ci is a constant integer reference to a constant: const float pi = 3.141; const float& unit_circle_area = pi; A reference to a constant can refer to a variable data object double x; const double& xr = x; // xr can be a function argument // xr refers to x as a const data object of type double xr = 10.0; // gives error, where x = 10.0; x *= 100.0; // are OK

The C++ Data Types Arrays are defined as follows type name[const expression] int v[10];// is OK, where int size; int v[size];// gives error, size is not const const int csize=10; double x[csize];// this is OK, elements are x[0] to x[csize-1] arrays of all types can be declared, except arrays of references: int& iar[10]; //gives error initializing an array: int a[ ] = {10,15,25}; double x[csize]={0.1,0.2};// csize = 10 char str[ ]=“C++”;// str[0] = ‘C’, str[1] = ‘+’, str[2] = “+, str[3] = ‘\0’ 2-dimensional arrays: double d[2][3] = { {0.0,0.1,0.2};// row 0 {0.4,0.5,0.6};// row 1}; arrays can be referenced and manipulated using pointers: int ar[10]; int *p=ar;// ar is the same as &ar[0], now p points to the array for(int i=0; i<10; i++){cout << “input an integer value \n”;// using iostream.h cin >> p[i];}// reads the value in ar[i]

The C++ Data Types Dynamic storage Allocation with the new and delete operators Pointers can be used to create arrays or objects of any size at run time with dynamic memory allocation The new operator: new type; allocates memory storage for an object and returns the address of that memory: new int; new double; for an array of objects: new type [number_of_elements]; new int [1000]; the return value from new is used to initialize pointers: double *pd = new double[100]; // allocates memory for 100 double elements int n = 1000; int *p = new int [n]; // notice that n is not a const int size; cout << “input array size”; cin >> size; float *pf = new float[size]; The delete operator: delete pointer_to_object; deallocates the storage of an object delete [ ] pointer_to_array; is used when deleting an array delete[ ] pd; delete [ ] pf; // for int, float, double, and char types [ ] is not needed delete is used to avoid memory leaks which occur when allocating storage for objects defined within a function and exiting the function.

The C++ Data Types typedef, enum, and void* the typedef keyword is used to define new type specifiers, typedef char* String; String a = “hello”; typedef unsigned long Age; Age Stone_Age; the enum keyword is used to define discrete data types, enum Boolean {false,true}; Boolean unordered = false; enum Long_Work_Day { Monday = 1, Tuesday, Thursday}; Long_Work_Day X = 2; // X is initialized to Tuesday void* is used to define generic pointers, int ival, *pi = 0; char *pc = 0; void *pv; pv = pi ; pv = pc // OK, implicit // conversion, pv can be made to point to different variable data types pc = pv // Error, pc must only point to char type objects pc = (char*) pv ; // Ok, since pv is casted to point to char type objects const int *pci = &ival; pv = pci // Error, pci is a pointer to a const data type pv = (void *) pci // Ok, explicit casting away from constness