ITP

BCA - OOPS and Data Structures

Write a program to Implement Single Inheritance

In : BCA Subject : OOPS and Data Structures

In Single Inheritance , one class inherits from only one base (parent) class . 

Example :

#include <iostream>
using namespace std;

// Base class (Parent)
class Animal {
protected:
    string name;

public:
    void setName(string n) {
        name = n;
    }

    void getName() {
        cout << "Name: " << name << endl;
    }
};

// Derived class (Child) - Inherits from Animal
class Dog : public Animal {
public:
    void speak() {
        cout << name << " says: Woof!" << endl;
    }
};

int main() {
    Dog myDog;           // Create object of derived class

    myDog.setName("Buddy");  // Access function from parent class
    myDog.getName();         // Access another parent function
    myDog.speak();           // Access function from child class

    return 0;
}

About us

A truly open platform where you may ask questions and get answers. We also provide comprehensive and easy-to-understand answers to question papers.  discover...

Site status

Flag Counter

Privacy Policy

Sitemap