下面来看看一个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