Inheritance

Posted on July 11, 2013. Filed under: C++ |


Same as Java concept, C++ allow inheritance, with slightly different syntax.
========================
//Mother.h
class Mother{
public:
int publicv;
protected:
int protectedv;
private:
int privatev;
};

==========================
//Daughter.h
//class Daughter inherit all the public and protected variables and methods from class Mother
class Daughter: public Mother { }

====================
#Daughter.cpp
#include
#include “Mother.h” //has to come before #include “Daughter.h”
#include “Daughter.h”
using namespace std;

void Daughter::doSomething(){
publicv = 1;
protectedv = 1;
//no access to privatev
}

Advertisements

Make a Comment

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

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: