When an object of a derived class is assigned to another object of the same derived class, is the data associated with the base class...

      

When an object of a derived class is assigned to another object of the same derived class, is the data associated with the base class copied? To find out, use the following two classes and write a program that demonstrates what happens.
class base {
int a;
public:
void load_a(int n) { a= n; }
int ge() { return a;}
};
class derived : public base {
int b;
public:
void load_b(int n) {b=n;}
int get_b() {return b;}
};

  

Answers


Davis
Yes, data from class is also copied when an object of a derived class is assigned to another object of the same derived class.Here is a program that demonstrates this fact:
#include
using namespace std;
class base {
int a;
public:
void load_a(int n) {a = n ; }
int get_a() {return a;}
};
class de: public base {
int b;
public:
void load_b(int n) {b=n;}
int get_b() {return b;}
};
int main ()
{
derived ob1, ob2;
ob1.load_a(5);
ob1.lo);
// assign ob1 to ob2
ob2 =ob1;
cout << "Here is ob1's a and b: " ;
cout <cout << "Here is ob2's a and b:";
cout << ob2.get_a() << ' ' << ob2.get_b () << "\n";
/*As you can probably guess, the output is the same for each object.*/
return 0;
}
Githiari answered the question on May 29, 2018 at 17:40


Next: Using the stack class, write a function called loadstack() that returns a stack that is already loaded with letters of the alphabet (a-z.). Assign this...
Previous: Given this class; class planet { int moons; double dist_from_sun; // in miles double diameter; double mass; public: //... double get_ () { return disfrom_sun; } }; Create a function called light() using c++...

View More Computer Science Questions and Answers | Return to Questions Index


Exams With Marking Schemes

Related Questions