Classes & Objects
- How many types of class are there in c++?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
There are three types of classes. They are abstract base classes, concrete derived classes, standalone classes.
- What is the output of this program?
#include <iostream>
using namespace std;
class Base
{
int K;
public:
void setInt(int num);
int getInt();
};
class Derived : public Base
{
int L;
public:
void setL(int num);
int multiply();
};
void Base::setInt(int num)
{
K = num;
}
int Base::getInt()
{
return K;
}
void Derived::setL(int num)
{
L = num;
}
int Derived::multiply()
{
return L * getInt();
}
int main()
{
Derived object;
object.setInt(12);
object.setL(3);
cout << object.multiply();
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this program, We are multiplying the value 12 and 3 by using inheritance.
- Pick out the correct statement about multiple inheritances.
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In multiple inheritances, We are able to derive a class from more than one base class.
- What is the output of this program?
#include <iostream>
using namespace std;
class BaseClass
{
int A;
public:
void setA(int num1)
{
A = num1;
}
void showA()
{
cout << A <<" ";
}
};
class DerivedClass : private BaseClass
{
int B;
public:
void setAB(int num1, int num2)
{
setA(num1);
B = num2;
}
void showAB()
{
showA();
cout << B << '\n';
}
};
int main()
{
DerivedClass object;
object.setAB(15, 25);
object.showAB();
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
In this program, We are passing the values from the main class and printing it on the inherited classes.
- What is the output of this program?
#include <iostream>
using namespace std;
class BaseClass
{
public:
virtual void Function()
{
cout << "4";
}
};
class DerivedClassA : public BaseClass
{
public:
void Function()
{
cout << " 5";
}
};
class DerivedClassB : public DerivedClassA
{
public:
void Function()
{
cout << " 6";
}
};
int main()
{
BaseClass *ptr;
BaseClass BaseObject;
DerivedClassA derivedObjectA;
DerivedClassB derivedObjectB;
ptr = &BaseObject;
ptr -> Function();
ptr = &derivedObjectA;
ptr -> Function();
ptr = &derivedObjectB;
ptr -> Function();
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
We are passing the objects and executing them in a certain order and we are printing the program flow.