Answer to Question #242575 in C++ for c++

Question #242575

Explain the term polymorphism

Distinguish between Compile-time polymorphism and run-time polymorphism

Explain how polymorphism is achieved through C++code

What are friend functions and classes

What is inheritance? Using a C++ code, illustrate how inheritance is implemented?

What is an access specifier? What are the various Access Specifiers in C++?

What do you mean by 'void' return type?


1
Expert's answer
2021-10-17T09:29:40-0400

Polymorphism is the ability of an object to assume different forms in different scenarios

In compile-time polymorphism, a function is called at the time of compilation of the program

In run-time polymorphism, a function is called during program execution

#include <iostream>
#include <string>
using namespace std;
class Addition {
public:
    int ADD(int x,int y)
    {
        return x+y;     
    }
    int ADD(string x, string y) {           
        return x + y;    
    }
};
int main() {
    Addition obj;  
    cout<<obj.ADD(128, 15)<<endl;
    cout<<obj.ADD("Hello", "User")<<endl;
    return 0;
}

A friend function is a function defined outside of the scope of a class but can access all private and protected members of the class.

When a class is declared a friend class, all member functions of the friend class become friend functions.

#include <iostream>
#include <string>
using namespace std;
class Grandfather{
    protected:
    string surname;
    public:
    Grandfather(string s):surname(s){}
};
class Father:public Grandfather{
    string name;
    public:
    Father(string a, string b): Grandfather(b), name{a}{}
    void details(){
        cout<<name<<" "<<surname;
    }
};
int main(){
    Father father("Lucas", "Graham");
    father.details();
    return 0;
}

Access specifiers define how class members can be accessed. There are three access specifiers in c++. public, private, and protected.

void return type means that a function does not return a value


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS