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
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...
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
Create initializer/constructor functions for book. Declare and implement functions like insert book(insert at End, InsertAtFirst), retrieve...
Create initializer/constructor functions for book. Declare and implement functions like insert book(insert at End, InsertAtFirst), retrieve book, update book information book using ISBN number in Library. Declare another function named Search to find a specific book from catalogue. #include<iostream.h> #include<conio.h> struct book{ char name[50]; int ISBN; char authorname[50]; char publishername[50]; int issuedate; int issuemonth; int issueyear; int retdate; int retmonth; int retyear; struct book *next=NULL; } void insert(struct book *head){ if *head.next==NULL{ struct book node; cout<<"enter the name of book";...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
For each of the following write the line(s) of code that: Declares and initializes (creates) an...
For each of the following write the line(s) of code that: Declares and initializes (creates) an ArrayList that holds String objects Adds to your ArrayList the words "Too" and "Fun" Verifies that your ArrayList now contains 2 elements Sets the second String in the ArrayList to "No" Verifies that your ArrayList still contains exactly 2 elements Prints the contents of the ArrayList to the screen in the following format: <element>, <element>, . . . , <element>
programming in python Design a program that initializes a list of 5 items to zero (use...
programming in python Design a program that initializes a list of 5 items to zero (use repetition operator). It then updates that list with a series of 5 random numbers. The program should find and display the following data: -The lowest number in the list - Average of the numbers stored in the list
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list operations int getLength() const {return length;} void insertNode(College); bool deleteNode(string); void displayList() const; bool searchList(string, College &) const; }; main.cpp /*   Build and procees a sorted linked list of College objects. The list is sorted in ascending order by the college code. Assume that the college code is unique. */ #include <iostream> #include <fstream> #include <string> #include "LinkedList.h" using namespace std; void buildList(const string...
Python: High school assignment, please keep simple In python: Use the following initializer list to create...
Python: High school assignment, please keep simple In python: Use the following initializer list to create an array: twainQuotes = ["I have never let my schooling interfere with my education.", "Get your facts first, and then you can distort them as much as you please.", "If you tell the truth, you don't have to remember anything.", "The secret of getting ahead is getting started.", "Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT