| | |
Making Derived Class Object Equal To Base Class Object
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I know that you can make a derived class object equal to a base class object, but I don't know how to do it the other way around. Here's the part of my code that I'm having difficulty with (Note: Animal is base class; Lion and Dog are derived classes).
The error message I get suggests that I need to overload the = operator to perform this, but for this assignment, I don't believe we are allowed to do that. How else can you do this?
// main.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "animal.h"
#include "lion.h"
#include "dog.h"
int main()
{
Animal a1( 0, 0 );
Dog d1( 60, 120, "Fido" );
Dog d2(0,0,"Toto");
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();
system("pause");
return 0;
} // end mainThe error message I get suggests that I need to overload the = operator to perform this, but for this assignment, I don't believe we are allowed to do that. How else can you do this?
![]() |
Other Threads in the C++ Forum
- Previous Thread: help....inventory system...
- Next Thread: help with overload of operator >>
Views: 513 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





