Question

In: Computer Science

Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...

Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called.

Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it!

// A wrong solution to this activity...
PoundDog::PoundDog() {
   age = 1;
   id  = -1;
   name.SetString("NoName");
}

#include <iostream>
#include <string>
using namespace std;

class MyString {
public:
MyString();
MyString(string s);
string GetString() const { return str; };
void SetString(string s) { str = s; };
private:
string str;
};

MyString::MyString() {
cout << "MyString default constructor called" << endl;
str = "";
}

MyString::MyString(string s): str(s) {
}


class PoundDog {
public:
PoundDog();
void Print() const;

private:
int age;
int id;
MyString name;
};

/* Your solution goes here */

void PoundDog::Print() const {
cout << "age: " << age << endl;
cout << "id: " << id << endl;
cout << "name: " << name.GetString() << endl;
}

int main() {
PoundDog currDog;
currDog.Print();
return 0;
}

Solutions

Expert Solution

> By Using the constructor initializer list, we can add the default constructor without even getting execute the MyString's default constructor. See the below code:

PROGRAM:

#include <iostream>
#include <string>
using namespace std;

class MyString {
public:
MyString();
MyString(string s);
string GetString() const { return str; };
void SetString(string s) { str = s; };
private:
string str;
};

MyString::MyString() {
cout << "MyString default constructor called" << endl;
str = "";
}

MyString::MyString(string s): str(s) {
}


class PoundDog {
public:
PoundDog();
void Print() const;

private:
int age;
int id;
MyString name;
};

PoundDog::PoundDog(): id(-1),age(1),name("NoName"){
}

void PoundDog::Print() const {
cout << "age: " << age << endl;
cout << "id: " << id << endl;
cout << "name: " << name.GetString() << endl;
}

int main() {
PoundDog currDog;
currDog.Print();
return 0;
}

> In the above code I have used a initializer list:

PoundDog::PoundDog(): id(-1),age(1),name("NoName"){
}

This list executes as the default constructor without calling the MyString's default constructor. It doesn't called MyString's default constructor as I didn't used the method SetString(String s) of the MyString Class.


Related Solutions

C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be used         - how to instantiate and initialize data members that are user-defined types at the same time
Write a C++program that initializes an array called resistances with the following initializer list: from file...
Write a C++program that initializes an array called resistances with the following initializer list: from file called "resistances. txt" {12.3, 98.5, 15.15, 135, 125} Using a while-loop, compute the sum of the elements of the resistances array and then obtain the average resistance and d isplay it. Then, use another loop to display the resistances that has value smaller than the average resistance that
inserting a node after given node. DLL::DLL(int x){ // constructor, initializes a list with one new...
inserting a node after given node. DLL::DLL(int x){ // constructor, initializes a list with one new node with data x    DNode *n = new DNode (x);    first = n;    last = n;    size=1; } DNode::DNode( int x){    data = x;    next = NULL;    prev = NULL; } void DLL::addFirst(int x) {    DNode* tmp = new DNode(x);    first = tmp;    last = first; } void DLL::insertAt(int ind, int x) {   ...
1) What is the member initializer list used for? Give a code example of using one...
1) What is the member initializer list used for? Give a code example of using one 2) Give two examples of when the copy constructor is called by the “compiler”. 3) What actions should be done in the destructor function? Give an example function header for the destructor for the Jedi class. When is the destructor called? Thank you!
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. ----------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT