0% found this document useful (0 votes)
17 views4 pages

Untitled Document 9

Uploaded by

pruthvi07u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Untitled Document 9

Uploaded by

pruthvi07u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Develop a c++ program to implement virtual base class.

#include <iostream>

using namespace std;

// Base class

class Base {

public:

int value;

Base() {

cout << "Base class constructor called." << endl;

value = 10; // Initialize value

};

// Class A inherits Base virtually

class A : virtual public Base {

public:

A() {

cout << "Class A constructor called." << endl;

};

int main() {

C obj; // Create an object of class C

obj.display(); // Display the value from the Base class

return 0;

Write a c++ program for multilevel inheritance.

#include <iostream>
using namespace std;

class Employee {
public:
void work() { cout << "Working..." << endl; }
};

class Manager : public Employee {


public:
void manage() { cout << "Managing team..." << endl; }
};

class Director : public Manager {


public:
void strategize() { cout << "Strategizing..." << endl; }
};

int main() {
Director d;
d.work(); // From Employee
d.manage(); // From Manager
d.strategize(); // From Director
return 0;
}

Write a program on single inheritance.

#include <iostream>

using namespace std;

// Base class

class Vehicle {

public:

void start() {

cout << "Vehicle started." << endl;

};

// Derived class

class Car : public Vehicle {

public:

void drive() {

cout << "Car is driving." << endl;

};

int main() {

Car myCar;

// Accessing functions from both base and derived class


myCar.start(); // From Vehicle (Base class)

myCar.drive(); // From Car (Derived class)

return 0;

Write a program on hybrid inheritance.

#include <iostream>

using namespace std;

// Base class 1

class Person {

public:

void displayPerson() {

cout << "I am a person." << endl;

};

// Base class 2

class Employee {

public:

void displayEmployee() {

cout << "I am an employee." << endl;

};

// Derived class inheriting from Person and Employee (Hybrid Inheritance)

class Manager : public Person, public Employee {

public:

void displayManager() {

cout << "I am a manager." << endl;

};

int main() {
Manager mgr;

// Accessing functions from multiple base classes and derived class

mgr.displayPerson(); // From Person

mgr.displayEmployee(); // From Employee

mgr.displayManager(); // From Manager

return 0;

You might also like