菜鸟的虚函数有关问题,马上给分!
初学者的虚函数问题,马上给分!!
先建一个抽象类Vehicle,再从其派生出car,truck二个类,在最后的文件中定义一个打印函数,输出其内容,但出错了,请高手帮忙看看,错在哪里?在线等,马上给分!!
//15.5.h
#include <iostream.h>
class Vehicle
{
public:
Vehicle(char *);
virtual ~Vehicle();
virtual void show() const=0;
protected:
char *name;
};
///////////////////////////////////////
//15.5.cpp
#include <iostream.h>
#include <string.h>
#include "15.5.h "
Vehicle::Vehicle(char *na)
{
name=new char[strlen(na)+1];
strcpy(name,na);
}
Vehicle::~Vehicle()
{
delete [] name;
}
///////////////////////////////////////
//15.5_1.h
#include <iostream.h>
#include "15.5.h "
class car :public Vehicle
{
public:
car(char *,int);
~car();
void show()const;
private:
int pl;
};
/////////////////////////////////////////
//15.5_1.cpp
#include <iostream.h>
#include "15.5_1.h "
car::car(char *vname,int a):Vehicle(vname)
{
pl=a;
}
car::~car()
{
cout < < "the object of car is delete " < <endl;
}
void car::show()
{
cout < < "the car 's name is: " < <name < <endl
< < "pl: " < <pl < <endl;
}
/////////////////////////////////////////////
//15.5_2.h
#include <iostream.h>
#include "15.5.h "
class truck :public Vehicle
{
public:
truck(char *,char *);
~truck();
void show()const;
private:
char *color;
};
///////////////////////////////
//15.5_2.cpp
#include <iostream.h>
#include <string.h>
#include "15.5_2.h "
truck::truck(char *tname,char *tcolor):Vehicle(tname)
{
color=new char[strlen(tcolor)+1];
strcpy(color,tcolor);
}
truck::~truck()
{
delete [] color;
}
void truck::show()
{
cout < < "the truck 's name is: " < <name < <endl
< < "color: " < <color < <endl;
}
/////////////////////////////////
//15.5_3.cpp
#include <iostream.h>
//#include "15.5.h "
#include "15.5_1.h "
#include "15.5_2.h "
void schoolshow(Vehicle &);
int main()
{
car vcar( "Benz ",500);
truck vtruck( "Ferrari ", "yellow ");
schoolshow(vcar);
schoolshow(vtruck);
return 0;
}
void schoolshow(Vehicle school)
{
school.show();
}
------解决方案--------------------
void schoolshow(Vehicle& school)
{
school.show();
}
------解决方案--------------------
#include "15.5_1.h "
#include "15.5_2.h "
这两个头文件都包含了 “15.5.h” 这个头文件,因此 Vehicle 这个类重复定义了。
------解决方案--------------------
void car::show()cosnt
{
cout < < "the car 's name is: " < <name < <endl
< < "pl: " < <pl < <endl;
先建一个抽象类Vehicle,再从其派生出car,truck二个类,在最后的文件中定义一个打印函数,输出其内容,但出错了,请高手帮忙看看,错在哪里?在线等,马上给分!!
//15.5.h
#include <iostream.h>
class Vehicle
{
public:
Vehicle(char *);
virtual ~Vehicle();
virtual void show() const=0;
protected:
char *name;
};
///////////////////////////////////////
//15.5.cpp
#include <iostream.h>
#include <string.h>
#include "15.5.h "
Vehicle::Vehicle(char *na)
{
name=new char[strlen(na)+1];
strcpy(name,na);
}
Vehicle::~Vehicle()
{
delete [] name;
}
///////////////////////////////////////
//15.5_1.h
#include <iostream.h>
#include "15.5.h "
class car :public Vehicle
{
public:
car(char *,int);
~car();
void show()const;
private:
int pl;
};
/////////////////////////////////////////
//15.5_1.cpp
#include <iostream.h>
#include "15.5_1.h "
car::car(char *vname,int a):Vehicle(vname)
{
pl=a;
}
car::~car()
{
cout < < "the object of car is delete " < <endl;
}
void car::show()
{
cout < < "the car 's name is: " < <name < <endl
< < "pl: " < <pl < <endl;
}
/////////////////////////////////////////////
//15.5_2.h
#include <iostream.h>
#include "15.5.h "
class truck :public Vehicle
{
public:
truck(char *,char *);
~truck();
void show()const;
private:
char *color;
};
///////////////////////////////
//15.5_2.cpp
#include <iostream.h>
#include <string.h>
#include "15.5_2.h "
truck::truck(char *tname,char *tcolor):Vehicle(tname)
{
color=new char[strlen(tcolor)+1];
strcpy(color,tcolor);
}
truck::~truck()
{
delete [] color;
}
void truck::show()
{
cout < < "the truck 's name is: " < <name < <endl
< < "color: " < <color < <endl;
}
/////////////////////////////////
//15.5_3.cpp
#include <iostream.h>
//#include "15.5.h "
#include "15.5_1.h "
#include "15.5_2.h "
void schoolshow(Vehicle &);
int main()
{
car vcar( "Benz ",500);
truck vtruck( "Ferrari ", "yellow ");
schoolshow(vcar);
schoolshow(vtruck);
return 0;
}
void schoolshow(Vehicle school)
{
school.show();
}
------解决方案--------------------
void schoolshow(Vehicle& school)
{
school.show();
}
------解决方案--------------------
#include "15.5_1.h "
#include "15.5_2.h "
这两个头文件都包含了 “15.5.h” 这个头文件,因此 Vehicle 这个类重复定义了。
------解决方案--------------------
void car::show()cosnt
{
cout < < "the car 's name is: " < <name < <endl
< < "pl: " < <pl < <endl;