下面来看看一个c++中的抽象类的例子,它有一个抽象方法draw()
。 在c++程序中,如果实现类的私有和公共成员,那么它是一个数据抽象的例子。
下面来看看看数据抽象的简单例子。
#include <iostream>
using namespace std;
class sum
{
private: int x, y, z;
public:
void add()
{
cout<<"enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"sum of two number is: "<<z<<endl;
}
};
int main()
{
sum sm;
sm.add();
return 0;
}
执行上面代码得到以下结果 -
enter two numbers:
3
6
sum of two number is: 9