0% found this document useful (0 votes)
148 views39 pages

C++ Friend Functions and Inheritance Concepts

The document discusses C++ concepts of friend functions and inheritance. It explains how friend functions allow non-member functions to access private data of a class, and outlines various inheritance types such as public, private, and protected inheritance. Additionally, it covers constructors, including default, parameterized, and copy constructors, along with their rules and overloading.

Uploaded by

Jyoti K
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)
148 views39 pages

C++ Friend Functions and Inheritance Concepts

The document discusses C++ concepts of friend functions and inheritance. It explains how friend functions allow non-member functions to access private data of a class, and outlines various inheritance types such as public, private, and protected inheritance. Additionally, it covers constructors, including default, parameterized, and copy constructors, along with their rules and overloading.

Uploaded by

Jyoti K
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

C++ FRIEND FUNCTION &

INHERITANCE

By [Link]
[Link]
RSET
FRIEND FUNCTION

• In encapsulation and data hiding: a non member function has no


permission to access private data of the class.
• The private data members of the class are accessed only from member
function of the same class. Any non-member function cannot access
the private data of the class.
• C++ allows a mechanism in which a non-member function can access
the private data members of the class i.e by declaring a non-member
function friend of the class whose private data has to be accessed.

[Link] RSET
class DCA
{
int roll;
friend void disp(void);
};
void disp(void)
{
----;
----;
}
int main()
{
disp();
}
[Link] RSET
RULES
• Not a member of any class therefore no need for scope resolution operator.
void sum::putdata(void) void disp(void)
• Friend function can be called like normal function, no need of object.(since it is not a
member of any class)
[Link](); disp();
• Definition also like normal function.(no need to mention the name of the class).
• Arguments assed while calling friend function should be the object of the class.

[Link] RSET
Q) Write a program to access private data using non-member function.
Use friend function.

[Link] RSET
class student void main()
{ {
int mark1;
student k;
public :
void get_marks() k.get_marks();
{ show_mark(k); //call to friend f(n)
cout<<“enter two values”; return 0;
cin>>mark1;
}
}
friend void show_mark(student); k
};

void show_mark(student a) student a


{
cout<<“mark of student =“<<a.mark1;
} [Link] RSET
Q) Write a program to declare friend function in two classes. Calculate
the sum of integers of both the classes using friend sum() function.

[Link] RSET
#include<iostream.h> class first void main()
#include<conio.h> { {
class first; int f; clrscr();
class second public : first a;
{ void get_value()
int s; second b;
cout<<“enter a number”;
public : a.get_value();
cin>>f;
void get_value() b.get_value();
}
{ sum(b,a);
friend void sum(second,first);
cout<<“enter a number”; }
};
cin>>s;
void sum(second d,first t)
}
friend void sum(second,first); {
cout<<“sum of numbers”<<t.f+d.s;
};
} [Link] RSET
Q) Write a program to exchange values between two classes using
friend functions. (assignment)

[Link] RSET
Q) Write a program to declare three classes. Declare integer array as
data member in each class. perform addition of two data member
arrays into array of third class. Use friend function.(assignment)

[Link] RSET
INHERITANCE
(Concept)
• Existing class are main components of inheritance
• New classes can be derived from existing class
• The properties of existing class are simply extended to new class
• The new class created using such methods are called as derived class and the
existing classes are known as base classes. The relationship between new class
and existing class are known as kind of relationship.
• The programmer can define new member variables and functions in derived
class but the base class remain unchanged.
• The object of derived class can access members of base class as well as
derived class. On the other hand the base class cannot access members of
derived class. The base classes do not know about their sub classes.
[Link] RSET
Class A

Class B=Derived class

class Name of the derived class : Access specifier Name of the base class
{
-----;
-----;
}
Eg: class B : public A
[Link] RSET
class B : public A
{
//members of class B
};

class B : private A
{
//members of class B
};

class B : protected A
{
//members of class B
};
[Link] RSET
(1)PUBLIC INHERITANCE
When a class is derived publically, all the public members of the base
class can be accessed by directly in the derived class where as in private
derivation, an object of derived class has no permission to access even
the public members of the base class directly.

[Link] RSET
Write a program to derive a class publically from base class. Declare the base
class with its member under public section.

