Professional Documents
Culture Documents
Bai Tap On Tap Lap Trinh Huong Doi Tuong
Bai Tap On Tap Lap Trinh Huong Doi Tuong
Bai Tap On Tap Lap Trinh Huong Doi Tuong
Cu 2. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> #include <string.h> class Employee { char ten[30]; char maso[10]; int tuoi; public : Employee (char name[], char code[], int age):tuoi(age) { strcpy(ten, name); strcpy(maso, code); } void Display() { cout<<-Ma so: <<maso<< -Ten: <<ten<< Tuoi:<<tuoi<<endl ; } }; void main() { Employee e( Nguyen Van A, 001, 20); e.Display(); }
A. Li bin dch. B. - Code: 001 - Name: Nguyen Van A - Age: 20 C. -Ma so: 001 -Ten: Nguyen Van A -Tuoi: 20 D. Chng trnh thc thi m khng xut hin g trn mn hnh. Cu 3. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class Test { int n; void Display(){cout<<n;} public: Test(){n=2012;} }; void main() { Test t; t.Display(); }
A. 0
C. 2012
B. Li bin dch
D. Li thc thi
}; class Derive:protected Base { public: Derive(int x){xVal = x;} }; void main() { Derive d(10); d.Print(); }
Cu 5. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include<iostream.h> class BaseA{ protected: int A; public: BaseA(int x=2){ A =x; } virtual ~BaseA(){ cout<<A; } }; class BaseB{ protected: int B; public: BaseB(int x=1){ B = x; }
virtual ~BaseB(){ cout<<B; } }; class Derive:public BaseA,public BaseB { public: virtual ~Derive(){cout<<A+B; } }; void main(){ BaseA *a; a=new Derive; delete a; }
A. 213
B. A+B
C. 321
D. AB
Cu 6. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class A{ public: int na; A(int a=0){na=a;}; ~A(){cout<<na<< ;}; }; void Func(A &a) { A *p=new A(2); a.na=1; delete p; } void main() { A a(4); Func(a); A *p=new A(3); }
A &n=*p;
delete p;
A. 2 3
B. 1 3 3
C. 2 3 3
D. 2 1
Cu 7. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class A { public: A(): a(2012),b("09cdt") int a; char *b; }; void main() { A a; cout << "b = " << a.b << endl; }
};
A. b = 09cdt C. b=
a = 2012. a = 2012.
B. b = 09cdt D. b= 0
a = 0. a= 2012.
Cu 8. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class B { int x, y; public: ~B() { cout << "B"; } B(int x=0, int y=0){this->x=x; this->y=y;cout<<x<< y;} }; class A: public B public: ~A() }; int main() { A a(1,2); } return 0; { int a, b; { cout << "A"; } A(int a=0, int b=0) {A::a=a; A::b=b;cout<<a<<b;}
A. AA0012
B. 0012AB
C. AB1200
D. B1200
Cu 9. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class Point { int xVal, yVal; public: Point(int x = 0, int y = 0) { xVal = x ; yVal = y ; cout<< "So nguyen " ; } Point(double x = 0, double y = 0) { xVal = x ; yVal = y ; cout<< "So thuc " ; } void Print() ; }; void Point :: Print(){ cout<< " ("<<xVal <<","<<yVal<<" )"; } void main() { Point pt(3.0); pt.Print(); }
Cu 10. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> #include <string.h> class C { public: virtual char* toString() { return "C";} };
class B: public C { char* toString() { return "B"; } }; class A: public B { char* toString() { return "A"; } }; void displayObject(C p) { cout << p.toString(); } int main() { displayObject(A()); displayObject(B()); displayObject(C()); return 0; }
A. CBB
B. BCA
C. AAA
D. CCC
Cu 11. Cho bit kt qu bin dch v thc thi chng trnh sau y?
#include<iostream.h> class tong { protected: int a,b; public: tong(int x,int y){a=x;b=y;}; ~tong(){a=0; b=0;}; }; void main() { tong *p=new tong(3,6); cout<<"Tong="<<p->a+p->b; }
Cu 12. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class Set { int n, size; public: Set(int s=0): size(s), n(5) { } Set Func(Set) ; void Print(){cout<<n<<" "<<size;} }; Set Set::Func(Set s1){ Set s=s1; return s;} void main() { Set s(10); Set s1=s.Func(s); s.Print(); }
A. Li bin dch
B. 10 5
C. 5 10
D. 0 10
Cu 13. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include<iostream.h> class BaseA{ protected: int A; public: BaseA(int x=2){ A =x; } ~BaseA(){ cout<<" A="<<A<<" "; } }; class BaseB: virtual public BaseA{ protected: int B; public: BaseB(int x=1){ B = x; } ~BaseB(){ cout<<"B = "<<B<<" "; } }; class BaseC: virtual public BaseA{ protected: int C; public: BaseC(int x=1){ C = x; } ~BaseC(){ cout<<"C = "<<C<<" "; } }; class Derive:public BaseB,public BaseC { public: Derive(){cout<<"A="<<A<<" ";} }; void main(){ Derive t; }
Cu 14. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class A{ public: int x, y; A(int a=1, int b=2){x=a; y=b;cout<<" "<<x<<" "<<y<<" ";} ~A(){cout<<" "<<x<< " "<<y<<" ";} }; void Func(A p, A &q) { q=p; q.x=10; } void main() { A g(3,4), h(5,6); Func(g, h); }
A. 3 4 5 6 3 4 10 4 3 4 B. 5 6 3 4 10 4 5 6 3 4
B. 3 4 5 6 5 6 3 4 D. 3 4 5 6 5 6 3 4 10 6
Cu 15. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class A { public: int na; A(int a=0){na=a;}; ~A(){cout<<na<< ;}; }; void Func(A &a) { A *p=new A(2); a.na=0; delete p; } void main() { A a(4); Func(a); A *p=new A(3); A &n=*p;
delete p; }
A. 2 3
B. 4 3 3
C. 2 3 0
D. 2 0
Cu 16. Cho bit kt qu bin dch v thc thi chng trnh sau y?
#include <iostream.h> #include <string.h> class A { public: A(): nam(2012) { strcpy(s, cdsptw-09cdt) }; int nam; char *lop; }; void main() { A a; cout << "lop = " << a.lop << ; cout << "nam = " << a.nam << endl; }
nam = 0
Cu 17. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class Base { public: Base(){ cout<<"A"; } ~Base(){ cout<<"B"; } }; class Derive:Base { public: Derive(){ cout<<"C"; } ~Derive(){ cout<<"D"; } }; void main() { Derive d; }
A. ABCD
B. ACDB
C. CBAD
D. CDAB
Cu 18. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> #include <stdlib.h> class A{ public: int na; A(int a=1){na=a;}; ~A(){cout<<na<<" " ;}; }; void Func(A &a) { A *p=new A(2); a=*p; delete p; } void main() { A a(4); Func(a); if (a.na==4) exit(0); A *n; n=new A(5); delete n; }
A. 1 5 4
B. 2 5
C. 2 5 4
D. 2 5 2
Cu 19. Cho bit kt qu xut ra mn hnh khi thc thi chng trnh sau y?
#include <iostream.h> class Foo { public: int x; int y; Foo() { x = 10; y = 10; } void p() { int x = 20; cout << "x = " << x << " "; cout << "y = " << y << endl; } }; int main() { Foo foo; foo.p(); return 0; }
A. x = 10 y = 10
B. x = 20 y = 20
C. x = 20 y = 10
D. x = 10 y = 20
Cu 20. Cho bit kt qu bin dch v thc thi chng trnh sau y?
#include<iostream.h> class BaseA{ protected: int A; public: BaseA(int x=2){ A =x; } ~BaseA(){ cout<<" A="<<A<<" "; } }; class BaseB: virtual public BaseA{
protected: int B; public: BaseB(int x=1){ B = x; } ~BaseB(){ cout<<"B = "<<B<<" "; } }; class BaseC: public BaseA{ protected: int C; public: BaseC(int x=1){ C = x; } ~BaseC(){ cout<<"C = "<<C<<" "; } }; class Derive:public BaseB, public BaseC { public: Derive(){cout<<"A="<<A<<" ";} }; void main(){ Derive t; }
Bi tp
Bi 1: Lp trnh hng i tng bng ngn ng C++
a) Xy dng lp NGUOI + Cc thuc tnh: H tn, ngy sinh + Cc phng thc: - Khi to (constructor) - Phng thc NHAP (nhp) nhp cc thng tin: H t n, ngy sinh - Phng thc HIEN (hin) nhp cc thng tin: H t n, ngy sinh b) Xy dng lp SINHVIEN: k tha t lp NGUOI + Cc thuc tnh: M sinh vin, im mn chnh tr, im mn c s, im mn chuyn ngnh, tng im + Cc phng thc: - Khi to (constructor) - Cc phng thc NHAP v HIEN Phng thc NHAP: o Gi phng thc NHAP ca lp NGUOI, o Ngoi ra nhp cc thng tin sau t bn phm: M sinh vin, im mn chnh tr, im mn c s, im mn chuyn ngnh. o Tng im tnh theo cch sau: Tng im = im mn c s + im mn chuyn ngnh Phng thc HIEN: Hin ra cc thng tin v sinh vin theo trnh t sau: M sinh vin, h tn, ngy sinh, im mn chnh tr, im mn c s, im mn chuyn ngnh, tng im. c) T cc lp xy dng trn, chng trnh chnh thc hin: + Nhp vo danh sch gm n sinh vin t bn phm. In ra sanh sch sinh vin va nhp. + iu kin sinh vin tt nghip l im mn chnh tr, im mn c s, im mn chuyn ngnh u phi ln hn hoc bng 5. Tm v in ra mn hnh danh sch sinh vin tt nghip + Sp xp danh sch sinh vin theo tt nghip theo tng im mn c s v im mn chuyn ngnh t cao xung thp. In kt qu va sp xp ra mn hnh. + Nhp m sinh vin vo t bn phm, hy in ra mn hnh thng tin v sinh vin va nhp. + Sp xp danh sch sinh vin tt nghip theo h tn A, B, Cin danh sch va sp xp ra mn hnh. + Tm kim sinh vin sinh nm 1992 v c tng im ln hn 13. In thng tin v sinh vin ny ln mn hnh.