Data Types
Data Types
int main(void)
{
int theInt = 42;
char* theBytes = &theInt;
out.open(“temp.txt”); in.open(“temp.txt”);
out << theInt << endl; in >> theInt;
out.close(); in.close();
.
} .
.
}
C++ Program to Write/Read Integer
Using Binary Files
int main(void) int main(void)
{ {
int theInt = 12345678; int theInt;
ofstream out; ifstream in;
#include <iostream>
using namespace std;
int main(void)
{
// Credit card balance
double balance = 10.10;
double interest = 0.1;
// Formatting
cout.precision(30);
cout << showpoint;
// Output
cout << "Balance is:\t " << balance << endl;
cout << "Interest is:\t " << interest << endl;
cout << "New balance is:\t " << (balance * (1 + interest)) << endl;
}
Interest Calculation Using Decimal Data Type
VC++ .NET
#include "stdafx.h"
#using <mscorlib.dll>
int _tmain()
{
// Credit card balance
Decimal balance = 10.10;
Decimal interest = 0.1;
// Output
Console::WriteLine("Balance is:\t {0}",balance.ToString("F30"));
Console::WriteLine("Interest is:\t {0}",interest.ToString("F30"));
Console::WriteLine("New Balance is:\t {0}",(balance * (1 + interest)).ToString("F30"));
return 0;
}
String Concatenation Problem
String* s = new String();
s = s.Concat(s,new String( “<html>”));
s = s.Concat(s,new String( “<body>”));
s = s.Concat(s,new String( “<ul>”));
s = s.Concat(s,new String( “<li> Item One”));
s = s.Concat(s,new String( “<li> Item Two”));
...
s = s.Concat(s,new String( “</ul>”));
s = s.Concat(s,new String( “</body>”));
s = s.Concat(s,new String( “</html>”));
ASCII Code Page
Latin-1 1252 Code Page
UNICODE LAYOUT Basic Plane
switch (d)
{
case Mon: cout << “More sleep!” << endl; break;
case Tue: cout << “Close to the hump!” << endl; break;
case Wed: cout << “Hump day!” << endl; break;
case Thu: cout << “Over the hump!” << endl; break;
case Fri: cout << “Yipee! “ << endl; break;
case Sat: cout << “Sweet weekend.” << endl; break;
case Sun: cout << “Rats, almost Monday.” << endl; break;
}
switch (d)
{
case 0: cout << “More sleep!” << endl; break;
case 1: cout << “Close to the hump!” << endl; break;
case 2: cout << “Hump day!” << endl; break;
case 3: cout << “Over the hump!” << endl; break;
case 4: cout << “Yipee! “ << endl; break;
case 5: cout << “Sweet weekend.” << endl; break;
case 6: cout << “Rats, almost Monday.” << endl; break;
}
Enumeration Types (Java
Example)
public final class Day {
public static final Day MON = new Day();
public static final Day TUE = new Day();
public static final Day WED = new Day();
public static final Day THU = new Day();
public static final Day FRI = new Day();
public static final Day SAT = new Day();
public static final Day SUN = new Day();
private Day() {
// Empty private constructor ensures the only objects of
// this type are the enumerated elements declared above.
}
}
Program
in
Disk
Virtual Memory
Computing Address of Element In
Multidimensional Array
#!/usr/bin/env perl
#
# Welcome to Perl!
#
# To run this program type:
#
# perl AssociativeArrayExample.pl
#
# If the program works... then you've installed
# perl correctly!
#
01 EMPLOYEE-RECORD. 01 OUTPUT-RECORD.
02 EMPLOYEE-NAME. 02 EMPLOYEE-NAME.
05 FIRST PICTURE IS X(20). 05 FIRST PICTURE IS X(20).
05 MIDDLE PICTURE IS X(20). 05 MIDDLE PICTURE IS X(20).
05 LAST PICTURE IS X(20). 05 LAST PICTURE IS X(20).
02 HOURLY-RATE PICTURE IS 99V99. 02 EMPLOYEE-NUMBER PICTURE IS 9(10).
02 EMPLOYEE-NUMBER PICTURE IS 9(10). 02 GROSS-PAY PICTURE IS 999V999.
02 NET-PAY PICTURE IS 999V999.
o Numerals 01, 02, 05 indicate hierarchical structure of
record
o PICTURE – indicates formatting for output
o X(20) – 20 alphanumeric characters
o 99V99 – 4 decimal digits with “.” in middle
o 9(10) – 10 decimal digits
Ada RECORD EXAMPLES
Employee_Record: Employee_Record_Type;
#include <iostream>
C++ UNION TYPES
using namespace std; //introduces namespace std
int main( void )
{
typedef union _GenericInput
{
OUTPUT:
bool
char
theBool;
theChar;
Enter a character: a
int theInt;
double theDouble; Enter a double: 10.2
} GenericInput;
66 66 66 66 66 66 24 40
GenericInput input0;
GenericInput input1; As boolean x[66]
cout << "Enter a character: ";
cin >> input0.theChar;
As character [f]
cout << "Enter a double: ";
As integer x[66666666]
cin >> input1.theDouble;
As double [10.2]
// You should not be able to assign these two variables
// because they hold different types (char and double) Press any key to continue
// but the “free union” capability in C,C++ allows this
// DANGEROUS!!!
input0 = input1;
cout << "As boolean x[" << input0.theBool << "]" << endl;
cout << "As character [" << input0.theChar << "]" << endl;
cout << "As integer x[" << input0.theInt << "]" << endl;
cout << "As double [" << input0.theDouble << "]" << endl;
return 0;
}
Ada UNION TYPES