0% found this document useful (0 votes)
10 views4 pages

C++ String Operations Assignment

The document discusses string operations in C++ and describes using friend functions to access private members of a class from outside the class; it provides an example of a friend function that accesses private data members of two classes and outputs their values, and presents questions about friend functions and programming tasks involving creating string and database classes in C++.

Uploaded by

Amol Ubale
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

C++ String Operations Assignment

The document discusses string operations in C++ and describes using friend functions to access private members of a class from outside the class; it provides an example of a friend function that accesses private data members of two classes and outputs their values, and presents questions about friend functions and programming tasks involving creating string and database classes in C++.

Uploaded by

Amol Ubale
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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.

Common questions

Powered by AI

The 'this' pointer in C++ is an implicit pointer available in member functions, pointing to the object for which the function is called. It enhances functionality by allowing developers to resolve naming conflicts between class attributes and parameters, support method chaining by returning the object itself, and differentiate between class members and arguments with the same names. This is crucial for designing fluent APIs and ensuring clear, unambiguous code .

Constructors in C++ initialize objects when they are created, which is crucial for setting up dynamically allocated memory. Destructors are responsible for cleaning up and releasing resources when an object’s lifetime ends. Dynamic memory allocation operators like 'new' and 'delete' complement this process by providing control over memory allocation and deallocation. 'New' allocates memory for an object at runtime, while 'delete' releases that memory, preventing memory leaks. This setup allows programs to handle memory efficiently based on needs .

A friend function in C++ allows access to private or protected data within a class by being declared as a 'friend'. This means that even though it's not a member of the class, it can access its private and protected members, which is usually not possible for non-member functions. This can be beneficial when two or more classes need to work closely together, allowing them to access and manipulate each other's private data without making that data public. This enhances encapsulation while maintaining necessary access for specific functions .

Inline functions in C++ are expanded at the point of invocation, potentially improving performance by eliminating the overhead of function calls. This can be particularly beneficial for small, frequently used functions. However, inlining can lead to code bloat if overused, especially for larger functions or those called numerous times, as it involves duplicating code at each call site. Additionally, inlining decisions are hints to the compiler, which may ignore them, and it can also increase binary size, complicating cache usage and possibly reducing performance on systems where cache hits are critical .

Static member functions and data members in a class can maintain a count of instances by using a static variable to increment each time an instance is created via any constructor. This counts all instances across different uses of the class without needing storage in each instance. However, pitfalls include the shared state problem where all instances share this static count, threading issues in concurrent environments, and a lack of encapsulation since static functions don't operate on instance data directly .

Forward declaration is required in C++ to inform the compiler about the existence of a class before it is defined. In the context of a friend function, forward declaration is useful because it ensures that when a class is declared, the compiler knows that the other class exists and that there might be functions accessing its members. For example, in classes A and B where the show function is a friend, class B is forward declared before class A to ensure that the friend function show, which requires both classes, compiles without errors .

Dynamic memory allocation in C++ is performed at runtime using 'new' and 'delete', allowing flexible usage of memory based on current needs, while static memory allocation occurs at compile time. Dynamic allocation offers the advantage of efficiently managing memory for varying program requirements, potentially improving performance in large applications. However, it requires careful management to prevent leaks and inefficient memory use. On the other hand, static memory allocation is simpler but can lead to wasted memory if allocation exceeds needs or insufficient memory if it falls short .

Friend functions provide the advantage of accessing private and protected data from outside the class, fostering close cooperation between classes. This might be advantageous when functionality requires external access to data while avoiding exposing it publicly. However, the downside is that it breaks encapsulation principles since it allows access to the internals of a class from outside, potentially leading to security risks if misused. Ensuring that only trusted functions or classes gain access is necessary to mitigate these risks .

A 'friend class' in C++ can access all private and protected members of another class in which it is declared as a friend, facilitating close cooperation between classes. This is typically used when classes are interdependent and need to share data without exposing it publicly, thus maintaining encapsulation. An example might be classes representing different parts of a complex data structure where one needs direct access to the internal state of another to function efficiently, such as a graphical object and its corresponding rendering class .

Writing a custom string handling function might be preferred in C++ to tailor behavior specific to program requirements or to better understand string manipulations at a fundamental level. This allows for optimization specific to use cases not covered by standard library functions. Challenges include managing memory efficiently, handling different string lengths and encodings, and ensuring that operations such as concatenation and comparison are accurate and performant. This requires significant understanding of memory management and string operations in C++ .

You might also like