#include<iostream.h> void main()


#include<conio.h> {
class A //base class
{ clrscr();
public : B b;
int X; b.X=20;//access to base class member
};
b.y=30;//access to derived class member
class B : public A
{ cout<<“member of A:”<<b.X;
public : cout<<“member of B :”<<b.y;
int Y; }
};
[Link] RSET
Write a program to derive a class publically from base class. Declare the base
class member under private section.

[Link] RSET
#include<iostream.h> class B : public A //derived class void main()
#include<conio.h> { {
/*public derivation*/
public : clrscr();
class A //base class
int Y; B b;
{
private : B() b.show_x();
int X; { y=30; } }
public : void show()
A() {
{ x=20; } show_x();
void show_x()
cout<<“y=“<<y;
{
cout<<“”x=”<<x; } Output:
} }; X=20
}; continued….
[Link] RSET Y=30
Explanation: Class B is derived publically from base class A. The private
members of the base class can be accessed by using public member
functions of the base class.
The object b invokes the member function show() of the derived class.
The function show() invokes show_x() function of base class.
The object b can access member function defined in both base class
and derived class.
i.e
[Link]() //invoke member function of the derived class.
b.show_x() // invokes member function of base class.

[Link] RSET
(2) PRIVATE INHERITANCE
The object of privately derived class cannot access the public members
of base class under public section.
Q.) Write a program to derive a class privately. Declare the member of
base class under public section.

[Link] RSET
#include<iostream.h> void show() OUTPUT
#include<conio.h>
{ X=20
class A
{ cout<<“X=“<<X; Y=40
public : cout<<“Y=“<<Y;
int X; }
};
};
class B : private A
{ void main()
public : {
int Y; clrscr();
B()
{
B b;
X=20; [Link]();
Y=40; }
} [Link] RSET
Explanation: class B is a derived class from class A. The member
variable X is a public member of base class. The object b of derived
class cannot access variable x directly.
b.X// cannot access
Member function of derived class can access members of base class. i.e
the function show() does the same.

[Link] RSET
Protected inheritance
Why??
The member functions of derived class cannot access the private
member variables of base class. The private members of base class can
be accessed using public member functions of same class. This
approach makes a program lengthy. To overcome the problem
associated with private data another access specifier called protected
was introduced.

[Link] RSET
Write a program to declare protected data in base class. Access data of base class
declared under protected section using member functions of derived class.

[Link] RSET
#include<iostream.h> void show()
#include<conio.h> {
class A cout<<“X=“<<X;
{ cout<<“Y=“<<Y;
protected : }};
int x; void main()
}; {
class B : private A
clrscr();
{
B b;
int y;
[Link]();
public :
}
B()
{ X=30; OUTPUT
Y=40; } X=30
[Link] RSET
Y=40
Types of inheritance
The process of inheritance depends on (1)number of base classes (i.e
program can use one or more base class to derive a single class)
(2)Nested derivation (the derived class can be used as base class and new
class can be derived from it.
Different types of inheritance are:
1. Single
2. Multiple
3. Hierarchical
4. multilevel
5. Hybrid
6. multipath

[Link] RSET
Single inheritance
When only one base class is used for derivation of a class and derived
class is not used as base class, such type of inheritance between one
base and derived class is known as single inheritance.

Multiple inheritance
When two or more base classes is used for derivation of a class, it is
called multiple inheritance.

[Link] RSET
Hierarchical inheritance
When a single base class is used for derivation of two or more classes.

Multilevel inheritance
When a derived class is derived from another derived class i.e, derived
class acts as base class.

[Link] RSET
Multilevel inheritance
When a class is derived from another class i.e derived class acts as base class
such type of inheritance is called multilevel inheritance.

Hybrid inheritance
Combination of one or more type of inheritance is called as hybrid
inheritance.

[Link] RSET
CONSTRUCTORS
Normally we invoke member function using object and also data members are
initialized through objects.
C++ provides a pair of in built special(???) member functions called constructor and
destructor.
Job???

int main() Class A int main()


class A
{ { { {
A a; public : A obj;
public :
int X,Y;
int X,Y; } } obj.X=20;
A()
obj.Y=30;
{
X=20; Note: when we create object for class, }
when the object A a; is initialized,
Y=30;
the constructor is invoked. Definition: A constructor is a special member function whose
} task is to initialize the objects of that class. It is special because
i.e automatically invoked
} its name is the same as the class name
[Link] RSET
RULES FOR CONSTRUCTOR
1) Constructor has the same name as that of the class it belongs.
2) Constructor does not return any value.i.e neither return value nor void
A()
{
X=20;
Y=30;
}
1) In inheritance concept where properties of one class is inherited by
another. Constructor is not inherited.
2) Constructor is executed when an object is declared.
3) Constructor can have default and can be overloaded.
4) The constructor without arguments is called as default constructor.
[Link] RSET
1) Default constructor
If we are not using any arguments in a constructor, then it is called as
default constructor.

class test
{
public :
test() Since no argument is passed
{ this is called as default constructor
----; (this is used to initialise a value)
----;
}
}
[Link] RSET
Simple program to understand default constructor
#include<iostream.h> int main()
#include<conio.h>
class test
{
{ test t; //constructor automatically invoked
int a,b; [Link](); //function call using object
test() //constructor declared
{
getch();
a=0; }
b=0;
void disp(void)
{
cout<<“value of a :”<<a;
/*no need of separate function of initialising the value of
cout<<“value of b :”<<b; variables*/
} OUTPUT
[Link]
RSET of a : 0
value of b :0
2) Parameterized constructor
class test
{
int a,b;
public :
test(int X,int Y) //parameterised constructor,
some argument is passed.
{
a=X;
b=Y; NOTE: now if we want to use constructor but value
} initialisation to be done by programmer, we define another
constructor called as parameterised constructor.
[Link] RSET
Simple program for parameterised constructor
#include<iostream.h> int main();
#include<conio.h> {
class test
test t(100,200); //NOTE: when we create object then
{ itself we have to pass the argument
int a,b;
[Link]();
public :
test(int X,int Y)
getch();
}
100 200
{
a=X;
b=Y;
}
void disp(void) OUTPUT
{
Value of a 100
cout<<“value of a”<<a;
cout<<“value of b”<<b; Value of b 200 X Y
[Link] RSET
}};
3) Copy constructor
• using copy constructor it is possible for a programmer to declare and
initialize one object using reference of another object.
• when ever a constructor is called a copy of an object is created.
• all copy constructor requires one argument, with reference to an
object of that class.

Syntax:
Class_name (const class_name &object) Const y?????
We are copying the object values,
{ no changes are needed.
------;
------;
}

[Link] RSET
Obj 1 X=100,
Y=200

class_name int main()


{ {
int x,y; Same copy
Parameterised constructor
Obj 1(100,200); to be created

{ Obj 2(OBJ 1);


X=20; Obj 2 X=100,
} Y=200
Y=30;
}
copy constructor syntax
{
----;
}

[Link] RSET
#include<iostream.h> void disp() //for displaying
#include<conio.h> {
class test cout<<“code :”<<code;
{ cout<<: price :<<price;
int code, price; }
public : int main()
test(int c,int p)//parameterised constr. {
{ test t1(101,100);
code=c; test t2(t1); //copy constructor called
price=p; /*object t2 is created member values are
initialised through t1 object*/
}
test(const test &t1) //copy constr. cout<<“t1 object”;
{ [Link]();
code=[Link]; cout<<“t2 object”;
price=[Link]; [Link]();
} } RSET
[Link]
Constructor overloading

Constructor overloading definition: a class contains more than one


constructor which are defined with same name as the class but
contains different number of arguments. depending on number of
arguments the compiler executes appropriate constructor.

[Link] RSET
OUTPUT
test(int X) int main() OBJECT A
#include<iostream.h> //[Link] A=0
{
#include<conio.h> { B=0
test A; OBJECT B
Class test a=b=X; A=100
} test B(100); B=100
{ OBJECT C
test(int X,int Y) test(100,200); A=100
int a,b; //[Link] B=200
{ cout<<“object A”;
public :
a=x; [Link]();
test() //(1)default b=y;
cout<<“object B”;
{ }
void disp() [Link]();
a=0;
{ cout<<“value of a”<<a; cout<<“object C”;
b=0;
cout<<“value of b”<<b; [Link]();
} }
}
[Link] RSET

You might also like