1. Create two classes, A and B, with default constructors that announce themselv
ID: 3684759 • Letter: 1
Question
1. Create two classes, A and B, with default constructors that announce themselves. lnherit a new class called C from A, and create a member object of B in C, but do not create a constructor for C. In main(), create an object of class C and explain the results.
2. Create a three-level hierarchy of classes with default constructors, along with destructors, both of which announce themselves to cout. Verify that for an object of the most derived type, all three constructors and destructors are automatically called. Explain the order in which the calls are made.
3. As part of a military video game, a designer has created a vehicle class hierarchy. The base class is Vehicle, and it has AirVehicle, LandVehicle, and SeaVehicle as derived classes. The class SeaPlane inherits from both AirVehicle and SeaVehicle. Specifically, what design issues had to be considered in developing the Seaplane class?
Explanation / Answer
1)
class A
{
A()
{
System.out.println("A");
}
}
class B
{
B()
{
System.out.println("B");
}
}
class C extends A
{
B b = new B();
}
public class AB
{
public static void main(String[] args)
{
C c = new C();
}
}
2)
#include <iostream>
using namespace std;
#define CLASS(ID) class ID
{
public:
ID ()
{
cout << #ID” constructor! ”;
}
~ID ()
{
cout << “~” << #ID” destructor! ”;
}
}
CLASS(A);
class B:public A
{
public:
B ()
{
cout << “B() constructor!” << endl;
}
~B()
{
cout << “~B() destructor!” << endl;
}
};
class C:public B
{
public:
C ()
{
cout << “C() constructor!” << endl;
}
~C ()
{
cout << “~C () destructor!” << endl;
}
};
int main()
{
C c;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.