C++ Object Creation

See to this code and try guessing the output

#include
using namespace std;

class Parent {

protected:
int num_p;
public:
Parent () : num_p (0) { }
Parent (const Parent& r) { num_p = r.num_p; }
int GetNumP () { return num_p; }
void SetNumP (int n) { num_p = n;}
};

class Child : public Parent {

protected:
int num_c;
public:
Child () : num_c (0) { }
Child (const Child& r) { num_c = r.num_c;}
int GetNumC () { return num_c;}
void SetNumC (int n) { num_c = n; }
};

int main( )
{

Child obj;
obj.SetNumP (10);
obj.SetNumC (20);

Child test = obj;
cout

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: