Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help ASAP, code in C++ a.cpp #include A.h #include using namespace std; A::A() { n = unnamed A; cout A::A(string name) : n(name)

I need help ASAP, code in C++

image text in transcribed

a.cpp

#include "A.h" #include

using namespace std;

A::A() { n = "unnamed A"; cout

A::A(string name) : n(name) { cout

A::~A() { cout

void A::setN(const char* s) { n = s; }

string A::getN(void) const { return n; }

a.h

#ifndef A_H #define A_H

#include using 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" #include

using namespace std;

B::B() { setN("unnamed B"); cout

B::B(string name) : A(name) { cout

B::~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" #include

using namespace std;

C::C() { setN("unnamed C"); cout

C::C(string name) : B(name) { cout

C::~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" #include using 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

cout

Question 1. What do you observe? 2. How are the constructors and destructors called? in which order? o 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 "testData" 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. 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions