为什么我需要通过指向其基础 class 的指针来引用继承 class 的对象?
Why do I need to refer an object of an inherited class through a pointer to its base class?
为什么我需要通过指向其基址 class 的指针来引用继承 class 的对象,而我知道对函数的调用是继承 [=] 独有的21=],会不会产生编译时错误?
为什么要多态?
编辑:
这里有一小段代码作为例子:
enum Suit { Spade, Heart, Club, Diamond };
enum Val { Ace=1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
class Card {
private:
Val val;
Suit suit;
public:
Card(Val val, Suit suit) : val(val), suit(suit) {
cout << "Card constructor called" << endl;
}
int get_val() { return val; }
Suit get_suit() { return suit; }
};
class BlackJackCard : public Card {
public:
int garbage;
BlackJackCard(Val val, Suit suit) : Card(val, suit), garbage(9) {}
int get_val() {
Val tmpVal = (Val)Card::get_val();
if(tmpVal == 1) return 11;
if(tmpVal < 10) return tmpVal;
return 10;
}
int exclusive_to_BJC() {
cout << "I do nothing!! and the garbage value my object holds is " << garbage << endl;
}
};
int main() {
Card *newCard = new BlackJackCard(King,Spade);
cout << newCard->get_val() << endl; // 13
cout << newCard->get_suit() << endl; // 0
/*Why should I use a base class referencing if I can't access all the*/
/*members of the object I have constructed(except for the virtual functions).*/
// cout << newCard->exclusive_to_BJC() << endl;
// cout << newCard->garbage << endl;
BlackJackCard *new_bjCard = new BlackJackCard(King,Spade);
cout << new_bjCard->get_val() << endl; // 10
cout << new_bjCard->get_suit() << endl; // 0
cout << new_bjCard->exclusive_to_BJC() << endl;
}
主要是因为这个原因(遵循link):low coupling.
如果你说指针,我懂C++,所以你也可以看看这个explained example。
你不需要,你可以。事实上,绝大多数时候,最好利用这种可能性,以获得更好的代码。考虑:
class Vehicle
{
};
class Car : public Vehicle
{
};
int f(Vehicle *)
{
// code written here will be able to work on any type of vehicle.
}
OOP 允许您以所有车辆都以相同方式对待的方式编写 f(),从 f() 的角度来看。事实上,Car 可以为 Vehicle 的功能提供专门的功能,甚至不需要 f() 知道。
为什么我需要通过指向其基址 class 的指针来引用继承 class 的对象,而我知道对函数的调用是继承 [=] 独有的21=],会不会产生编译时错误?
为什么要多态?
编辑:
这里有一小段代码作为例子:
enum Suit { Spade, Heart, Club, Diamond };
enum Val { Ace=1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
class Card {
private:
Val val;
Suit suit;
public:
Card(Val val, Suit suit) : val(val), suit(suit) {
cout << "Card constructor called" << endl;
}
int get_val() { return val; }
Suit get_suit() { return suit; }
};
class BlackJackCard : public Card {
public:
int garbage;
BlackJackCard(Val val, Suit suit) : Card(val, suit), garbage(9) {}
int get_val() {
Val tmpVal = (Val)Card::get_val();
if(tmpVal == 1) return 11;
if(tmpVal < 10) return tmpVal;
return 10;
}
int exclusive_to_BJC() {
cout << "I do nothing!! and the garbage value my object holds is " << garbage << endl;
}
};
int main() {
Card *newCard = new BlackJackCard(King,Spade);
cout << newCard->get_val() << endl; // 13
cout << newCard->get_suit() << endl; // 0
/*Why should I use a base class referencing if I can't access all the*/
/*members of the object I have constructed(except for the virtual functions).*/
// cout << newCard->exclusive_to_BJC() << endl;
// cout << newCard->garbage << endl;
BlackJackCard *new_bjCard = new BlackJackCard(King,Spade);
cout << new_bjCard->get_val() << endl; // 10
cout << new_bjCard->get_suit() << endl; // 0
cout << new_bjCard->exclusive_to_BJC() << endl;
}
主要是因为这个原因(遵循link):low coupling.
如果你说指针,我懂C++,所以你也可以看看这个explained example。
你不需要,你可以。事实上,绝大多数时候,最好利用这种可能性,以获得更好的代码。考虑:
class Vehicle
{
};
class Car : public Vehicle
{
};
int f(Vehicle *)
{
// code written here will be able to work on any type of vehicle.
}
OOP 允许您以所有车辆都以相同方式对待的方式编写 f(),从 f() 的角度来看。事实上,Car 可以为 Vehicle 的功能提供专门的功能,甚至不需要 f() 知道。