Inheritance
- What are the things are inherited from the base class?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
These things can provide necessary information for the base class to make a logical decision.
- Which design patterns benefit from the multiple inheritances?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Adapter and observer pattern
- What is the output of this program?
#include <iostream>
using namespace std;
class BaseClassA
{
protected:
int FirstData;
public:
BaseClassA()
{
FirstData = 150;
}
~BaseClassA()
{
}
int FirstFunction()
{
return FirstData;
}
};
class BaseClassB
{
protected:
int SecondData;
public:
BaseClassB()
{
SecondData = 250;
}
~BaseClassB()
{
}
int SecondFunction()
{
return SecondData;
}
};
class DerivedClass : public BaseClassA, public BaseClassB
{
int ThirdData;
public:
DerivedClass()
{
ThirdData = 350;
}
~DerivedClass()
{
}
int Function()
{
return (ThirdData + FirstData + SecondData);
}
};
int main()
{
BaseClassA FirstObject;
BaseClassB SecondObject;
DerivedClass ThirdObject;
cout << ThirdObject.BaseClassA :: FirstFunction() << endl;
cout << ThirdObject.BaseClassB :: SecondFunction() << endl;
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
In this program, We are passing the values by using multiple inheritance and printing the derived values.
- What is the output of this program?
#include <iostream>
using namespace std;
struct Student1
{
int num;
};
struct Student2
{
int* val;
};
struct Student3 : public Student1, public Student2
{
};
int main()
{
Student3* ptr = new Student3;
ptr->val = 0;
cout << "Above structures are Inherited...";
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We apply the multiple inheritance to structure.
- What is the output of this program?
#include
using namespace std;
class StudentClass
{
public:
int RollNo , mark1 , mark2 ;
void get()
{
RollNo = 25, mark1 = 15, mark2 = 40;
}
};
class sportsClass
{
public:
int sortMarks;
void getsm()
{
sortMarks = 20;
}
};
class statementClass:public StudentClass,public sportsClass
{
int Total,average;
public:
void display()
{
Total = (mark1 + mark2 + sortMarks);
average = Total / 3;
cout << Total << " ";
cout << average;
}
};
int main()
{
statementClass object;
object.get();
object.getsm();
object.display();
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We are calculating the total and average marks of a student by using multiple inheritance.