3.1) An inheritance hierarchy is defined in BaseDerived.h (source file provided)
ID: 655374 • Letter: 3
Question
3.1) An inheritance hierarchy is defined in BaseDerived.h (source file provided) consisting of two classes, Base class and Derived class. Write a testing program main() that instantiates objects ofboth type Base and Derived, using both the default constructors and also the constructors that take arguments (i.e. parameterized constructor). Write code to output the member variables of each of the objects, and compile and run your program. Now change the class member access of the Base class default constructor from public to private. Try re-compiling the program. What happens? Remove from main() the definitions of the objects that cause default constructors to automatically run. Leaving the Base class default constructor with private access, make modifications to the Derived class that will allow the main() function to compile and run.
3.2 A derived class D is defined in ConstructionOrder.h(source file provided) which inherits from two base classes, classes B1 and B2. The class D also contains two objects of type C1 and C2. Write code to implement destructors for each class, each destructor should display the class name, e.g.
Explanation / Answer
#include <iostream>
#include "Animal.h"
using std::endl;
using std::cout;
void Animal::soundThatAnimalMakes( )
{
cout << "No Sound" << std::endl;
};
// Animal.h
// test03
// Created by Sildomar T. Monteiro on 4/9/15.
// Copyright (c) 2015 Sildomar T. Monteiro. All rights reserved.
#ifndef Animal_h
#define Animal_h
class Animal {
public:
virtual void soundThatAnimalMakes() = 0;
};
#endif
// ConstructionOrder.h
// Created by Sildomar T. Monteiro on 4/9/15.
// Copyright (c) 2015 Sildomar T. Monteiro. All rights reserved.
#ifndef ConstructionOrder_h
#define ConstructionOrder_h
using std::cout;
using std::endl;
class B1 {
public:
B1(int b1_) //constructor
: b1(b1_)
{ cout << "B1 constructor" << endl; }
~B1() //constructor
{ cout << "B1 destructor" << endl; }private:
int b1;
};
class B2 {
public:
B2(int b2_) //constructor
: b2(b2_)
{ cout << "B2 constructor" << endl; }
private:
int b2;
};
class C1 {
public:
C1(int c1_) //constructor
:c1( c1_)
{ cout << "C1 constructor" << endl; }
private:
int c1;
};
class C2 {
public:
C2(int c2_) //constructor
: c2(c2_)
{ cout << "C2 constructor" << endl; }
private:
int c2;
};
class D : public B1, public B2 {
public:
D( int c2_, int c1_, int b2_, int b1_ , int d1_ ) //constructor
: c2D(c2_), c1D(c1_), B2(b2_), B1(b1_), d1(d1_)
{ cout << "D constructor" << endl; }
private:
C1 c1D;
C2 c2D;
int d1;
};
#endif
// BaseDerived.h
// Created by Sildomar T. Monteiro on 4/9/15.
// Copyright (c) 2015 Sildomar T. Monteiro. All rights reserved.
#ifndef BaseDerived_h
#define BaseDerived_h
class Base{
public:
Base() : //default constructor
PubNumB(1), PrivNumB(2) { }
Base( int num_a, int num_b ) : // constructor
PubNumB( num_a ), PrivNumB( num_b ) { }
int Get_PrivNumB()
{ return PrivNumB; }
int calc()
{ return PubNumB * PrivNumB; }
int PubNumB;
private:
int PrivNumB;
};
class Derived : public Base{
public:
Derived(): //default constructor
PubNumD(3), PrivNumD(4){ }
Derived( int num_c, int num_d, int num_e, int num_f ) : // constructor
PubNumD(num_c), PrivNumD( num_d), Base(num_e, num_f) { }
int Get_PrivNumD()
{ return PrivNumD; }
int PubNumD;
private:
int PrivNumD;
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.