Question: Using C++ A.cpp: #include A.h #include using namespace std; A::A() { n = unnamed A; cout /////////////////////////////////////////////////////////////// A.h: #ifndef A_H #define A_H #include using namespace
Using C++

A.cpp:
#include "A.h" #includeusing namespace std; A::A() { n = "unnamed A"; cout ///////////////////////////////////////////////////////////////
A.h:
#ifndef A_H #define A_H #includeusing namespace std; class A { public: A(); A(string name); ~A(); void setN(const char* s); string getN() const; private: string n; }; #endif ///////////////////////////////////////////////////////////////
B.cpp
#include "B.h" #includeusing namespace std; B::B() { setN("unnamed B"); cout ////////////////////////////////////////////////////////////////////////
B.h
#ifndef B_H #define B_H #include "A.h" class B : public A { public: B(); B(string name); ~B(); private: }; #endif////////////////////////////////////////////////////////
C.cpp
#include "C.h" #includeusing namespace std; C::C() { setN("unnamed C"); cout /////////////////////////////////////////////////////////////////////////
C.h
#ifndef C_H #define C_H #include "B.h" class C : public B { public: C(); C(string name); ~C(); private: }; #endif//////////////////////////////////////////////////////////////////////
InheritanceTest.cpp
#include "A.h" #include "B.h" #include "C.h" #includeusing namespace std; int main (int argc, char * const argv[]) { // We put everything inside a nested block so that we can put a // breakpoint on the return in Visual Studio and still see all // of the destructor. { cout Using C++
Exercise 1: The first part of today's lab is to learn Class in C++ and investigate how inheritance affects which methods get called and in what order constructors and destructors are called. Please look at the 1.Inheritance folder in your download, compile and run files A.cpp D, A.h D, B.cpp B, B.h D, C.cpp B, C.h and InheritanceTest.cpp D. Question 1. What do you observe? 2. How are the constructors and destructors called? in which order? Take away: construction is done from the super class (A first) to derived classes (then B, and C later). Destruction occurred in the reversed order, from derived class (C first) and then the super classes (then B, then A). Coding (finish in 20min) 1. Add an "int" type private data member named "testData1" in A, and update A's "parameterized constructor" to initialize this newly added data member using "initialization list." You don't need to update other constructors. You may complete other constructors in Step 4. o Take away: how to initialize an instance variable in the initialization list. 2. Add another "int" type private data member named "testData2" in A, and update A's "default constructor" to initialize this newly added data member without using the initialization list. You don't need to update other constructors. You may complete other constructors in Step 4. . Take away: alternate ways to initialize, all of the ways are valid. 3. Now you should have two data members added to A, can you tell the difference between using the "initialization list" or not? o Take away: alternate ways to initialize, all of the ways are valid. 4. Please complete both "default constructor" and "parameterized constructor" to properly initialize all data members in A. o Take away: All ways are valid, but you need to make sure to initialize_ALL_instance variables. Notice the ones created on the stack (local) has garbage in them! 5. Add an "int" type private data member named "testDataB" in class B and a "double" type private data member named "testDataC" in class C. Update constructors of B and C to make sure that the constructors in B and C (which should initialize their new members to some reasonable values) use the correct superclass constructors to initialize every data member properly
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
