of 66 /66
第4第 第第第第第第第第

第 4 章 类和对象深入讨论

  • Author
    wilma

  • View
    134

  • Download
    2

Embed Size (px)

DESCRIPTION

第 4 章 类和对象深入讨论. 4.1 自引用指针 this 例 4.1 this 指针的引例。 #include class A{ public: A(int x1){ x=x1; } void disp(){ cout

Text of 第 4 章 类和对象深入讨论

  • 4

  • 4.1 this 4.1 this #include class A{ public: A(int x1){ x=x1; } void disp(){ cout
  • 4.2 this#includeclass A{public: A(int x1){ x=x1; } void disp(){cout
  • 4.3 this*this#include class Sample{private: int x,y;public:Sample(int i=0,int j=0){ x=i;y=j; }void copy(Sample&xy);void print()cout
  • 4.2 4.2.1 , : [];

  • 4.4 #include class exam{public: void set_x(int n){ x=n; } int get_x(){ return x; } private: int x; }; main() {exam ob[4];int i;for (i=0;i
  • 4.5 #include class exam{public:exam() {x=123;}exam(int n) {x=n;}int get_x(){return x;}private:int x; }; main() {exam ob1[4]={11,22,33,44};exam ob2[4]={55,66};exam ob3[4]={exam(11),exam(22),exam(33),exam(44)};exam ob4[4]={exam(55),exam(66)};ob4[2]=exam(77);

  • ob4[3]=exam(88);for(int i=0;i
  • 4.2.2 1.: * ;, ->

  • 4.7 #include class exe{public: void set_a(int a){ x=a; } void show_a(){ cout
  • 2. 4.7main(): main(){ exe ob[2],*p; ob[0].set_a(10); ob[1].set_a(20); p=ob; p->show_a(); p++; p->show_a(); return 0;}

  • 4.8 #include class example{public:example(int n,int m){x=n;y=m;}int get_x(){return x;}int get_y(){return y;}private:int x,y;}; main() {example op[4]={example(1,2),example(3,4),example(5,6),example(7,8)};int i;example *p;p=&op[3]; //for(i=0;i
  • 4.2.3 1 ::*=&::*->*

  • 4.9#include class A{public: A(int i){z=i;} int z;};void main(){ A ob(5); A *pc1; //pc1 pc1=&ob; //pc1 int A::*pc2; //pc2 pc2=&A::z; //pc2 cout
  • 4.2.3 2 ::*=::*->*

  • 4.10#include class Coord{ public: Coord(int a=0,int b=0){x=a;y=b;} int getx(){return x;} int gety(){return y;} private: int x,y; };void main(){ Coord op(5,6); //op Coord *pc1=&op; //pc1 int (Coord::*pc_getx)(); //pc_getx pc_getx=Coord::getx; //pc_getx cout
  • 4.3 4.3.1

  • 4.11 #includeclass aClass{ public: aClass(int n) { i=n; } void set(int n){ i=n; } int get( ){ return i; }private: int i; };void sqr(aClass ob){ ob.set(ob.get()*ob.get()); cout
  • 4.3.2

  • 4.12 #includeclass aClass {public: aClass(int n) { i=n; } void set(int n){ i=n; } int get(){ return i;} private: int i; };void sqr(aClass *ob){ ob->set(ob->get() * ob->get()); cout
  • 4.3.3

  • 4.13 #includeclass aClass {public: aClass(int n) { i=n; } void set(int n){ i=n; } int get(){ return i;} private: int i; };void sqr(aClass& ob){ ob.set(ob.get() * ob.get()); cout
  • 4.4 4.4.1 ,static, ,, : static ;

  • 4.15#include#includeclass Student { public: Student(char *name1,char *stu_no1,float score1); ~Student(); void show(); // void show_count_sum_ave(); // private: char *name; // char *stu_no; // float score; // static int count; // , static float sum; // , static float ave; // ,};

  • Student::Student(char *name1,char *stu_no1,float score1 ){ name=new char[strlen(name1)+1]; strcpy(name,name1); stu_no=new char[strlen(stu_no1)+1]; strcpy(stu_no,stu_no1); score=score1; ++count; // sum=sum+score; // ave=sum/count; // } Student::~Student(){ delete []name; delete []stu_no; --count; sum=sum-score; }

  • void Student::show(){ cout
  • void main(){ Student stu1("Liming","990201",90); stu1.show(); stu1.show_count_sum_ave(); Student stu2("Zhanghao","990202",85); stu2.show(); stu2.show_count_sum_ave();}

  • 1static2main() ::=; (3):: ::45

  • 4.4.2 : static ; ,: ::() . () ->()

  • 4.16#include#includeclass Student { private: char *name; // char *stu_no; // float score; // static int count; // , static float sum; // , public: Student(char *name1,char *stu_no1,float score1); ~Student(); void show(); // , staticvoid show_count_sum(); // , };

  • Student::Student(char *name1,char *stu_no1,float score1 ){ name=new char[strlen(name1)+1]; strcpy(name,name1); stu_no=new char[strlen(stu_no1)+1]; strcpy(stu_no,stu_no1); score=score1; ++count; // sum=sum+score; // }Student::~Student(){ delete []name; delete []stu_no; --count; sum=sum-score;}

  • void Student::show(){ cout
  • void main(){ Student stu1("Liming","990201",90); stu1.show(); Student::show_count_sum();// Student stu2("Zhanghao","990202",85); stu2.show(); stu2.show_count_sum(); // stu2}

  • 1static2 (3)456this7

  • 4.17#includeclass small_cat{public: small_cat(double w){weight=w;total_weight+=w;total_number++;} static void display(small_cat &w) {cout
  • 4.4.3

  • 4.18 #include class myclass { public: myclass() // ,,i1 { ++i; } static int i; // i }; int myclass::i=0; // i,static int main() { int *count=&myclass::i; // int, myclass ob1,ob2,ob3,ob4; cout
  • 4.5 C++ ,,

  • 4.5.1 ,,, 1:: 2

  • 4.19 #include#includeclass girl {public:girl(char *n,int d){ name=new char[strlen(n)+1]; strcpy(name,n); age=d; }friend void disp(girl &); //~girl(){ delete []name; }private: char *name; int age;};void disp(girl &x) //{ cout
  • 4.5.2 ,,friend ,

  • 4.20 #include#includeclass girl;class boy {public:boy(char *n,int d){ name=new char[strlen(n)+1]; strcpy(name,n); age=d;}void disp(girl &); // disp()boy~boy(){ delete []name; }private: char *name; int age;};

  • class girl{public: girl(char *n,int d) { name=new char[strlen(n)+1]; strcpy(name,n); age=d;} friend void boy::disp(girl &); // boydisp()girl ~girl(){ delete []name; }private: char *name; int age;};void boy::disp(girl &x) // disp(){cout
  • void main() { boy b("Chen Hao",25); girl e("Zhang Wei ",18); b.disp(e); } : Boys name is Chen Hao ,age: 25 Girls name is: Zhang Wei ,age: 18

  • 4.5.3 : class Y { // }; class X { // friend Y; // YX // }; 1 2

  • 4.21 #include#includeclass girl;class girl{public: girl(char *n,int d) { name=new char[strlen(n)+1]; strcpy(name,n); age=d;} ~girl(){ delete []name; }private: char *name; int age; friend boy; // boygirl};

  • 4.6 ,: class A { //... }; class B { A a; //AaB public: // };

  • ,B : class X{ 1 1; 2 2; n n; }; ,X; X::X(0):1(1),, i(i) ,,n(n) { // }

  • 4.22 #includeclass A{public:A(int x1,int y1){x=x1;y=y1;}void show(){cout
  • 4.7 4.7.1 const, : const & : int a=5; const int& b=a; ,b,

  • 4.23 #includeint add(const int& i,const int& j);void main(){ int a=20; int b=30; cout
  • 4.7.2 const, : const [()]; const [()]; , 1C++ 2C++

  • 4.24 #includeclass Sample {public: int m; Sample(int i,int j){ m=i; n=j;} void setvalue(int i){ n=i; } void disply(){ cout
  • 4.7.3 1. const ,,

  • 4.25 #include class Date { public: Date(int y,int m,int d); void showDate(); const int &r; private: const int year ; const int month; const int day; }; Date::Date(int y,int m,int d) :year(y),month(m),day(d),r(year) { } inline void Date::showDate() { cout
  • 2. const,: ()const; const,const

  • 4.26 #include class Date { public: Date(int y,int m,int d); void showDate(); void showDate() const; private: int year; int month; int day; };Date::Date(int y,int m,int d):year(y),month(m),day(d){ }void Date::showDate(){ cout
  • 1 2

  • 1. 2.this 3. 4.pointp1p4. (1)point p2,p3;(2)point p2=p1; (3)point p2(p1);(4)p4=p1; 5.

  • A.this B. C.static D. 6. A. B. C. D.friend

  • 7. A. B. C. D. 8.

  • #include#includeclass Student{public:Student(){ ++x; coutSno; }static int get_x(){ return x;}int get_Sno(){ return Sno;}private:static int x;int Sno;};int Student::x=0;void main(){cout

  • 9.#include#includeclass CTest{public:const int y2;CTest(int i1,int i2):y1(i1),y2(i2){y1=10;x=y1;}int readme()const;private:int x;const int y1;};int CTest::readme()const int i;i=x;x++;return x;}void main(){CTest c(2,8);int i=c.y2;c.y2=i;i=c.y1;}

  • 10.#include#includeclass CTest{public:CTest(){ x=20;}void use_friend();private:int x;friend void friend_f(CTest fri);};void friend_f(CTest fri){fri.x=55;}void CTest::use_friend(){ CTest fri; this->friend_f(fri); ::friend_f(fri);}void main(){CTest fri,fri1; fri.friend_f(fri);friend_f(fri1);}

  • 11.#include#includeclass CTest{public:CTest(){x=20;}void use_this();private:int x;};void CTest::use_this(){CTest y,*pointer;this=&y;*this.x=10;pointer=this;pointer=&y;}void main(){CTest y;this->235;}

  • 12.bookquprice5qu1~5pricequ10qu*price 13.qu*price