下面來(lái)看看一個(gè)C++中的抽象類(lèi)的例子,它有一個(gè)抽象方法draw()。 在C++程序中,如果實(shí)現(xiàn)類(lèi)的私有和公共成員,那么它是一個(gè)數(shù)據(jù)抽象的例子。
下面來(lái)看看看數(shù)據(jù)抽象的簡(jiǎn)單例子。
#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;
}
執(zhí)行上面代碼得到以下結(jié)果 -
Enter two numbers:
3
6
Sum of two number is: 9