KEMBAR78
C++ Notes For CS Engineers Module-4 | PDF | Parameter (Computer Programming) | C++
0% found this document useful (0 votes)
17 views30 pages

C++ Notes For CS Engineers Module-4

Module-4 covers key concepts in C++ including overloading, templates, and inheritance. It explains function and operator overloading, provides examples of function and class templates, and discusses various types of inheritance such as single, multilevel, multiple, and hybrid inheritance. The module emphasizes the importance of these concepts for code flexibility, reusability, and reducing redundancy.

Uploaded by

prahladkumar3489
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)
17 views30 pages

C++ Notes For CS Engineers Module-4

Module-4 covers key concepts in C++ including overloading, templates, and inheritance. It explains function and operator overloading, provides examples of function and class templates, and discusses various types of inheritance such as single, multilevel, multiple, and hybrid inheritance. The module emphasizes the importance of these concepts for code flexibility, reusability, and reducing redundancy.

Uploaded by

prahladkumar3489
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/ 30

Module-4.

Overloading, Templates and Inheritance

Overloading
In C++, overloading allows you to define multiple functions or operators with the same
name, but with different signatures. This is a core concept in function overloading and
operator overloading, both of which help in making the code more flexible and easier
to read.

Key Concepts of Overloading


• Function Overloading: Multiple functions with the same name but different
parameter types or number of parameters.
• Operator Overloading: Allows you to redefine the behavior of operators (+, -, *,
etc.) for user-defined types (like classes).

Function Overloading
You can have multiple functions with the same name but with different parameters (either
in type or number of parameters).

Example: Function Overloading


#include <iostream>
using namespace std;

class Calculator {
public:
// Function to add two integers
int add(int a, int b) {
return a + b;
}

// Function to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Function to add two floating-point numbers


float add(float a, float b) {
return a + b;
}

1
};

int main() {
Calculator calc;

// Calling the overloaded add function


cout << "Sum of 5 and 10 (int): " << calc.add(5, 10) << endl;
cout << "Sum of 5, 10, and 15 (int): " << calc.add(5, 10, 15) << endl;
cout << "Sum of 5.5 and 10.5 (float): " << calc.add(5.5f, 10.5f) << endl;

return 0;
}

Key Points of Function Overloading


• Same function name, but with different parameter types or number of
parameters.
• Return type does not matter for overloading. The compiler distinguishes
overloaded functions by parameters alone.
• Useful for simplifying code when you need to perform similar operations on
different types of data.

Operator Overloading
Operator overloading allows you to redefine the behavior of operators for user-defined
types (classes). This allows you to perform operations on objects in the same way you
would with built-in types.

Example: Operator Overloading


Let's overload the + operator for a class Point to add two Point objects.
#include <iostream>
using namespace std;

class Point {
public:
int x, y;

// Constructor
Point(int x_val, int y_val) : x(x_val), y(y_val) {}

2
// Overload the '+' operator to add two Point objects
Point operator+(const Point& p) {
return Point(x + p.x, y + p.y);
}

void display() const {


cout << "Point(" << x << ", " << y << ")" << endl;
}
};

int main() {
Point p1(1, 2), p2(3, 4);

// Using overloaded '+' operator


Point p3 = p1 + p2; // p3.x = 4, p3.y = 6

p3.display(); // Output: Point(4, 6)

return 0;
}

Key Points of Operator Overloading


• Custom Behavior for Operators: Allows you to define how operators like +, -, *,
etc., behave with objects of user-defined types.
• Syntax: Use the operator keyword followed by the operator being overloaded.
• Can be used for most operators like +, -, ==, <, [], (), etc.
• Does not change operator precedence or associativity.

Example: Overloading the << and >> Operators (Stream Insertion/Extraction)


You can also overload stream operators to enable easy output of objects to cout.
#include <iostream>
using namespace std;

class Box {
public:
int length, width;

Box(int l, int w) : length(l), width(w) {}

3
// Overload the '<<' operator for output
friend ostream& operator<<(ostream& os, const Box& b) {
os << "Length: " << b.length << ", Width: " << b.width;
return os;
}
};

int main() {
Box b(10, 5);

// Using the overloaded '<<' operator


cout << b << endl; // Output: Length: 10, Width: 5

return 0;
}

