Passing Arguments in Inheritance in C++

Passing arguments to function of class in inheritance in C++ programming. Inheritance allows us to inherit attributes and methods from one class to another class.

Program Inheritance Calling Function

Program

#include<iostream.h>
#include<conio.h>
 class cls
{
public:
void fun()
{
cout<<"This is fun() function of class cls";
}
};
class cls2:public cls //inheriting class 'cls2' from class 'cls'.
{
public:
void fun2()
{
cout<<"This is fun2() function of class cls2"<<endl;
fun(); //calling 'fun()' function.
}
};
void main()
{
cls2 obj; //'obj' is object of class 'cls2'.
obj.fun2(); //calling 'fun2()' function.
getch();
}
Output
This is fun2() function of class cls2
This is fun() function of class cls

Program Inheritance and Passing Argument

Program

#include<iostream.h>
#include<conio.h>
 class cls
{
public:
void fun()
{
cout<<"This is fun() function of class cls";
}
void fun2(char c)
{
cout<<"Iam in Section "<<c<<endl;
}
};
class cls2:public cls //inheriting class 'cls2' from class 'cls'.
{
public:
void fun3()
{
cout<<"This is fun3() function of class cls2"<<endl;
}
};
class cls3:public cls2 //inheriting class 'cls3' from class 'cls2'.
{
public:
cls3() //this is default constructor.
{
cout<<"This is Default Constructor of class cls3"<<endl;
}
};
void main()
{
cls3 ob; //'ob' is object of class 'cls3'.
ob.fun2('B'); //calling and passing argument.
ob.fun3(); //calling 'fun3()' function.
ob.fun(); //calling 'fun()' function.
getch();
}
Output
This is Default Constructor of class cls3
Iam in Section B
This is fun3() function of class cls2
This is fun() function of class cls