//程序作者:管宁
//站点: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();
}
新闻热点
疑难解答