Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this assignment ASAP. Code in C++ Exercise1: The first part of today's lab is to learn Class in C++ and investigate

I need help with this assignment ASAP. Code in C++

Exercise1: 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.cppPreview the document, A.hPreview the document, B.cppPreview the document, B.hPreview the document, C.cppPreview the document, C.hPreview the document and InheritanceTest.cppPreview the document.

Question

What do you observe? 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)

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. Take away: how to initialize an instance variable in the initialization list. 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. Now you should have two data members added to A, can you tell the difference between using the "initialization list" or not? Take away: alternate ways to initialize, all of the ways are valid. 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! 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.

a.cpp

#include "A.h" #include

using namespace std;

A::A() { n = "unnamed A"; cout << "Inside A::A() for object " << n << endl; }

A::A(string name) : n(name) { cout << "Inside A::A(string) for object " << n << endl; }

A::~A() { cout << "Inside A::~A() for object " << n << endl; }

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 << "Inside B::B() for object " << getN() << endl; }

B::B(string name) : A(name) { cout << "Inside B::B(string) for object " << getN() << endl; }

B::~B() { cout << "Inside B::~B() for object " << getN() << endl; }

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 << "Inside C::C() for object " << getN() << endl; }

C::C(string name) : B(name) { cout << "Inside C::C(string) for object " << getN() << endl; }

C::~C() { cout << "Inside C::~C() for object " << getN() << endl; }

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 << "Creating an unnamed A." << endl; A a1; cout < B b1; cout < C c1; cout << endl << "Creating a named A." << endl; A a2("a2"); cout << endl << "Creating a named B." << endl; B b2("b2"); cout << endl << "Creating a named C." << endl; C c2("c2");

cout << endl; } return 0; }

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 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 1 Lncs 8055

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013 Edition

3642402844, 978-3642402845

Students also viewed these Databases questions

Question

Define marketing concepts.

Answered: 1 week ago

Question

1 what does yellow colour on the map represent?

Answered: 1 week ago

Question

9. Mohawk Industries Inc.

Answered: 1 week ago

Question

8. Satyam Computer Services Limited

Answered: 1 week ago