Initial commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
A base pointer to access derived objects
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class BaseClass {
|
||||
int i;
|
||||
public:
|
||||
void setInt(int num) {
|
||||
i = num;
|
||||
}
|
||||
int getInt() {
|
||||
return i;
|
||||
}
|
||||
};
|
||||
class derived: public BaseClass {
|
||||
int j;
|
||||
public:
|
||||
void setJ(int num) {
|
||||
j = num;
|
||||
}
|
||||
int getJ() {
|
||||
return j;
|
||||
}
|
||||
};
|
||||
int main()
|
||||
{
|
||||
BaseClass *baseClassPointer;
|
||||
derived d;
|
||||
baseClassPointer = &d; // BaseClass pointer points to derived object
|
||||
// access derived object using BaseClass pointer
|
||||
baseClassPointer->setInt(10);
|
||||
cout << baseClassPointer->getInt() << " ";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user