Select an option to see the answer and solution.
Inheritance in C++
practice.
Practice every MCQ with options. Use Show answers when you want the correct option and solution.
80
Questions
3/4
Page
Pick an option on a question to see the right answer and solution.
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public:
Mammal(){
cout<<"I'm a Mammal\n";
}
~Mammal(){}
};
class Human: public Mammal
{
public:
Human(){
cout<<"I'm a Human\n";
}
~Human(){}
};
class Male: public Human
{
public:
Male(){
cout<<"I'm a Male\n";
}
~Male(){}
};
class Female: public Human
{
public:
Female(){
cout<<"I'm a Female\n";
}
~Female(){}
};
int main(int argc, char const *argv[])
{
Male M;
return 0;
}Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A
{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
A *a;
a->func();
return 0;
}Select an option to see the answer and solution.
Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
A(){
a = 0;
}
void show(){
a++;
cout<<"a: "<<a<<endl;
}
};
class B: public A
{
public:
};
int main(int argc, char const *argv[])
{
B b;
b.show();
return 0;
}Select an option to see the answer and solution.
Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
A(){
a = 0;
}
static void show(){
a++;
cout<<a;
}
};
class B: public A
{
public:
};
int main(int argc, char const *argv[])
{
B b;
b.show();
return 0;
}Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
virtual A(){
cout<<"A's Constructor\n";
}
};
class B: public A
{
public:
A(){
cout<<"Present inside the class B\n";
}
};
int main(int argc, char const *argv[])
{
A a;
return 0;
}Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
A(){
a = 0;
}
void show(){
a++;
cout<<"a: "<<a<<endl;
}
};
class B: private A
{
public:
void show(){
show();
}
};
int main(int argc, char const *argv[])
{
B b;
b.show();
return 0;
}Select an option to see the answer and solution.
#include <iostream>
using namespace std;
class Base
{
public:
virtual void print() const = 0;
};
class DerivedOne : public Base
{
public:
void print() const
{
cout << "DerivedOne\n";
}
};
class DerivedTwo : public Base
{
public:
void print() const
{
cout << "DerivedTwo\n";
}
};
class Multiple : public DerivedOne, public DerivedTwo
{
public:
void print() const
{
DerivedTwo :: print();
}
};
int main()
{
int i;
Multiple both;
DerivedOne one;
DerivedTwo two;
Base *array[ 3 ];
array[ 0 ] = &both;
array[ 1 ] = &one;
array[ 2 ] = &two;
array[ i ] -> print();
return 0;
}Select an option to see the answer and solution.
Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
virtual static void show(){
cout<<"class A\n";
}
};
class B: public A
{
public:
static void show(){
cout<<"class B\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A();
B b;
a = &b;
a->show();
return 0;
}Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
A(){
cout<<"Constructor of class A\n";
}
};
class B: public A
{
int a = 15;
public:
B(){
cout<<"Constructor of class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
return 0;
}Select an option to see the answer and solution.
#include <iostream>
using namespace std;
class student
{
public:
int rno , m1 , m2 ;
void get()
{
rno = 15, m1 = 10, m2 = 10;
}
};
class sports
{
public:
int sm;
void getsm()
{
sm = 10;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot = (m1 + m2 + sm);
avg = tot / 3;
cout << tot;
cout << avg;
}
};
int main()
{
statement obj;
obj.get();
obj.getsm();
obj.display();
}Select an option to see the answer and solution.
Select an option to see the answer and solution.
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public:
virtual void Define(){
cout<<"I'm a Mammal\n";
}
};
class Human: public Mammal
{
public:
void Define(){
cout<<"I'm a Human\n";
}
};
class Male: public Human
{
public:
void Define(){
cout<<"I'm a Male\n";
}
};
class Female: public Human
{
public:
void Define(){
cout<<"I'm a Female\n";
}
};
int main(int argc, char const *argv[])
{
Mammal *M = new Mammal();
Male m;
Female f;
*M = m;
M->Define();
M = &m;
M->Define();
return 0;
}Select an option to see the answer and solution.
#include <iostream>
using namespace std;
struct a
{
int count;
};
struct b
{
int* value;
};
struct c : public a, public b
{
};
int main()
{
c* p = new c;
p->value = 0;
cout << "Inherited";
return 0;
}Select an option to see the answer and solution.
#include <iostream>
using namespace std;
class Base1
{
protected:
int SampleDataOne;
public:
Base1()
{
SampleDataOne = 100;
}
~Base1()
{
}
int SampleFunctOne()
{
return SampleDataOne;
}
};
class Base2
{
protected:
int SampleDataTwo;
public:
Base2()
{
SampleDataTwo = 200;
}
~Base2()
{
}
int SampleFunctTwo()
{
return SampleDataTwo;
}
};
class Derived1 : public Base1, public Base2
{
int MyData;
public:
Derived1()
{
MyData = 300;
}
~Derived1()
{
}
int MyFunct()
{
return (MyData + SampleDataOne + SampleDataTwo);
}
};
int main()
{
Base1 SampleObjOne;
Base2 SampleObjTwo;
Derived1 SampleObjThree;
cout << SampleObjThree.Base1 :: SampleFunctOne() << endl;
cout << SampleObjThree.Base2 :: SampleFunctTwo() << endl;
return 0;
}Select an option to see the answer and solution.
Select an option to see the answer and solution.
Select an option to see the answer and solution.