/*
*copyright (c)2015,烟台大学计算机学院
*All rights reserved
*文件名称:project.cpp
*作者:孙春红
*完成日期:2015年4月5日
*版本号:v1.0
*
*问题描述:阅读程序;
*输入描述:略。
*程序输出:略。
*/
#include <iostream>
using namespace std;
class base
{
private:
int m;
public:
base(){};//定义函数结构体
base(int m)
{
this->m=m;
}//将m的值赋予this 指向本类对象m
int get()
{
return m;
}
void set(int m)
{
this->m=m;
}//将m的值赋予this 指向本类对象m
};//base_end
int main()
{
base *ptr;
ptr=new base[2];//开辟新的空间给数组base
ptr->set(30);
ptr=ptr+1;//开辟数组第二个空间
ptr->set(50);
base a[2]= {1,9};//定义一个新的数组a
cout<<a[0].get()<<","<<a[1].get()<<endl;
cout<<ptr->get()<<",";
ptr=ptr-1;
cout<<ptr->get()<<endl;
delete[] ptr;
return 0;
}
结果:
1,9
50,30
<pre class="cpp" name="code">#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); //指针ep指向成员函数
cout<<ep->GetMin()<<endl; //输出ep所指向的成员函数
CE a=*ep; //定义一个新的对象,将ep指向的数据成员函数的值赋予对象a
cout<<a.GetMin()*3+15<<endl;
return 0;
}