Sie sind auf Seite 1von 4

MCA Department, Sinhgad Institute Of

Technology,Lonavala.

Assignment Number : 04

Assignment Title : String Operations

Date :

Remark :
MCA Department, Sinhgad Institute Of
Technology,Lonavala.

Assignment No : 04

Title : Write C++ program for String operations.

Aim : To understand use of Friend function in C++.

Theory :
Friend Function:
A friend function is used in object-oriented programming to allow access to
private or protected data in a class from outside the class. Normally a function which
is not a member of a class cannot access such information; neither can an external
class. Occasionally such access will be advantageous for the programmer; under these
circumstances, the function or external class can be declared as a friend of the class
using the keyword "friend." The function or external class will then have access to all
information – public, private or protected – within the class.

For Example:
#include <iostream>
using namespace std;

class B;//Forward declaration of class B in order for example to


compile
class A
{
private:
int a;
public:
A() { a=0; }
friend void show(A& x, B& y);
};

class B
{
private:
int b;
public:
B() { b=6; }
friend void show(A& x, B& y);
};

void show(A& x, B& y)


{
cout << "A::a=" << x.a << endl;
cout << "B::b=" << y.b << endl;
}

int main()
{
A a;
B b;
show(a,b);
}

Output: - A::a=0
B::b=6
MCA Department, Sinhgad Institute Of
Technology,Lonavala.

Program:-

Design and Implement Class ‘String’ with a default, parameterized and copy
Constructors. Provide member functions to accept and display string and friend
function to concatenate and compare two strings without using operator overloading.

class String
{
char *str;
int length;
public:
//constructors
//member function declarations
//friend function declaration
};
//Definations of functions
//Defination of Friend function
//main fuction

Questions:-
1. What is Friend function?
2. Can we access private elements of class in which we have declared it as
friend?
3. List the functions of string.h file and explain it in short.
4. How we allocate and release memory in C++?
5. Write code for memory allocation of 2D array?
6. Do we require friend word when we write definition of the friend function?
7. Can we make member function of another class as a friend function?
8. Where do we declare friend function in private part or public part?
9. Why forward declaration of class is required?
10. Is friend function is member of class?

Design Problem:

1. Write a menu driven program to perform all the operations on String without
using library functions.
2. Develop a program in C++ to prepare the mark sheet of an University
examination assuming that the following items can be read from the keyboard:
Name of the student
Roll number
Subject code
Subject name
Internal marks
External marks
Construct the data base with suitable member functions for initializing and
destroying the data viz constructor, default constructor, copy constructor,
destructor, static member functions, friend class, this pointer, inline code and
dynamic memory allocation operators –new and delete.
3. Develop a program in C++ to create database of the following items:
MCA Department, Sinhgad Institute Of
Technology,Lonavala.
Age
Ward number
Bed number
Nature of Illness
Date Of admission
Construct the data base with suitable member functions for initializing and
destroying the data viz constructor, default constructor, copy constructor,
destructor, static member functions, friend class, this pointer, inline code and
dynamic memory allocation operators –new and delete.

Das könnte Ihnen auch gefallen