第五周阅读程序
来源:互联网 发布:tpo小站模考软件mac 编辑:程序博客网 时间:2024/05/09 02:29
(1)
#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; cout<<ptr->get()<<","; ptr=ptr-1; cout<<ptr->get()<<endl; delete[] ptr; return 0;}
运行结果:
(2)
#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); cout<<ep->GetMin()<<endl; CE a=*ep; cout<<a.GetMin()*3+15<<endl; return 0;}
运行结果:
(3)
#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; t1.output_time( ); Time *p2=&t1; //指向对象的指针 p2->output_time( ); void (Time::*p3)( ); //指向成员函数的指针 p3=&Time::output_time; (t1.*p3)( ); return 0;}
运行结果:
(4)
#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干神马?void output_student(const Student& student ){ cout << student.get_name() << "\t"; cout << student.get_score() << endl;}int main(){ Student stu( "Wang", 85 ); output_student( stu ); return 0;}
运行结果:
(5)
#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; myClass *ptr_Arr=new myClass[3]; ptr=ptr_Arr; myClass C; cout<<myClass::number<<endl; delete []ptr; return 0;}
运行结果:
(6)
#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;}int main(){ cout <<"Resultl="<<Test::func()<<endl; Test a; cout<<"Result2="<<a.func()<<endl; Test::sfunc (a); return 0;}
运行结果:
学习心得:
第六个程序中,在输出result2时,忘记了静态成员是类中所有对象的公共元素。
0 0
- 第五周程序阅读
- 第五周阅读程序
- 第五周 程序阅读
- 第五周程序阅读(1)
- 第五周程序阅读(2)
- 第五周程序阅读(3)
- 第五周程序阅读(4)
- 第五周程序阅读(5)
- 第五周程序阅读(6)
- 第五周阅读程序一
- 第五周阅读程序二
- 第五周阅读程序三
- 第五周程序阅读四
- 第五周阅读程序五
- 第五周阅读程序6
- 第五周 阅读程序 1
- 第五周 阅读程序 2
- 第五周 阅读程序 5
- Mat imread 与 IplImage cvLoadImage区别
- devise的使用
- Max Points on a Line
- Kinect开发笔记之八C#实现Kinect声音的追踪
- spring 集成hibernate和jdbcTemplate
- 第五周阅读程序
- LeetCode | Binary Search Tree Iterator
- iOS学习之WebView的使用
- 查看oracle数据库的连接数以及用户
- 动态规划之0_1背包
- leetcode-88 Merge Sorted Array
- java基础篇---反射机制
- 开源视频图像处理库
- Java继承-类、超类、子类