diamond problem

 





The diamond problem occurs when two superclasses of a class have a common base class.

 For example, in the following diagram, the TA class gets two copies of all attributes of Person class, this causes ambiguities.



===================================

#include<iostream> 

using namespace std; 

class Person { 

// Data members of person 

public: 

Person(int x) { cout << "Person::Person(int ) called" << endl; } 

}; 


class Faculty : public Person { 

// data members of Faculty 

public: 

Faculty(int x):Person(x) { 

cout<<"Faculty::Faculty(int ) called"<< endl; 

}; 


class Student : public Person { 

// data members of Student 

public: 

Student(int x):Person(x) { 

cout<<"Student::Student(int ) called"<< endl; 

}; 


class TA : public Faculty, public Student { 

public: 

TA(int x):Student(x), Faculty(x) { 

cout<<"TA::TA(int ) called"<< endl; 

}; 


int main() { 

TA ta1(30); 

``````````````````````````````````````````````````````

Person::Person(int ) called

Faculty::Faculty(int ) called

Person::Person(int ) called

Student::Student(int ) called

TA::TA(int ) called

`````````````````````````````````````````````````````````````


===============================



#include <iostream>

using namespace std;


class Animal{

public:

    Animal(){

        cout << "animal constructor" << endl;

    }

    int age;

    void walk(){

        cout << "animal walks" << endl;

    }

};


class Tiger :virtual public Animal{

public:

    Tiger(){

        cout << "constructor of tiger" << endl;

    }

};


class Lion :virtual public Animal{

public:

    Lion(){

        cout << "constructor of Lion" << endl;

    }

};


class Liger : public Tiger , public Lion{

public:

    Liger(){

        cout << "constructor of Liger" << endl;

    }

};


int main()

{

    Liger anil;

    anil.walk();

    return 0;

}

===========================


animal constructor

constructor of tiger

constructor of Lion

constructor of Liger

animal walks



=================================











Comments

Popular posts from this blog

Difference between Structure and Array in C

Mutitreading Notes