第4周-项目0-阅读程序
来源:互联网 发布:3d制作软件 编辑:程序博客网 时间:2024/04/28 11:35
问题及代码:
#include <iostream>using namespace std;class base{private: int m;public: base() {}; base(int m){this->m=m;} int get(){return m;} void set(int m){this->m=m;}};//base_endint main(){ base *ptr; ptr=new base[2]; ptr->set(30); ptr=ptr+1; ptr->set(50); base a[2]= {1,9}; cout<<a[0].get()<<","<<a[1].get()<<endl; /// 1 9 cout<<ptr->get()<<",";/// 50 ptr=ptr-1; cout<<ptr->get()<<endl;/// 30 delete[] ptr; return 0;}
运行结果:
问题及代码:
#include<iostream>using namespace std;class CE{private: int a,b; int getmin(){return (a<b? a:b);}public: int c; void SetValue(int x1,int x2, int x3) { a=x1; b=x2; c=x3; } int GetMin();};int CE::GetMin(){ int d=getmin(); return (d<c? d:c);}int main(){ int x=5,y=12,z=8; CE *ep; ep=new CE; ep->SetValue(x+y,y-z,10);/// 17 4 10 cout<<ep->GetMin()<<endl;/// 4 CE a=*ep; cout<<a.GetMin()*3+15<<endl;///27 return 0;}
运行结果:
问题及代码:
#include <iostream>using namespace std;class Time{public: Time(int,int,int); void output_time( ); int hour; int minute; int sec;};Time::Time(int h,int m,int s){ hour=h; minute=m; sec=s;}void Time::output_time( ){ cout<<hour<<":"; cout<<minute<<":" <<sec<<endl;}int main( ){ Time t1(10,13,56); int *p1=&t1.hour; //指向数据成员的指针 cout<<*p1<<endl;/// 10 t1.output_time( /// 10:13:56 Time *p2=&t1; //指向对象的指针 p2->output_time( );/// 10:13:56 void (Time::*p3)( ); //指向成员函数的指针 p3=&Time::output_time;/// 10:13:56 (t1.*p3)( ); return 0;}
运行结果:
问题及代码:
#include <iostream>#include <string>using namespace std;class Student{public: Student() {} Student( const string& nm, int sc = 0 ): name(nm), score(sc) {} ///(1)下面的const干神马?______常数据成员_______ void set_student( const string& nm, int sc = 0 ) { name = nm; score = sc; } ///(2)下面的const分别干神马?___声明一个常成员函数________ const string& get_name() const { return name; } int get_score() const { return score; }private: string name; int score;};///(3)下面的const干神马?____Student类对象的常引用_________void output_student(const Student& student ){ cout << student.get_name() << "\t";/// Wang cout << student.get_score() << endl;/// 85}int main(){ Student stu( "Wang", 85 ); output_student( stu ); return 0;}
运行结果:
问题及代码:
#include<iostream>using namespace std;class myClass{public: myClass(){ number++;} ~myClass(){ number--;} static int number;};int myClass::number=0;int main(){ myClass *ptr; myClass A,B;/// 2 myClass *ptr_Arr=new myClass[3];/// 3 ptr=ptr_Arr; myClass C;/// 1 cout<<myClass::number<<endl; delete []ptr; return 0;}
运行结果:
问题及代码:
#include <iostream>using namespace std;class Test{ private: static int val; int a; public: static int func(); static void sfunc(Test &r);};int Test::val=20;int Test::func(){ val+=val; return val;}void Test::sfunc (Test &r){ r.a=25; cout<<"Result3="<<r.a<<endl;/// 25}int main(){ cout <<"Resultl="<<Test::func()<<endl;/// 40 Test a; cout<<"Result2="<<a.func()<<endl;/// 80 Test::sfunc (a); return 0;}
运行结果:
知识点总结:
指针、const、static
和指针有关的阅读程序,进一步掌握指针工作的原理。
学习心得:
加深了对知识点的理解。
0 0
- 第4周-项目0-阅读程序
- 第4周项目--程序阅读4
- 第4周项目--程序阅读2
- 第6周项目程序阅读4
- 第8周项目 程序阅读(4)
- 第11周项目 程序阅读(4)
- 第12周项目 程序阅读(4)
- 第13周项目 程序阅读(4)
- 第3周-项目0-阅读程序
- 第5周-项目0-阅读程序
- 第12周项目一 程序阅读题(4)
- 第12周项目1-阅读程序(4)
- 第12周 项目1-阅读程序(4)
- 第4周项目--程序阅读3对象是数组
- 第4周项目1-阅读程序(1)
- 第4周项目1-阅读程序(2)
- 第4周项目1-阅读程序(3)
- 第11周项目-阅读程序,写结果4
- java多线程之消费者生产者模式
- JavaScript版飞机大战
- javaScript_DOM事件类型
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项
- 字符串的加密与解密
- 第4周-项目0-阅读程序
- 算法竞赛入门经典第一章习题解答
- 循环不变式与lower_bound证明
- iOS-default.png启动图片(上下多出黑色区域问题)
- BlockingQueue的使用
- 游戏开发的资源网站
- 飞机大战(JavaScript)
- 【c语言】在终端输入多行信息,找出包含"ould"的行,并打印改行
- 第4周-项目1-体验常成员函数-平面坐标点类