Templates in C++
Templates in C++ allow you to create generic classes or functions that can work with any
data type. Templates enable you to write generic code once and use it for different data
types, reducing redundancy and increasing flexibility.
There are two primary types of templates in C++:
1. Function Templates - Allows you to write generic functions.
2. Class Templates - Allows you to write generic classes.

Function Templates
A function template is a blueprint for generating functions that can operate on different
data types.

Syntax of Function Template


template <typename T>
T functionName(T arg1, T arg2) {
// Function implementation
return arg1 + arg2;
}
• template <typename T>: Declares a template. T is a placeholder for any data type.
• T: The placeholder that gets replaced by the actual type when the function is called.

Example: Function Template to Add Two Numbers

4
#include <iostream>
using namespace std;

// Function template for addition


template <typename T>
T add(T a, T b) {
return a + b;
}

int main() {
cout << "Addition of 5 and 3 (int): " << add(5, 3) << endl; // Works for int
cout << "Addition of 5.5 and 3.2 (float): " << add(5.5, 3.2) << endl; // Works for float

return 0;
}

Class Templates
A class template is a blueprint for creating generic classes that can operate with any
data type.

Syntax of Class Template


template <typename T>
class ClassName {
T value; // A member variable of type T

public:
ClassName(T val) : value(val) {}

void showValue() {
cout << "Value: " << value << endl;
}
};
template <typename T>: Declares a template for the class.

T: The placeholder for the data type that will be used.

5
Example: Class Template for a Simple Box
#include <iostream>
using namespace std;

// Class template for a Box


template <typename T>
class Box {
T value;

public:
Box(T v) : value(v) {}

void showValue() {
cout << "Box Value: " << value << endl;
}
};

int main() {
Box<int> box1(10); // Box with int
Box<double> box2(3.14); // Box with double
Box<string> box3("Hello"); // Box with string

box1.showValue();
box2.showValue();
box3.showValue();

return 0;
}

Template Specialization
Sometimes, you may want to specialize a template for a particular data type (like int,
char, or float). Template specialization allows you to define a different behavior for
specific types.

Example: Specializing a Template for char Type


#include <iostream>
using namespace std;

6
// General template
template <typename T>
void print(T value) {
cout << "General template: " << value << endl;
}

// Specialization for char


template <>
void print<char>(char value) {
cout << "Specialized template for char: " << value << endl;
}

int main() {
print(10); // Uses general template
print("Hello"); // Uses general template
print('A'); // Uses specialized template for char

return 0;
}

Template with Multiple Parameters


You can use multiple template parameters in a function or class template.

Example: Function Template with Two Parameters


#include <iostream>
using namespace std;

// Function template with two type parameters


template <typename T, typename U>
T add(T a, U b) {
return a + b;
}
int main() {
cout << "Addition of 5 and 3.5 (int + double): " << add(5, 3.5) << endl; // Works for
different types

return 0;
}

7
Template with Default Arguments
You can also provide default types for template parameters.

Example: Template with Default Parameter


#include <iostream>
using namespace std;

// Class template with default parameter


template <typename T = int>
class Box {
T value;

public:
Box(T v) : value(v) {}

void showValue() {
cout << "Box Value: " << value << endl;
}
};

int main() {
Box<> box1(10); // Default type is int
Box<double> box2(3.14); // Explicitly specifying double

box1.showValue();
box2.showValue();

return 0;
}

Inheritance in C++
Inheritance is a fundamental concept in Object-Oriented Programming (OOP). It allows
a new class (called the derived class) to inherit the properties and behaviors (data
members and member functions) from an existing class (called the base class). The
primary goal of inheritance is reusability—it enables the creation of a new class based
on an existing class, which reduces redundancy.

8
In C++, the class which inherits the members of another class is called derived class and
the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.

Types of Inheritance in C++


There are several types of inheritance in
C++, each defined based on how many
classes are involved and how they are
related. Here are the main types:
1. Single Inheritance: One base class
and one derived class.
2. Multiple Inheritance: A derived
class inherits from more than one
base class.
3. Multilevel Inheritance: A derived
class inherits from a base class, and
then another class inherits from that derived class.
4. Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
5. Hybrid Inheritance: A combination of two or more types of inheritance.

Derived Classes
A Derived class is defined as the class derived from the base class.
The Syntax of Derived class:
class derived_class_name :: visibility-mode base_class_name
{
// body of the derived class.
}

1. Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited from
the only one base class.

