Newest Viewed Downloaded

filese.g.. ifstream infile; infile.open(“scores.dat”); if (!infile) { cerr << “Unable to open scores.dat\n”; exit(0); }

Object oriented programming

What is Class? What is Object? From object-oriented point of view Class is a user-defined data type which contains relevant data and functions Object is a variable declared under some class data type From philosophy concept Class is an abstract concept that describes the attributes of a collection of objects

From C to C++

Namespace 變數、函數、或物件所屬的空間名稱,在不同的namespace中可使用相同的變數名稱。 std: C++所有系統提供的函數所屬的namespace avoid conflict of variable names in the different class libraries

namespace example

compare to C: #include main( ){ printf(“….”); without namespace //This program outputs the message // //C++:one small step for the program, //one giant leap for the programmer // //to the screen #include using namespace std; int main(){ cout <<"C++:one small step for the program,\n" <<"one giant leap for the programmer \n"; return 0; }

namespaces

create namespace examples: namespace mfc{ int inflag; void g(int); … } namespace owl{ int inflag; … }

namespace

use variables in a namespace use scope resolution operator :: e.g. mfc::inflag = 3; owl::inflg = -823; cout << mfc::inflag; the using directive e.g.. using namespace mfc; inflag = 3; // 相當於 mfc::inflag = 3; owl::inflag = -823;

C++ Input/Output

cin >> x >> len; cout << x << len; C++ Input/Output objects cin standard input cout standard output cerr standard error e.g. cin >> x; cin >> len; cout << x; cout << len;

C++ Input/Output

Enter the id and the average:900038 90.8 Id:900038 Average:90.8 example #include using namespace std; int main( ) { int id; float av; cout << “Enter the id and the average:”; cin >> id >> av; cout << “Id:” << id << ‘\n’ << “Average:” << av << ‘\n’; return 0; }

C++ Input Output

1 10 100 1000 Manipulators for the format of I/O set width to n: setw(n) for (i=1; i<=1000; i*=10) cout << setw(6) << i << ‘\n’;

manipulators

4! 6! 8! endl: end of line, write new line e.g. int i=4, j=6, k=8; char c=‘!’; cout << i << c << endl << j << c <<‘\n’ << k << c << endl;

manipulators

i=91 (decimal) i=133 (octal) i=5b (hexadecimal) i=91 (decimal) oct (octal), hex(hexadecimal), dec(decimal) e.g. int i = 91; cout << “i=“ << i << “ (decimal)\n”; cout << “i=“ << oct << i << “ (octal)\n”; cout << “i=“ << hex << i << “ (hexadecimal)\n”; cout << “i=“ << dec << i << “ (decimal)\n”;

manipulators

listed in chap. 14-9 dec, endl, fixed, flush, hex, left, oct, right, scientific, setfill( c ), setprecision(n), setw(n), showpoint, noshowpoint, showpos, noshowpos, skipws, noskipws, ws

manipulators

******1.05 *****10.15 ****200.87 setfill, setprecision e.g. float a = 1.05, b=10.15, c=200.87; cout << setfill(‘’) << setprecision(2); cout << setw(10) << a << ‘\n’; cout << setw(10) << b << ‘\n’; cout << setw(10) << c << ‘\n’;

files (example)

#include using namespace std; const int cutoff =6000; const float rate1 =0.3; const float rate2 =0.6; int main(){ ifstream infile; ofstream outfile; int income,tax; infile.open("income.txt"); outfile.open("tax.txt"); while (infile >>income ){ if (income

files (example cont.)

input file “income.txt” 2214 10500 31010 result: output file “tax.txt” Income = 2214 greenbacks Tax= 664 greenbacks Income = 10500 greenbacks Tax= 6299 greenba cks Income = 31010 greenbacks Income = 18605 greenbacks

files

testing whether files are open file object converts to ‘true’ if open successfully, otherwise converts to ‘false e.g. ifstream infile; ifstream.open(“scores.dat”); … if (infile) { … } // if open sucessfully or if (!infile) { … } // if fail to open the file

files

e.g.. ifstream infile; infile.open(“scores.dat”); if (!infile) { cerr << “Unable to open scores.dat\n”; exit(0); }

C++ features

1 true bool data type values: true (1) or false(0) manipulators: boolalpha, noboolalpha (default) e.g. bool flag; flag = (3 < 5); cout << flag << ‘\n’; cout << boolalpha << flag << ‘\n’;

the type string

Bravo xxxxxxxxxx string initialization e.g. #include string s1; string s2 = “Bravo”; string s3 = s2; string s4(10,’x’); cout << s1 << ‘\n’; cout << s2 << ‘\n’; cout << s4 << ‘\n’;

the type string

a s t r i n g \0 C-style string (end with a null char ‘\0’) char mystring = “a string”; or char mystring[ ] = “a string”; printf(“%s\n”, mystring); char mystring[9] the null character ‘\0’ is added by the C compiler automatically

the type string

Length=7 Ed Wood Ed string length string s = “Ed Wood”; cout << “Length=“ << s.length( ) << ‘\n’; input a string separate by space or new line cout << “Enter a string:”; cin >> s; cout << s;

Showing 1 - 20 of 42 items Details

Name: 
CtoC
Author: 
isu
Company: 
isu
Description: 
filese.g.. ifstream infile; infile.open(“scores.dat”); if (!infile) { cerr << “Unable to open scores.dat\n”; exit(0); }
Tags: 
int | cout | string | the | ptr | elephant | void | print
Created: 
3/6/2003 6:56:05 AM
Slides: 
42
Views: 
0
Downloads: 
0
Rating: 
0


> Comment



Share this presentation
|

Comments

Share this presentation:

|
Sitemap