See the following program:
#include <iostream>
using namespace std;
class MathSymbol {
public:
virtual void doOperation() = 0; // pure virtual class, so MathSymbol is a abstract class
virtual void print(){
cout<<"print MathSymbol"<<endl;
}
void move(){
cout<<"move MathSymbol"<<endl;
}
};
class B : public MathSymbol {
public:
void doOperation(){
cout<<"Operation in B"<<endl;
}
void print(){
cout<<"print B"<<endl;
}
void move(){
cout<<"move B"<<endl;
}
};
class C : public MathSymbol {
public:
void doOperation(){
cout<<"Operation in C"<<endl;
}
void print(){
cout<<"print C"<<endl;
}
void move(){
cout<<"move C"<<endl;
}
};
int main(){
// MathSymbol a; // this is error. because MathSymbol is abstract.
MathSymbol *pa = NULL;
B b, *pb = &b;
C c, *pc = &c;
MathSymbol* array[] = {pb,pc};
int len = 2;
for(int i = 0; i < len; i++){
array[i] -> doOperation();
}
for(int i = 0; i < len; i++){
array[i] -> print();
}
for(int i = 0; i < len; i++){
array[i] -> move();
}
return 0;
}
Its running result is:
Operation in B
Operation in C
print B
print C
move MathSymbol
move MathSymbol
Subscribe to:
Post Comments (Atom)
Manacher's Longest Palindromic Substring Algorithm
http://manacher-viz.s3-website-us-east-1.amazonaws.com/#/
-
Suppose a bus is running on a loop route. It takes 10 mins for the bus to finish the route. If a guy arrives at a bus stop at uniformly rand...
-
Question: You are playing a card game with me. Suppose I shuffle a deck of 52 cards, then I show you the card one by one to you from top t...
-
Move Objects There are N objects kept in a row. The ith object is at position x_i. You want to partition them into K groups. You want ...
No comments:
Post a Comment