Trusted by millions of Kenyans
Study resources on Kenyaplex

Get ready-made curriculum aligned revision materials

Exam papers, notes, holiday assignments and topical questions – all aligned to the Kenyan curriculum.

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

Answer Attachments

Exams With Marking Schemes

Related Questions