0% found this document useful (0 votes)
4 views7 pages

CPP Inheritance Concepts

Inheritance

Uploaded by

0112cs221035
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)
4 views7 pages

CPP Inheritance Concepts

Inheritance

Uploaded by

0112cs221035
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/ 7

nstructor Calling, Multiple Inheritance, Diamond Problem, Method Overloading,

1. Constructor Calling in C++:

When you create an object of a class, its constructor gets called automatically. Example:

#include <iostream>

using namespace std;

class Base {

public:

Base() {

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

};

class Derived : public Base {

public:

Derived() {

cout << "Derived class constructor called" << endl;

};

int main() {

Derived obj; // Both Base and Derived constructors will be called

return 0;

}
2. Multiple Inheritance in C++:

Multiple inheritance allows a derived class to inherit from more than one base class.

#include <iostream>

using namespace std;

class A {

public:

void showA() {

cout << "Class A" << endl;

};

class B {

public:

void showB() {

cout << "Class B" << endl;

};

class C : public A, public B {

public:

void showC() {

cout << "Class C" << endl;

}
};

int main() {

C obj;

obj.showA();

obj.showB();

obj.showC();

return 0;

3. Diamond Problem in C++:

The diamond problem occurs when a class inherits from two classes that both inherit from a single

class.

#include <iostream>

using namespace std;

class A {

public:

A() {

cout << "Constructor of A called" << endl;

void show() {

cout << "Class A" << endl;

};
class B : virtual public A {

public:

B() {

cout << "Constructor of B called" << endl;

};

class C : virtual public A {

public:

C() {

cout << "Constructor of C called" << endl;

};

class D : public B, public C {

public:

D() {

cout << "Constructor of D called" << endl;

};

int main() {

D obj;

obj.show(); // No ambiguity due to virtual inheritance

return 0;
}

4. Method Overloading in C++:

Method overloading is when two or more methods in the same class have the same name but

different parameters.

#include <iostream>

using namespace std;

class MathOperations {

public:

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

};

int main() {

MathOperations obj;

cout << "Sum (int): " << obj.add(5, 3) << endl;

cout << "Sum (double): " << obj.add(5.5, 3.3) << endl;

return 0;

}
5. Method Overriding in C++:

Method overriding occurs when a derived class provides its own implementation of a method

already defined in its base class.

#include <iostream>

using namespace std;

class Base {

public:

virtual void show() {

cout << "Base class show method" << endl;

};

class Derived : public Base {

public:

void show() override {

cout << "Derived class show method" << endl;

};

int main() {

Base* basePtr;

Derived obj;

basePtr = &obj;
basePtr->show(); // Calls Derived class show() method because of overriding

return 0;

You might also like