当前位置:编程学习 > C/C++ >>

C++璇█缂栫▼闂 - 宸茶В鍐?- 鎼滄悳闂棶

鍔ㄦ€佸唴瀛樻槸浠€涔堟剰鎬濓紵

鎬庝箞鐢?鍦ㄤ粈涔堝湴鏂圭敤鐨勶紵锛?/p>

答案:鍛靛懙锛屼綘鎬庝箞鍙堥棶浜嗕竴閬嶅憿锛?br>鍔ㄦ€佸唴瀛橈紝鏄寚绋嬪簭鍦ㄨ繍琛屾椂锛屽姩鎬佷粠鍐呭瓨鐢宠绌洪棿锛屼竴鑸殑绋嬪簭濡傦細int a= 10;锛岃繖鏍风殑鏉¤鍙ワ紝a鐨勭┖闂存槸浠?b>鏍堢┖闂?/b>涓婄敵璇风殑锛屽綋a鎵€鍦ㄧ殑鍑芥暟杩愯瀹屽悗锛宎鎵€浣跨敤鐨勫唴瀛樼┖闂村氨浼氳嚜鍔ㄩ噴鏀撅紝杩欏彨鍋氶潤鎬佷娇鐢ㄥ唴瀛樸€?br>鑰屾墍璋撶殑鍔ㄦ€佸唴瀛橈紝鏄€氳繃绋嬪簭鍛樿嚜宸辩鐞嗗唴瀛樻潵瀹炵幇鐨勶紝褰撲綘闇€瑕佹椂锛屽氨浠?b>鍫嗙┖闂翠笂鐢宠绌洪棿锛屽湪浣跨敤瀹屾椂锛屽啀杩樼粰鍐呭瓨锛堟敞鎰忔鏃舵墍鍦ㄥ嚱鏁拌繍琛屽畬鎴愬悗锛屼笉浼氳嚜鍔ㄩ噴鏀剧┖闂达級锛屾瘮濡俰nt *a = new int(10);褰撲綘浣跨敤瀹宎鎸囬拡鍚庯紝蹇呴』鎵嬪伐閫氳繃delete a;鏉ラ噴鏀炬墍鐢ㄧ殑鍐呭瓨銆傝繖灏辨槸鍔ㄦ€佸唴瀛樸€?br>涓€鑸姩鎬佸唴瀛樺湪C++涓殑澶氱敤鍦ㄧ户鎵垮拰澶氭€佷笂锛岀粰浣犱竴涓皬渚嬪瓙锛岃寰椾綘灏遍兘鏄庣櫧浜嗭細
class Person{
public:
virtual void print(){

cout << "Person" << endl;

}
};
class Student : public Person{
public:
void print(){
cout << "Student" << endl;
}
};
class Teacher: public Person{
public:
void print(){
cout << "Teacher" << endl;
}
};
鍦ㄤ娇鐢ㄦ椂锛?br>Person *one = new Person();
Person *two = new Student();
Person *three = new Teacher();
one->print();//鏄剧ず鈥淧erson"
two->print();//鏄剧ず"Student"
three->print();//鏄剧ず"Teacher"
delete one;
delete two;
delete three;

上一个:帮忙解决c++问题
下一个:关于C++编程

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,