9
Example 1
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
In the above example, Employee is the base class and Programmer is the derived class.

Example 2
#include <iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};

class B : private A
{
public:
void display()

10
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();

return 0;
}

In the above example, class A is privately inherited. Therefore, the mul() function of class
'A' cannot be accessed by the object of class B. It can only be accessed by the member
function of class B.

How to make a Private Member Inheritable


The private member is not inheritable. If we modify the visibility mode by making it public,
but this takes away the advantage of data hiding.

C++ introduces a third visibility modifier, i.e., protected. The member which is declared
as protected will be accessible to all the member functions within the class as well as the
class immediately derived from it.

Visibility modes can be classified into three categories:

o Public: When the member is declared as public, it is accessible to all the functions
of the program.
o Private: When the member is declared as private, it is accessible within the class
only.
o Protected: When the member is declared as protected, it is accessible within its

11
own class as well as the class immediately derived from it.
Visibility of Inherited Members
Base class visibility Derived class visibility

Public Private Protected


Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
Public Public Private Protected

2. Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another derived class.

Example
When one class inherits another class which is further inherited by another class, it is
known as multi level inheritance in C++. Inheritance is transitive so the last derived class
acquires all the members of all its base classes.

#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};

12
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}

3. Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes
from two or more classes.

Syntax of the Derived class:


class D : visibility B-1, visibility B-2, ?
{
// Body of the class;
}

13
Example

#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};

class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);

14
c.get_b(20);
c.display();

return 0;
}

Ambiquity Resolution in Inheritance


Ambiguity can be occurred in using the multiple inheritance when a function with the
same name occurs in more than one base class.

Example
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
std::cout << "Class A" << std::endl;
}
};
class B
{
public:
void display()
{
std::cout << "Class B" << std::endl;
}
};
class C : public A, public B
{
public:
void view()
{
A::display();
}
};
int main()

15
{
C c;
c.view(); //call view function
return 0;
}

The above issue can be resolved by using the class resolution operator with the function.
In the above example, the derived class code can be rewritten as:

class C : public A, public B


{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.

}
};

An ambiguity can also occur in single inheritance.

4. Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.

16
Example
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};

class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
class D : public B, public C

17
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}

5. Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class from a
base class.

Syntax of Hierarchical inheritance:


class A
{
// body of the class A.
}
class B : public A
{

18
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}

Example
#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()

19
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <<m<< std::endl;
std::cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}

Virtual Base Class


In C++, virtual inheritance is used to solve the diamond problem when multiple
inheritance occurs. The diamond problem arises when a class is derived from two classes
that share a common base class. Without virtual inheritance, the common base class
would be inherited multiple times, leading to ambiguity and redundancy in the derived
class.
By using virtual inheritance, we ensure that the base class is only inherited once, even
in the case of multiple inheritance.
What is the Diamond Problem?
The diamond problem occurs when a class inherits from two classes that both inherit
from a common base class. Without virtual inheritance, the base class will be inherited
twice, leading to redundant and conflicting copies of the base class data members.
Here’s a simple representation of the diamond problem:

20
Base Class
/ \
Derived1 Derived2
\ /
Derived3 (This class inherits from Derived1 and Derived2)
If Derived1 and Derived2 both inherit from Base Class, then Derived3 would have two
copies of Base Class. This can lead to ambiguity in function calls and waste memory.

What is Virtual Inheritance?


Virtual inheritance ensures that a class is only inherited once by the derived class, even
if it appears multiple times in the inheritance hierarchy. This eliminates the diamond
problem and ensures that there is only one instance of the base class in the final derived
class.

Syntax of Virtual Inheritance


To implement virtual inheritance in C++, we use the virtual keyword when defining the
base class inheritance.

class Derived1 : virtual public Base {


// Derived1 class code
};

class Derived2 : virtual public Base {


// Derived2 class code
};

class Derived3 : public Derived1, public Derived2 {


// Derived3 class code
};
In the example above, both Derived1 and Derived2 use virtual inheritance to inherit from
Base. This ensures that Derived3 has only one instance of Base.

Example: Virtual Base Class


#include <iostream>
using namespace std;

// Base class
class Base {

21
public:
int value;

Base(int v) : value(v) {
cout << "Base class constructor, value = " << value << endl;
}

void display() {
cout << "Base class value: " << value << endl;
}
};

