C++中基类对象无法赋给派生类对象,那么怎样才能将只将基类中的两个成员数据赋给派生类相应的成员数据?
//debug.cpp#include<iostream>usingnamespacestd;#include"animal.h"#include"lion.h"#inc...
//debug.cpp
#include <iostream>
using namespace std;
#include "animal.h"
#include "lion.h"
#include "dog.h"
int main()
{
Animal a1(0,0);
Dog d1(60, 120, "Fido");
Dog d2(0,0);
Lion lion1(45, 300);
a1.print();
d1.Print();
d2.Print();
lion1.Print();
a1=d1;
cout<<"Animal 1 now has the same height and weight"
<< "as dog 1\n";
a1.print();
d2=a1;
cout<<"Dog 2 now has the same height and weight"
<< "as animal 1\n";
d2.Print();
return 0;
}
d2=a1;这一句,将60,120给d2 展开
#include <iostream>
using namespace std;
#include "animal.h"
#include "lion.h"
#include "dog.h"
int main()
{
Animal a1(0,0);
Dog d1(60, 120, "Fido");
Dog d2(0,0);
Lion lion1(45, 300);
a1.print();
d1.Print();
d2.Print();
lion1.Print();
a1=d1;
cout<<"Animal 1 now has the same height and weight"
<< "as dog 1\n";
a1.print();
d2=a1;
cout<<"Dog 2 now has the same height and weight"
<< "as animal 1\n";
d2.Print();
return 0;
}
d2=a1;这一句,将60,120给d2 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询