//程序作者:管寧 
//站點:www.cndev-lab.com 
//所有稿件均有版權,如要轉載,請務必聞名出處和作者 
//例程1 
#include <iostream> 
using namespace std; 
class Vehicle 
{ 
public: 
Vehicle(float speed,int total) 
{ 
Vehicle::speed=speed; 
Vehicle::total=total; 
} 
void ShowMember() 
{ 
cout<<speed<<""<<total<<endl; 
} 
PRotected: 
float speed; 
int total; 
}; 
class Car:public Vehicle 
{ 
public: 
Car(int aird,float speed,int total):Vehicle(speed,total) 
{ 
Car::aird=aird; 
} 
void ShowMember() 
{ 
cout<<speed<<""<<total<<""<<aird<<endl; 
} 
protected: 
int aird; 
}; 
void main() 
{ 
Vehicle a(120,4); 
a.ShowMember(); 
Car b(180,110,4); 
b.ShowMember(); 
cin.get(); 
} 
//程序作者:管寧 
//站點:www.cndev-lab.com 
//所有稿件均有版權,如要轉載,請務必聞名出處和作者 
//例程2 
#include <iostream> 
using namespace std; 
class Vehicle 
{ 
public: 
Vehicle(float speed,int total) 
{ 
Vehicle::speed=speed; 
Vehicle::total=total; 
} 
void ShowMember() 
{ 
cout<<speed<<""<<total<<endl; 
} 
protected: 
float speed; 
int total; 
}; 
class Car:public Vehicle 
{ 
public: 
Car(int aird,float speed,int total):Vehicle(speed,total) 
{ 
Car::aird=aird; 
} 
void ShowMember() 
{ 
cout<<speed<<""<<total<<""<<aird<<endl; 
} 
protected: 
int aird; 
}; 
void test(Vehicle &temp) 
{ 
temp.ShowMember(); 
} 
void main() 
{ 
Vehicle a(120,4); 
Car b(180,110,4); 
test(a); 
test(b); 
cin.get(); 
} 
//程序作者:管寧 
//站點:www.cndev-lab.com 
//所有稿件均有版權,如要轉載,請務必聞名出處和作者 
//例程3 
#include <iostream> 
using namespace std; 
class Vehicle 
{ 
public: 
Vehicle(float speed,int total) 
{ 
Vehicle::speed = speed; 
Vehicle::total = total; 
} 
virtual void ShowMember()//虛函數 
{ 
cout<<speed<<""<<total<<endl; 
} 
protected: 
float speed; 
int total; 
}; 
class Car:public Vehicle 
{ 
public: 
Car(int aird,float speed,int total):Vehicle(speed,total) 
{ 
Car::aird = aird; 
} 
virtual void ShowMember()//虛函數,在派生類中,由于繼續的關系,這里的virtual也可以不加 
{ 
cout<<speed<<""<<total<<""<<aird<<endl; 
} 
public: 
int aird; 
}; 
void test(Vehicle &temp) 
{ 
temp.ShowMember(); 
} 
int main() 
{ 
Vehicle a(120,4); 
Car b(180,110,4); 
test(a); 
test(b); 
cin.get(); 
} 
//程序作者:管寧 
//站點:www.cndev-lab.com 
//所有稿件均有版權,如要轉載,請務必聞名出處和作者 
#include <iostream> 
using namespace std; 
class Vehicle 
{ 
public: 
Vehicle(float speed,int total) 
{ 
Vehicle::speed=speed; 
Vehicle::total=total; 
} 
virtual void ShowMember() 
{ 
cout<<speed<<""<<total<<endl; 
} 
virtual ~Vehicle() 
{ 
cout<<"載入Vehicle基類析構函數"<<endl; 
cin.get(); 
} 
protected: 
float speed; 
int total; 
}; 
class Car:public Vehicle 
{ 
public: 
Car(int aird,float speed,int total):Vehicle(speed,total) 
{ 
Car::aird=aird; 
} 
virtual void ShowMember() 
{ 
cout<<speed<<""<<total<<""<<aird<<endl; 
} 
virtual ~Car() 
{ 
cout<<"載入Car派生類析構函數"<<endl; 
cin.get(); 
} 
protected: 
int aird; 
}; 
void test(Vehicle &temp) 
{ 
temp.ShowMember(); 
} 
void DelPN(Vehicle *temp) 
{ 
delete temp; 
} 
void main() 
{ 
Car *a=new Car(100,1,1); 
a->ShowMember(); 
DelPN(a); 
cin.get(); 
} 
新聞熱點
疑難解答