// Derived class 1
class Derived1 : virtual public Base {
public:
Derived1(int v) : Base(v) {
cout << "Derived1 constructor" << endl;
}
};

// Derived class 2
class Derived2 : virtual public Base {
public:
Derived2(int v) : Base(v) {
cout << "Derived2 constructor" << endl;
}
};

// Further derived class


class Derived3 : public Derived1, public Derived2 {
public:
Derived3(int v) : Base(v), Derived1(v), Derived2(v) {
cout << "Derived3 constructor" << endl;
}
};

int main() {
Derived3 obj(10); // Constructing Derived3 object with value 10

22
obj.display(); // Accessing the display method of Base class

return 0;
}

Abstract Class in C++


In C++, an abstract class is a class that cannot be instantiated directly. It serves as a
base class for other classes and contains at least one pure virtual function. An abstract
class provides a blueprint for derived classes, enforcing that they implement specific
functions.
Abstract classes are crucial in Object-Oriented Programming (OOP) as they enable
polymorphism, allowing different classes to have a common interface while providing
their own specific implementations.

Pure Virtual Function


A pure virtual function is a function declared in a class that has no implementation in
that class and must be implemented by any non-abstract derived class. It is defined by
using the = 0 syntax in the function declaration.
Syntax:
class AbstractClass {
public:
virtual void show() = 0; // Pure virtual function
};
• = 0 makes the function pure virtual.
• A class that contains one or more pure virtual functions is called an abstract class.

Key Characteristics of Abstract Classes


1. Cannot be Instantiated: You cannot create objects of an abstract class directly.
AbstractClass obj; // Error: Cannot instantiate an abstract class.
2. Must Have Pure Virtual Functions: An abstract class must have at least one pure virtual
function. If all functions in a class are implemented, the class can be instantiated.
3. Derived Classes Must Implement Pure Virtual Functions: Any derived class of
an abstract class must provide an implementation for all pure virtual functions to
become a concrete (non-abstract) class. Otherwise, the derived class remains
abstract.
4. Can Have Non-Pure Virtual Functions: An abstract class can also have regular
member functions (functions with implementations). These functions can be used
by derived classes.

23
Syntax of an Abstract Class
class AbstractClass {
public:
virtual void pureVirtualFunction() = 0; // Pure virtual function
void regularFunction() {
// Regular function with implementation
std::cout << "Regular function" << std::endl;
}
};

Example of an Abstract Class


#include <iostream>
using namespace std;

// Abstract class
class Shape {
public:
// Pure virtual function
virtual void draw() = 0;

// Regular function
void info() {
cout << "This is a shape." << endl;
}
};

// Derived class: Circle


class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle!" << endl;
}
};

// Derived class: Rectangle


class Rectangle : public Shape {
public:
void draw() override {

24
cout << "Drawing a Rectangle!" << endl;
}
};

int main() {
// Shape shape; // Error: cannot instantiate abstract class

Circle c;
Rectangle r;

// Calling functions from derived classes


c.draw(); // Output: Drawing a Circle!
r.draw(); // Output: Drawing a Rectangle!

c.info(); // Output: This is a shape.


r.info(); // Output: This is a shape.

return 0;
}

Why Use Abstract Classes?


1. Enforce Consistency in Derived Classes:
• An abstract class allows you to define a common interface (set of pure virtual
functions) that all derived classes must implement. This ensures that all derived
classes provide specific behaviors for these functions.
2. Polymorphism:
• Abstract classes enable polymorphism in C++. For example, if you have a pointer
to the base abstract class, you can call the draw() function, and it will call the
appropriate implementation depending on the actual object type (whether it's a
Circle, Rectangle, etc.).
3. Code Reusability:
• Abstract classes allow you to write general code that can be shared across all
derived classes, while still allowing flexibility for specific implementations in the
derived classes.
Abstract Class with Multiple Derived Classes
You can define multiple derived classes from an abstract class and implement their own
versions of the pure virtual functions.

#include <iostream>

25
using namespace std;

class Vehicle {
public:
virtual void start() = 0; // Pure virtual function
virtual void stop() = 0; // Pure virtual function
};

class Car : public Vehicle {


public:
void start() override {
cout << "Car is starting." << endl;
}
void stop() override {
cout << "Car is stopping." << endl;
}
};

