Inheritance Concept
Inheritance Concept
Inheritance Concept
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are vehicles"<<endl;
}
};
// sub class derived from two base classes
class Car: public fourWheeler{
public:
Car()
{
cout<<"Car has 4 Wheels"<<endl;
}
};
// main function
int main()
{
//creating object of sub class will
//invoke the constructor of base classes
Car obj;
return 0;
}
3. C++ program for Multiple Inheritance.
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
4. C++ program for demonstrating the accessibility in derived class in Inheritance.
#include <iostream>
using namespace std;
class Base {
protected :
int num = 7; //protected value
};
class Derived : public Base {
public :
void func() {
cout << "The value of num is: " << num;
}
};
int main() {
Derived obj;
obj.func();
return 0;
}