C++ 35:Single level Inheritance

 


Inheritance (or Generalization):

A generalization is a taxonomic relationship between a more general classifier and a more specific classifier. Each instance of the specific classifier is also an indirect instance of the general classifier. Thus, the specific classifier inherits the features of the more general classifier.

  • Represents an "is-a" relationship.
  • An abstract class name is shown in italics.
  • SubClass1 and SubClass2 are specializations of SuperClass.

The figure below shows an example of inheritance hierarchy. SubClass1 and SubClass2 are derived from SuperClass. The relationship is displayed as a solid line with a hollow arrowhead that points from the child element to the parent element.

Inheritance (or Generalization)

Inheritance Example - Shapes

The figure below shows an inheritance example with two styles. Although the connectors are drawn differently, they are semantically equivalent.

Inheritance Example - Shapes


Single Level Inheritance


Example:

#include <iostream>
using namespace std;

class Person
{
protected:
    char name[20];
    int age;

public:
    void readdata()
    {
        cout << "Enter Name : ";
        cin >> name;
        cout << "Enter age : ";
        cin >> age;
    }
    void showdata()
    {
        cout << "Name is: " << name << endl;
        cout << "Age is : " << age << endl;
    }
};
class Citizen : public Person
{
private:
    char nat[10];

public:
    void readdata()
    {
        Person::readdata();
        cout << "Enter Nationalty: ";
        cin >> nat;
    }
    void showdata()
    {
        Person::showdata();
        cout << "Nationality: " << nat << endl;
    }
};
int main()
{
    Citizen a;
    a.readdata();
    a.showdata();
    return 0;
}

Output:

Enter Name : Hary
Enter age : 34
Enter Nationalty: Ind
Name is: Hary
Age is : 34
Nationality: Ind



Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected