Function overriding in c++ || C++
In this, we are going to see a program on Function overriding in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

class BaseClass
{
public:
	int calc(int a,int b)
	{
		return a+b;
	}
};

class DerivedClass : public BaseClass
{
public:
	int calc(int a,int b)
	{
		return a*b;
	}
};

int main()
{
	clrscr();
	BaseClass obj;
	DerivedClass obj1;
	cout<<"Calculated value from Base Class object = "<<obj.calc(10,20)<<"\n";
	cout<<"Calculated value from Derived Class object = "<<obj1.calc(10,20);
	getch();
	return 0;
}

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;
     
class BaseClass
{
public:
	int calc(int a,int b)
	{
		return a+b;
	}
};

class DerivedClass : public BaseClass
{
public:
	int calc(int a,int b)
	{
		return a*b;
	}
};

int main()
{
	BaseClass obj;
	DerivedClass obj1;
	cout<<"Calculated value from Base Class object = "<<obj.calc(10,20)<<"\n";
	cout<<"Calculated value from Derived Class object = "<<obj1.calc(10,20);
	return 0;
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post