C++71-80

download C++71-80

of 5

Transcript of C++71-80

  • 7/30/2019 C++71-80

    1/5

    Q71. Characteristics of Friend function.1. It is not in the scope of the class to which it has been declared as friend.2. Since it is not in the scope of the class, it cannot be called using the object of that

    class.3. It can be invoked like a normal function without the help of an object.4. Unlike member function, it cannot access the member names directly and has to

    use an object name and dot membership operator with each member name.5. It can be declared either in the public or the private of a class without affecting its

    meaning.6. Usually, it has the objects as arguments.

    Q72. Write a C++ program using friend function to add two complex numbers.#include

    #include

    class Cmplx1

    {

    int real,imagin;

    public :

    void get()

    {coutreal;

    coutimagin;

    }

    friend void sum(Cmplx1,Cmplx1);

    };

    void sum(Cmplx1 c1,Cmplx1 c2)

    {

    cout

  • 7/30/2019 C++71-80

    2/5

    Q74. What are friend classes? Explain its uses with an example.

    A friend class and all its member functions have access to all the private membersdefined within other class.

    #include using namespace std;class Numbers{

    int a;int b;public:

    Numbers(int i, int j){

    a = i;b = j;

    }friend class Average;

    };

    class Average{

    public:int average(Numbers x);

    };

    int Average:: average(Numbers x){

    return ((x.a + x.b)/2);}

    int main(){

    Numbers ob(23, 67);Average avg;cout

  • 7/30/2019 C++71-80

    3/5

    need to be declared as friend in both the classes. They need not be members of eitherof these classes.

    Q76(a) Differentiate between Inline and friend function.

    Inline function: To eliminate the cost of calls to small functions, C++ proposes a newfeature called inline function. An inline function is a function that is expanded in linewhen it is invoked. That is, the compiler replaces the function call with the correspondingfunction code. An inline function makes a program a program run faster because theoverhead of a function call and return in eliminated. An inline function must be definedthey are called. The inline functions are defined as

    inline function-header

    {

    Function body

    }

    Friend Function: The private members cannot be accessed from outside the class. Thatis, a non-member function cannot have an access to the private data of a class. However,there could be a situation where we would like two classes to share a particular function.In such situations, C++ allows the common function to be made friendly with both theclasses, thereby allowing the function to have access to the private data of these classes.Such a function need not be a member of any these classes. To make an outside functionfriendly to the class, we have to simply declare this function as a friend of the class asshown below:

    class ABC

    {

    public:

    friend void xyz();

    }

    The friend declaration should be preceded by the keyword friend. A friend function,although not a member function, has full access rights to the private member of theclasses.

    Q77. What is Inheritance?

    Q78. What is Extensibility?

    C++ allows the extension of the functionality of the existing software components. In C++ this is achieved through abstract classes and inheritance.

  • 7/30/2019 C++71-80

    4/5

    Q79. Explain why and when do we use protected instead of private.

    Private data members cannot be accessed outside the class. When a class inherits a baseclass, all the data members except the private get inherited into it. So if we want datamembers to be accessible to only derived classes and not privately or publiclyaccessible, then we can use protected.

    Q80. What is inheritance in C++? What are the various types of inheritance?What is the advantage of inheritance?

    Inheritance is a method by which new classes are created or derived from the existingclasses. Using Inheritance some qualities of the base classes are added to the newlyderived class, apart from its own features The advantage of using "Inheritance" is due tothe reusability of classes in multiple derived classes. The ":" operator is used for inhertinga class.

    When creating a class, instead of writing completely new data members and memberfunctions, the programmer can designate that the new class should inherit the members

    of an existing class. This existing class is called the base class, and the new class isreferred to as the derived class.

    The following table lists the visibility of the base class members in the derived classes.

    Various types of inheritance:

    1. Single inheritance: When a derived class inherits from only one base class, it is

    known as single inheritance.

    2. Multiple inheritance: When a derived class inherits from multiple base classes, it

    is known as multiple inheritance.

    3. Hierarchical inheritance: When many derived classes inherits from a single base

    class, it is known as hierarchical inheritance.

    4. Multilevel inheritance: When a derived class inherits from a class which itself

    inherits from another class, it is known as multilevel inheritance.

    5. Hybrid inheritance: There could be a situation where we need to apply two or

    more types of inheritance to design a program. When a subclass inherits from

    multiple base classes and all of its base classes inherits from a single base class,this formof inheritance is known as hybrid inheritance.

    Base classvisibility

    Derived class visibility

    Publicderivation.

    Privatederivation.

    Protectedderivation.

    Private Not inherited Not inherited Not inherited

    Protected Protected Private Protected

    Public Public Private Protected

  • 7/30/2019 C++71-80

    5/5

    The advantages of inheritance are:

    It permits code reusability.

    Reusability saves time in program development.

    It encourages the reuse of proven and debugged high-quality software, thus

    reducing problem after a system becomes functional.