Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

C++

Dr. Kuppusamy .P
Associate Professor / SCOPE
friend function
• A friend function is a function of the class defined outside the class scope but it
has the right to access all the private and protected members of the class.
• The friend functions appear in the class definition but friends are the member
functions.
• It can be invoked like a normal function without using the object.
• The friend function is declared using friend keyword.
// syntax of friend function
class classname
{
friend data_type function_name(arguments);
};
Example
#include <iostream>
using namespace std;
class Rect {
double width;

public:
friend void printWidth(Rect rect );
void setWidth( double wid );
};

// Member function definition


void Rect::setWidth( double wid ) {
width = wid;
}

Dr. Kuppusamy P
Example
void printWidth(Rect rect ) {
//friend function can directly access any member of this class
cout << "Width of Rectangle : " << rect.width <<endl;
}

// Main function
int main() {
Rect rect;
// set width without member function
rect.setWidth(10.0);

// Use friend function to print


printWidth( rect );
return 0;
}
Dr. Kuppusamy P
friend class
• A friend class can access private and protected members of other class in which it
is declared as friend.
• For example, a LinkedList class may be allowed to access private members of
Node.

#include <iostream>

using namespace std;

class A
{
int x =5;
friend class B; // friend class.
};
friend class
• class B
{
public:
void display(A &a)
{
cout<<"value of x is : "<<a.x;
}
};
int main()
{
• In example, class B is declared as a friend inside
A a; the class A.
B b; • Therefore, B is a friend of class A.
b.display(a); • Class B can access the private members of class A.
return 0;
}
Inheritance in OOP
• Inheritance is the mechanism in which one class inherits the attributes (variables)
and methods of another class i.e., inheritance defines relationship among classes.
• The class whose properties and methods are inherited is known as the parent class
or superclass or base class.
• The class derives (inherits) the properties and methods from the parent class is the
child class or derived class or sub class.
• The child class can add its own fields and methods in addition to the parent class
fields and methods.
Syntax :
class subclass_name : access_mode base_class_name
{
//body of subclass
};

Dr. Kuppusamy P
Types of inheritance

Dr. Kuppusamy P
Single inheritance
class Parent //main function
{ int main()
public: {
int id_p;
}; Child obj1;
obj1.id_c = 7;
// Sub class obj1.id_p = 91;
class Child : public Parent cout << "Child id is " << obj1.id_c;
{ cout << "Parent id is " << obj1.id_p;
public:
int id_c; return 0;
}; }
Multi-level inheritance
• a derived class is created from another derived class.

// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
Multi-level inheritance
// first sub_class derived from class vehicle
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are vehicles"<<endl;
}
};

// another sub class derived from the derived class fourWheeler


class Car: public fourWheeler{
public:
Car()
{
cout<<"Car has 4 Wheels"<<endl;
}
};
Multi-level inheritance
// main function
int main()
{
Car obj;
return 0;
}
Multiple inheritance
• one sub class is inherited from more than one base classes

// first base class


class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};
Multiple inheritance
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler {

};

// main function
int main()
{
Car obj;
return 0;
}
Hierarchical inheritance
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// first sub class


class Car: public Vehicle
{

};
Hierarchical inheritance
// second sub class
class Bus: public Vehicle
{

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
Hybrid inheritance
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

//base class
class Fare
{
public:
Fare()
{
cout<<"Fare of Vehicle\n";
}
};
Hybrid inheritance
// first sub class
class Car: public Vehicle
{

};

// second sub class


class Bus: public Vehicle, public Fare
{

};
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Bus obj2;
return 0;
}

You might also like