问题及代码:
#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; //new开辟一个新的存储空间,存放对象
ep->SetValue(x+y,y-z,10); //为存放的对象赋值 (17,4,10)
cout<<ep->GetMin()<<endl; //输出三个数中的最小值 4
CE a=*ep; //新建一个对象a 赋值为 ep指向的对象的数据
cout<<a.GetMin()*3+15<<endl;//4*3+15=27
return 0;
}
运行结果:
知识点总结:
利用指针操作对象
程序详解如程序注释