Displaying Full Name and Initials using Hybrid Inheritance
In this program, we are going to Display Full Name and Initials using Hybrid Inheritance in C++ Programming Language. 
 


The Code given below can be used in TURBO C++ Compilers: -

#include<iostream.h>
#include<conio.h>

// base class
class GetFirstName
{
    public:
    char *FirstName;
    char FN;
    char getFN()
    {
	cout << "Enter first name: ";
	cin >> FirstName;
	FN = FirstName[0];
	return FN;
    }
};

// base class
class GetLastName
{
    public:
    char *LastName;
    char LN;
    char getLN()
    {
	cout << "Enter last name: ";
	cin >> LastName;
	LN = LastName[0];
	return LN;
    }
};


// second sub class
class PrintFullName : public GetFirstName, public GetLastName
{
    public:
    void printFullName()
    {
	cout << "\nFull name: " << FirstName <<" "<<LastName;
    }
};

// main function
void main()
{
    clrscr();
    PrintFullName obj1;
    obj1.getFN();
    obj1.getLN();
    obj1.printFullName();
    getch();
}

//.......Coded by SHREYA IDATE


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

// base class
class GetFirstName
{
public:
    string FirstName;
    void getFN()
    {
        cout << "Enter first name: ";
        cin >> FirstName;
    }
};

// base class
class GetLastName
{
public:
    string LastName;
    void getLN()
    {
        cout << "Enter last name: ";
        cin >> LastName;
    }
};

// second sub class
class PrintFullName : public GetFirstName, public GetLastName
{
public:
    string FullName;
    void printFullName()
    {
        FullName = FirstName + " " + LastName;
        cout << "\nFull name: " << FullName;
    }
};

// main function
int main()
{
    PrintFullName obj1;
    obj1.getFN();
    obj1.getLN();
    obj1.printFullName();
    return 0;
}

//........Coded by SHREYA IDATE


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post