class Bike : public Vehicle {


public:
void start() override {
cout << "Bike is starting." << endl;
}
void stop() override {
cout << "Bike is stopping." << endl;
}
};

int main() {
// Vehicle vehicle; // Error: Cannot instantiate abstract class

Car car;
Bike bike;

car.start(); // Output: Car is starting.


car.stop(); // Output: Car is stopping.

bike.start(); // Output: Bike is starting.

26
bike.stop(); // Output: Bike is stopping.

return 0;
}

Pointer and Inheritance in C++


In C++, pointers are a fundamental concept that allow you to dynamically allocate
memory and access objects through references. When it comes to inheritance, pointers
play a crucial role in enabling polymorphism, where you can treat objects of derived
classes as objects of base classes.
Let’s break this down and explain how pointers work in the context of inheritance.

Polymorphism with Pointers


Polymorphism is one of the core features of Object-Oriented Programming (OOP). It
allows objects of different derived classes to be treated as objects of a common base
class. In C++, polymorphism typically works with virtual functions and base class
pointers.
1. Base Class Pointer to Derived Class Objects
You can use a base class pointer to point to objects of derived classes. This is essential
for polymorphic behavior because the base class pointer can refer to both base and
derived class objects, allowing you to call overridden functions based on the actual type
of the object, not the pointer type.

Syntax:
BaseClass *ptr; // Pointer to base class
DerivedClass obj; // Object of derived class
ptr = &obj;

2. Virtual Functions
To enable dynamic dispatch (i.e., calling the correct function based on the actual object
type, not the pointer type), we need to use virtual functions. A virtual function in the
base class ensures that the overridden version of the function in the derived class gets
called, even when accessed via a base class pointer.

Example of Pointers and Inheritance


#include <iostream>
using namespace std;

27
// Base class
class Animal {
public:
virtual void sound() { // Virtual function
cout << "Animal makes a sound!" << endl;
}
};

// Derived class 1
class Dog : public Animal {
public:
void sound() override { // Override the base class function
cout << "Dog barks!" << endl;
}
};

// Derived class 2
class Cat : public Animal {
public:
void sound() override { // Override the base class function
cout << "Cat meows!" << endl;
}
};

int main()
{
Animal *animalPtr; // Pointer to base class
Dog dog;
Cat cat;

// Base class pointer pointing to derived class objects


animalPtr = &dog;
animalPtr->sound(); // Output: Dog barks!
animalPtr = &cat;
animalPtr->sound(); // Output: Cat meows!

return 0;
}

28
Overloading Member Functions in C++
In C++, function overloading allows you to define multiple functions with the same
name but with different parameters. This helps in creating functions that perform similar
tasks but for different types or numbers of arguments.
For member functions in a class, overloading means defining multiple functions with the
same name, but they must have different signatures. A signature consists of the
function's name and its parameters (types, number, or order of parameters). The return
type is not considered part of the signature for overloading.

Why Overload Member Functions?


1. Reusability: Overloading allows you to use the same function name for different
operations, making the code cleaner and easier to maintain.
2. Flexibility: You can handle various data types or different numbers of parameters
with a single function name.
3. Simplification: It simplifies the code by reducing the need for different function
names that perform similar tasks.

Syntax for Overloading Member Functions


To overload a member function, you define multiple versions of the function with the
same name but with different parameters. The number or type of parameters should differ
between the overloaded functions.

class ClassName {
public:
returnType functionName(parameterList); // Function 1
returnType functionName(parameterList); // Function 2
// ... other overloaded functions
};

• Overloaded functions must differ in the number or types of parameters. If two


functions only differ in return type, it's not considered overloading.

Example of Overloading Member Functions


Here’s a simple example where a class Calculator overloads the add function to
handle different numbers and types of parameters.

#include <iostream>
using namespace std;

29
class Calculator {
public:
// Member function to add two integers
int add(int a, int b) {
return a + b;
}

// Overloaded member function to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Overloaded member function to add two floating-point numbers


double add(double a, double b) {
return a + b;
}
};

int main() {
Calculator calc;

cout << "Sum of 2 integers: " << calc.add(5, 10) << endl; // Calls add(int, int)
cout << "Sum of 3 integers: " << calc.add(5, 10, 15) << endl; // Calls add(int, int,
int)
cout << "Sum of 2 doubles: " << calc.add(5.5, 10.5) << endl; // Calls add(double,
double)

return 0;
}

30

You might also like