1) Write a C++ programme which demonstrate single inheritance and multiple inheritance.
2) Write a C++ programme which demonstrate hybrid inheritance and multilevel inheritance.
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout<<"Base class content.";
}
};
class B : public A {};
class C : public B {};
int main() {
C obj;
obj.display();
return 0;
}
#include <iostream>
using namespace std;
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};
class Bat: public Mammal, public WingedAnimal {};
int main() {
Bat b1;
return 0;
}
// C++ program to demonstrate hierarchical inheritance
#include <iostream>
using namespace std;
// base class
class Animal {
  public:
  void info() {
    cout << "I am an animal." << endl;
  }
};
// derived class 1
class Dog : public Animal {
  public:
  void bark() {
    cout << "I am a Dog. Woof woof." << endl;
  }
};
// derived class 2
class Cat : public Animal {
  public:
  void meow() {
    cout << "I am a Cat. Meow." << endl;
  }
};
int main() {
  // Create object of Dog class
  Dog dog1;
  cout << "Dog Class:" << endl;
  dog1.info(); // Parent Class function
  dog1.bark();
  // Create object of Cat class
  Cat cat1;
  cout << "\nCat Class:" << endl;
  cat1.info(); // Parent Class function
  cat1.meow();
  return 0;
}
Comments
Leave a comment