Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views9 pages

Week 13

The document explains the concepts of friend functions and classes in C++, which allow access to private and protected members from outside the class. It also covers polymorphism, including function overloading, operator overloading, and function overriding, emphasizing the importance of virtual functions for dynamic polymorphism. Examples are provided to illustrate these concepts, including a demonstration of function overriding with base and derived classes.

Uploaded by

ziomlubda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Week 13

The document explains the concepts of friend functions and classes in C++, which allow access to private and protected members from outside the class. It also covers polymorphism, including function overloading, operator overloading, and function overriding, emphasizing the importance of virtual functions for dynamic polymorphism. Examples are provided to illustrate these concepts, including a demonstration of function overriding with base and derived classes.

Uploaded by

ziomlubda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Objective: (Mapping CLO 03+4 + CLO 01)

 Friend Functions- Friend classes


 C++ Polymorphism

Friend Functions- Friend classes:


Data hiding is a fundamental concept of object-oriented programming. It restricts
the access of private members from outside of the class.
Similarly, protected members can only be accessed by derived classes and are
inaccessible from outside. For example,

However, there is a feature in C++ called friend functions that break this rule and
allow us to access member functions from outside the class.
A friend function can access the private and protected data of a class. We declare a
friend function using the friend keyword inside the body of the class.

Syntax:
class ClassName {
friend returnType functionName(parameters); // friend declaration
};
Example 1: Working of friend Function
C++ program to demonstrate the working of friend function.
#include <iostream>
using namespace std;

class Box {
private:
int width;

public:
Box() : width(10) {}

// Friend function declaration


friend void printWidth(Box b);
};

// Friend function definition (not a member of Box)


void printWidth(Box b) {
cout << "Width: " << b..width << endl; // Can access private member
}

int main() {
Box b;
printWidth(b); // Output: Width: 10
return 0;
}

Used the word const:


#include <iostream>
using namespace std;

class Box {
private:
int width;

public:
Box(){
Width=10;
}

// Friend function declaration


friend void printWidth(const Box b);
};

// Friend function definition (not a member of Box)


void printWidth( Box b) {
cout << "Width: " << b.width << endl; // Can access private member
b.width = 15;
}

int main() {
Box b;
printWidth(b); // Output: Width: 10
return 0;
}

Friend classes:
We can also use a friend Class in C++ using the friend keyword. For example,

hen a class is declared a friend class, all the member functions of the friend class
become friend functions.
Since ClassB is a friend class, we can access all members of ClassA from inside
ClassB.
However, we cannot access members of ClassB from inside ClassA. It is because
friend relation in C++ is only granted, not taken.
C++ Polymorphism:
Polymorphism is an important concept of object-oriented programming. It simply
means more than one form. That is, the same entity (function or operator) behaves
differently in different scenarios. For example,
The + operator in C++ is used to perform two specific functions. When it is used
with numbers (integers and floating-point numbers), it performs addition.

We can implement polymorphism in C++ using the following ways:


 Function overloading
 Operator overloading
 Function overriding
 Virtual functions

Function Overriding
When we call the function using an object of the derived class, the function of the
derived class is executed instead of the one in the base class.
So, different functions are executed depending on the object calling the function.
This is known as function overriding in C++.
Here, we have used a print() function in the Base class and the same function in the
Derived class. When we call print() using the Derived object derived1, it overrides
the print() function of Base by executing the print() function of the Derived class.
It's a runtime polymorphism because the function call is not resolved by the
compiler, but it is resolved in the runtime instead.
Function overriding allows a derived class to provide a different implementation for a
method that is already defined in the base class. It enables dynamic polymorphism
where the appropriate method is selected at runtime based on the object type.
Function overriding is the ability of redefining a specific behavior (function) of a base
class within derived class(es). Besides the provisioning of a specific modified
implementation of a base class function, function overriding is used to perform
runtime polymorphism.
How to Solve …..

Key Points:
 The function in the base class must be declared as virtual to allow overriding.
 The function in the derived class must have the same signature as the one in the base class.
 Overriding enables dynamic (runtime) binding of methods.

Example of Function Overriding:


#include <iostream>
using namespace std;

class Base {
public:
virtual void show() {
cout << "Base class show function called" << endl;
}
};

class Derived : public Base {


public:
void show() override { // overriding the base class function
cout << "Derived class show function called" << endl;
}
};

int main() {
Base *bptr;
Derived d;
bptr = &d;

// Base class pointer refers to derived class object


bptr->show(); // This will call the derived class function due to overriding
return 0;
}
Output:
Derived class show function called

In this example, even though bptr is a pointer to the base class, the derived class's show function is
called due to function overriding.

Basic Function Overriding


Problem Statement:
Create two classes: Animal (base class) and Dog (derived class). The Animal class should have a
virtual function speak that prints "The animal speaks". The Dog class should override the speak
function to print "The dog barks".
Objective:
 Demonstrate basic function overriding.
 Use base class pointers to call derived class methods.
Solution:

#include <iostream>
using namespace std;

class Animal {
public:
virtual void speak() {
cout << "The animal speaks" << endl;
}
};

class Dog : public Animal {


public:
void speak() override {
cout << "The dog barks" << endl;
}
};

int main() {
Animal *animalPtr;
Dog dog;

animalPtr = &dog;
animalPtr->speak(); // calls Dog's speak() due to overriding

return 0;
}
Expected Output:
The dog barks

Overriding with Virtual Functions


Problem Statement:
Create a base class Shape with a virtual function area() that returns 0. Create two derived classes:
Rectangle and Circle, each with their own area() implementations that calculate the area of the
respective shapes.
Objective:
 Implement overriding in classes that calculate areas for different shapes.
 Demonstrate polymorphism by calling the appropriate area() method at runtime.
Solution:
#include <iostream>
#include <cmath>
using namespace std;

class Shape {
public:
virtual double area() {
return 0;
}
};

class Rectangle : public Shape {


private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() override {
return width * height;
}
};

class Circle : public Shape {


private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() override {
return M_PI * radius * radius;
}
};

int main() {
Shape *shapePtr;
Rectangle rect(5, 10);
Circle circ(7);

shapePtr = &rect;
cout << "Area of rectangle: " << shapePtr->area() << endl;

shapePtr = &circ;
cout << "Area of circle: " << shapePtr->area() << endl;

return 0;
}
Expected Output:
Area of rectangle: 50
Area of circle: 153.938

You might also like