#include <iostream> using namespace std; class CA{ public: CA(){a=2; b=3; c=4; } int a, b, c; }; class CB: public CA{ public: CB(){ a=100; b=200;} int a, b; // 可以覆蓋覆類別 int getX(){ return a*b; } int getY(){ return CA::a*CA::b*c; } // 若要使用父類別 }; int main() { CB b; cout<< "a*b= "<< b.getX()<< endl; cout<< "a*b= "<< b.getY()<< endl; b.a = 1; b.CA::a = 5; b.c = 2; cout<< "a*b= "<< b.getX()<< endl; cout<< "a*b= "<< b.getY()<< endl; return 0; }
沒有留言:
張貼留言