integral enum floatingfloat double long double char short int long bool trong Visual C++ Gán các giá nhiên cho các tham hàm... Hãy các công sau:a.Tính lãnh tháng nhân viên trong công ty.
Trang 1Khoa Công
1 2 3 4 55
Trang 2Trang cho sinh viên và
[2] A Complete Guide to Programming in C++, Ulla
Kirch-Prinz and Peter Prinz, Jones and Bartlett
Publishers, 2002
[3] The C++ Programming Language, The 3rd
Professional, 2003
hành: 30%
Trang 3Khoa Công
1 2 3 4 55Inline Functions6
Trang 5int a[4], i;
for (i=0; i<4; i++){
printf("\nNhap a%d = ", i);
scanf("%d", &a[i]);
} printf("\nBan vua nhap 4 so:");
for (i=0; i<4; i++){
printf("%d ", a[i]);
} }
Trang 7Primary Memory
the editor and stored
on disk.
Preprocessor program processes the code.
Loader puts program
in memory.
CPU takes each instruction and executes it, possibly storing new data values as the program executes.
Compiler
Compiler creates object code and stores
it on disk.
Linker links the object code with the libraries, creates a.out and stores it on disk
Editor Preprocessor
Linker
CPU
Primary Memory
.
.
Disk Disk Disk Disk
Trang 810 //define local constant PI
11 const float PI= static_cast< float>( ::PI);
Access the global PI with
::PI
Cast the global PI to a
float for the local PI.
This example will showthe difference between
float and double.
12 // display values of local and global PI constants
13 cout << setprecision( 20 )
14 << " Local float value of PI = "<<PI
15 << "\nGlobal double value of PI = "<< ::PI<< endl;
16 return0; // indicates successful termination
17 } // end main
Borland C++ command-line compiler output:
Local float value of PI = 3.141592741012573242
Global double value of PI = 3.141592653589790007
Microsoft Visual C++ compiler output:
Local float value of PI = 3.1415927410125732
Global double value of PI = 3.14159265358979
cin cout cerr
thông báo
Trang 9cin and cout (and #include <iostream>):
Function main appears exactly
once in every C++ program
Function main returns an
integer value.
Left brace { begins
function body.
Corresponding right brace }
ends function body.
Statements end with
a semicolon ;.
Name cout belongs to namespace std.
Stream insertion operator.
Keyword return is one of several means to exit function; value 0 indicates program
5 int integer1; // first number to be input by user
6 int integer2; // second number to be input by user
7 int sum; // variable in which sum will be stored
8 cout << "Enter first integer\n" ; // prompt
9 cin >> integer1; // read an integer
10 cout << "Enter second integer\n" ; // prompt
11 cin >> integer2; // read an integer
12 sum = integer1 + integer2; // assign result to sum
13 cout << "Sum is " << sum << endl; // print sum
12 return 0 // indicate that program ended successfully
15 } // end function main
Declare integer variables.
Use stream extraction operator with standard input stream to obtain user input.
Stream manipulator std::endl
outputs a newline, then “flushes output buffer.”
Concatenating, chaining or cascading stream insertion operations.
Calculations can be performed in output statements:
alternative for lines 12 and 13:
std::cout << "Sum is " << integer1 + integer2 << std::endl;
#include <iostream>
using namespace std;
void main() {int n;
Trang 10integral enum floating
float double long double char short int long bool
trong Visual C++
Gán các giá nhiên cho các tham hàm
Trang 12void swap3( int &x, int &y) { int t = x; x = y; y = t; }
Trang 136 int squareByValue( int ); // function prototype
7 void squareByReference( int & ); // function prototype
8 int main(){
9 int x = 2 , z = 4 ;
10 // demonstrate squareByValue
11 cout << "x = " << x << " before squareByValue\n" ;
12 cout << "Value returned by squareByValue: "
13 << squareByValue( x ) << endl;
14 cout << "x = " << x << " after squareByValue\n" << endl;
Notice the & operator,
18 cout << "z = " << z << " after squareByReference" << endl;
19 return 0 // indicates successful termination
20 } // end main
21 // squareByValue multiplies number by itself, stores the
22 // result in number and returns the new value of number
23 int squareByValue( int number ) {
24 return number *= number; // caller's argument not modified
25 } // end function squareByValue
26 void squareByReference( int &numberRef ) {
27 numberRef *= numberRef; // caller's argument modified
28 } // end function squareByReference
Changes number, but original parameter (x)
11 cout << "x = " << x << endl << "y = " << y << endl;
12 return 0 // indicates successful termination
8 cout << "x = " << x << endl << "y = " << y << endl;
9 return 0 // indicates successful termination
10 }
Uninitialized reference– compiler error
Borland C++ command-line compiler error message:
Error E2304 Fig03_22.cpp 11: Reference variable 'y' must be initialized in function main()
Microsoft Visual C++ compiler error message:
D:\cpphtp4_examples\ch03\Fig03_22.cpp(11) : error C2530: 'y' : references must be initialized
Trang 14int func1 (int);
float func1 (int);
int func1 (float);
void func1 (int = 0, int);
void func2 (int, int = 0);
void func2 (int);
void func2 (float);
void func ( int i, int j = 0 ){
cout << “So nguyen: ” << i << “ ” << j << endl;
}
void func ( float i = 0, float j = 0){
cout << “So thuc:” << i << “ ” << j <<endl;
Trang 15Hãy các công sau:
a.Tính lãnh tháng nhân viên trong công ty.
b.In danh sách nhân viên có
c nhân viên có >= 1200000.
d.In danh sách các nhân viên theo phòng ban, phòng ban trùng nhau thì
theo mã nhân viên.
Trang 16Khoa Công
1 2 3 4 55
Trang 18dàng ra các
Trang 19printf(“%d / %d / %d\n”, d.day, d.mon, d.year);
Trang 20(object) (class)
Trang 21Destructor
Constructor
Other public methods
1984 0610234T 9.2
Trang 22hóa và các liên quan.
Trang 2314/09/2014 29
gói: Nhóm gì có liên quan
tính có
Trang 24Nguyên lý : tránh tái
trình an toàn không thay
trình khác nâng
trúc
Trang 26Cú pháp khai báo
hàm thành Khai báo và
vi truy
– Constructor – Destructor
};
Trang 27private : int width;
int length;
public : void set(int w, int l); int area();
Trang 28public : void set ( int w, int l);
int area() { return width*length; } };
void Rectangle :: set (int w, int l) {
scope operator
class {
//constructor //default constructor
:
Private data:
hrs mins secs
Set Increment Write Time Time
Trang 29(<danh
class Rectangle {
Rectangle r1;
r1.set(5, 8);
} r1 is statically allocated
width length
r1 width = 5 length = 8
width length
r1 width = 5 length = 8
5000
???
r2 6000 5000
width = 8 length = 10
//dot notation
//arrow notation
class Rectangle {
//arrow notation
Trang 30void init(int ox, int oy);
void move(int dx, int dy);
void display();
};
void point::init ( int ox, int oy) {
cout<<"Ham thanh phan init\n";
x = ox; y = oy;
}
void point::move ( int dx, int dy) {
cout<<"Ham thanh phan move\n";
x += dx; y += dy;
}
void point::display () {
cout<<"Ham thanh phan display\n";
cout<<"Toa do: "<<x<<" "<<y<<"\n";
}
voidmain() {
Ham thanh phan move Ham thanh phan display Toa do: 3 6
Trang 32return (x==pt.x && y==pt.y);
}
return (x==pt x && y==pt y);
}
return (x==pt.x && y==pt.y);
x 5
2 x
y
Trang 34point( int ox, int oy) { x = ox; y = oy; }/*Hàm
void move ( int dx, int dy);
point( int ox, int oy = 1){ x = ox; y = oy;}/*Hàm
void move ( int dx, int dy);
MyClass x;
MyClass* p = new MyClass;
khác (overload)
MyClass x(5);
MyClass* p = new MyClass(5);
có các constructor khác , trình
.
Trang 3514/09/2014 38
class point{
int x, y;
public :
point(int ox, int oy = 1){ x = ox; y = oy;}
void move ( int dx, int dy);
Trang 36~vector(); //Hàm voiddisplay();
Truy (“thành viên x có > 10 không?”)
y là bao nhiêu?”)
Trang 37các truy quy tên
sau: “get” , theo là
tên thành viên truy
intgetX();
intgetSize();
intStudent::setGPA (double newGPA){
if ((newGPA >= 0.0) && (newGPA <= 4.0)){
this->gpa = newGPA;
return0; // Return 0 to indicate success}
else{return-1; // Return -1 to indicate failure}
}
Trang 38Trong C, static khai
width length
width length
r1
r3
r2 count
MyClass:
class MyClass{
public : MyClass();
Trang 39MyClass* x = new MyClass;
(thông qua tên
Các thành viên private và protected
truy thông qua các hàm thành viên
Trang 40truy thành viên public khi các
\ }
Dummy(){cout << "Entering a C++ program saying \n";}
~Dummy(){cout << "And then exitting ";}
Hàm Nhap()Hàm Xuat();
Trang 41Khoa Công
là thành
là thành
phát Hàm
L
có là thành khác , khi
Trang 42TamGiac(doublexA, doubleyA, doublexB, doubleyB,
doublexC, doubleyC) : A(xA,yA), B(xB,yB),C(xC,yC){
C(xC,yC), loai(l) {}
?
Trang 44Diem(double xx, doubleyy) : x(xx), y(yy){ }
voidSet(doublexx, double yy) {
public:String(char*s) { p = strdup(s); }String(const String &s) { p = strdup(s.p); }
~String() {cout << "delete "<< (void *)p << "\n";
delete[] p;
}};
double x,y;
public:Diem(doublexx = 0, doubleyy = 0) : x(xx), y(yy){ }voidSet(double xx, doubleyy) {
x = xx, y = yy;
}//
};
Trang 4514/09/2014 16
classString{
char *p;
public:
String(char*s = "") { p = strdup(s); }
String(const String &s) { p = strdup(s.p); }
*ms=“19920014”, intns = 1982) : HoTen(ht), MaSo(ms), NamSinh(ns) { }
~String() { cout << "delete "<< (void *)p << "\n";
delete[] p;
}};
Trang 46class String {
char *p;
public:
String( char *s ) { p = strdup(s); }
String( const String &s ) { p = strdup(s.p); }
int *pi = new int ;
int *pj = new int (15);
Diem *pd = new Diem(20,40);
String *pa = new String("Nguyen Van A");
Trang 47int *pai = new int [10];
Diem *pad = new Diem[5];
String *pas = new String[5];
String (char *s = "Alibaba") { p = strdup(s); }
String (const String &s) { p = strdup(s.p); }
Diem *pad =newDiem[5];
//Ca 5 diem co cung toa do (0,0)String *pas =new String[5];
//Ca 5 chuoi cung duoc khoi dong la “Alibaba”
Trang 49};
classJERRY{
public:voidChange(TOM T){
T.SecterTom++;
}};
Trang 50có hai tách
private và chi mã hóa các hàm thành
int gio, phut, giay;
static bool HopLe(int g, int p, int gy);
public:
ThoiDiem(int g = 0, int p = 0, int gy = 0) {Set(g,p,gy);}
void Set(int g, int p, int gy);
int LayGio() const {return gio; }
int LayPhut() const {return phut; }
int LayGiay() const {return giay; }
ThoiDiem(int g = 0, int p = 0, int gy = 0) {Set(g,p,gy);}
void Set(int g, int p, int gy);
int LayGio() const {return tsgiay/3600;}
int LayPhut() const {return (tsgiay%3600)/60;}
int LayGiay() const {return tsgiay%60;}
Trang 51Program class XX { type1 prop1;
type2 prop2;
type Method1( ) {
}
};
void main() { XX x; // object variable
x.Method( );
}
pick nouns
pick verbs hành vi thành class
double ChuVi() const;double DienTich() const;};
toán, ta nên khai báo là thành classQuocGia{
longDanSo;
double DienTich;
doubleTuoiTrungBinh;
public:doubleTinhTuoiTB() const;//
};
Trang 52double xA, yA;
double xB, yB, xC, yC;
public : //
};
class HinhTron{
double tx, ty , BanKinh;
public : //
riêng Không copy constructor và destructor
Hàm Nhap()Hàm Xuat();
Trang 53các thao tác Set, Cong, Tru, Nhan, ChiaPhanSo A, B, C, D, E;
C.Set(A.Cong(B));
E.Set(D.Cong(C));
E = A + B + C + D ???
Trang 54Các toán cho phép ta cú pháp toán
Trang 552/3 + 5 – 6/5 = ?
aa@ aa.operator@(int) operator@(aa,int)
Trang 56void Set( long t, long m);
long LayTu() const {
PhanSo Cong(PhanSo b) const;
PhanSo operator+ (PhanSo b) const;
PhanSo operator- ()const
{
returnPhanSo(-tu, mau);
}
bool operator==(PhanSo b) const;
bool operator!=(PhanSo b) const;
voidXuat() const;
Trang 5714/09/2014 16
PhanSo PhanSo::Cong(PhanSo b) const {
return PhanSo(tu*b.mau + mau*b.tu, mau*b.mau);
}
PhanSo PhanSo:: operator + (PhanSo b) const {
return PhanSo(tu*b.mau + mau*b.tu, mau*b.mau);
}
bool PhanSo:: operator == (PhanSo b) const {
return tu*b.mau == mau*b.tu;
Trang 58Khi phép toán hàm thành ,
Phép toán 2 ngôi 1 tham
và phép toán 1 ngôi không có tham :
a - b; // a.operator -(b);
PhanSo operator + (PhanSo b) const ; PhanSo operator + (long b) const { return PhanSo(tu + b*mau, mau);}
void Xuat() const ; };
Trang 5914/09/2014 24
class PhanSo{
long tu, mau;
public :
PhanSo ( long t, long m) { Set(t,m); }
PhanSo operator + (PhanSo b) const ;
PhanSo operator + ( long b) const ;{ return PhanSo(tu + b*mau, mau);}
friend PhanSo operator + ( int a , PhanSo b);
};
PhanSo operator + ( int a , PhanSo b)
{ return PhanSo(a*b.mau+b.tu, b.mau); }
nguyên trong các phép toán và quan
nguyên, nguyên và phân
+,-,*,/,<,>,==,!=,<=,>= cho phân và nguyên
thao tác trên phân và nguyên
class PhanSo{
long tu, mau;
public :
PhanSo ( long t, long m) {Set(t,m);}
void Set ( long t, long m);
PhanSo operator + (PhanSo b) const ;
PhanSo operator + ( long b) const ;
friend PhanSo operator + ( int a, PhanSo b);
PhanSo operator - (PhanSo b) const ;
PhanSo operator - ( long b) const ;
friend PhanSo operator - ( int a, PhanSo b);
PhanSo operator * (PhanSo b) const ;
PhanSo operator * ( long b) const ;
friend PhanSo operator * ( int a, PhanSo b);
PhanSo operator / (PhanSo b) const ; PhanSo operator / ( long b) const ;
friend PhanSo operator / ( int a, PhanSo b);
bool operator == (PhanSo b) const ;
bool operator == ( long b) const ;
friend bool operator == ( long a, PhanSo b);
bool operator != (PhanSo b) const ;
bool operator != ( long b) const ;
friend bool operator != ( int a, PhanSo b);
bool operator < (PhanSo b) const ;
bool operator < ( long b) const ;
friend bool operator < ( int a, PhanSo b);
};
Trang 60các khai báo trên, ta có
doubler = 2; // double r = double(2);
doubles = r + 3; // double s = r + double(3);
cout << sqrt(9); // cout << sqrt(double(9));
void Set( long t, long m);
PhanSo operator + (PhanSo b) const ;
friend PhanSo operator + ( int a, PhanSo b);
PhanSo operator - (PhanSo b) const ;
friend PhanSo operator - ( int a, PhanSo b); //…
};
Trang 61PhanSo ( long t, long m) { Set(t,m); }
PhanSo ( long t) { Set(t,1); }
void Set ( long t, long m);
friend PhanSo operator + (PhanSo a, PhanSo b);
friend PhanSo operator - (PhanSo a, PhanSo b);
Trang 62NumStr( char *p) { s = strdup(p); }
operator double () { return atof(s); }
friend ostream & operator << (ostream &o, NumStr &ns);
cout << "s1 = " << s1 << "\n"; // Xuat 's1 = 123.45' ra cout cout << "s2 = " << s2 << "\n"; // Xuat 's2 = 34.12' ra cout cout << "s1 + s2 = " << s1 + s2 << "\n";
// Xuat 's1 + s2 = 157.57' ra cout cout << "s1 + 50 = " << s1 + 50 << "\n";
// Xuat 's1 + 50 = 173.45' ra cout cout << "s1*2=" << s1*2 << "\n"; // Xuat 's1*2=246.9' ra cout cout << "s1/2 = " << s1/2 << "\n";
// Xuat 's1 / 2 = 61.725' ra cout }
Trang 63PhanSo( long t = 0, long m = 1) {Set(t,m);}
void Set( long t, long m);
friend PhanSo operator + (PhanSo a, Pham So b);
operator double () const { return double (tu)/mau;}