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?
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
Comments
Leave a comment