As promised in my previous blog on inheritance , we’ll take up an interesting example to get a better grip on inheritance concept ☺️ .
2. Base class, Social Media
In order to design the base class, we’ll need to figure out the common features of all the social media that we use. I can think of: Name of social media, number of letters per post (twitter allows 140), good for sharing pics/text/both, popularity, owned by (company name), advertising level and so on..
3. Social Media class definition
As per the various features identified for the base class in above section, the definition of the class will be as follows:
void setMediaName(char *mediaName);
}; //Social Media Class Definition over
4. Derived class, Facebook
Class facebookDer:public socialMediaBase
Since facebookDer class is derived from socialMediaBase class, all the parameters of base class are available in facebook class also.
In facebook, we have options to create photo albums. We have added a class function to facebookDer class for the same.
5. Derived class, Twitter
class twitterDer:public socialMediaBase
void isRetweetAllowed(int isAllowed);
}; // twitter class definition over
All of socialMediaBase class parameters are available to twitterDer class due to inheritance. Retweet is specific to twitter, so its function is added in twitterDer class.
Lets define the main function now. We create 2 objects, one each of twitter and facebook class. We set the media name using the base class function setMediaName . Then, functions specific to derived classes are called.
Using namespace std;
int main (void)
//Base class function setMediaName is //accessible by derived class object;
//Facebook class function
} //End Main function
Friends, I hope you enjoyed this example as much as I did creating it ☺️ . Please feel free to write to me for any clarifications.
Happy learning ☺️ .