Question

For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

  1. For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string);

  2. In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code:

cout << ll.get_name() << endl;
ll.make_test_list();
ll.set_name("My List");
cout << ll.get_name() << endl;

Output should be:

Test List
My List

  1. Compile and run your code; the output should print 'Test List' and then (on another line) 'My List'.

  2. We will eventually construct a function that outputs the node values in your linked list; however, first we should overload the operator<< for your Node struct (this way makes things down the road 'easier').

  3. Add the following declaration directly after your Node struct declaration (i.e., outside of it):

    std::ostream& operator<<(std::ostream& os, const Node& n);

    and then provide the following definition in your Node.cpp file:

std::ostream& operator<<(std::ostream& os, const Node& n) {
os << n.value;
return os;
}

Note: You may need to #include<iostream> in Node.cpp.

  1. In the public access specifier in your LinkedList header file, add

    void print(char, std::ostream& os = std::cout);

  2. If this was an exam, could you explain the different parts comprising the declaration provided above? If not, take a moment to think about it; things will be clearer once you look at the definition (below).

  3. Then, add the following definition for print to your LinkedList cpp file:

void LinkedList::print(char c, ostream& os) {
Node* cur = head;
while (cur != nullptr) {
os << *cur;
if (cur != tail)
os << c;
if (cur == tail)
os << endl;
cur = cur->next;
}
}

  1. Let's test our print function now by adding the following lines to Main.cpp

cout << endl << "ll.print('\\t');" << endl;
ll.print('\t');
cout << endl << "ll.print('\\n');" << endl;
ll.print('\n');

LinkedList.cpp --------

#include <iostream>
#include "LinkedList.h"
using namespace std;

LinkedList::LinkedList() : head(nullptr), tail(nullptr) {}

LinkedList::LinkedList(string name) : head(nullptr), tail(nullptr), name(name) {}


void LinkedList::make_test_list() {
/* again, not the real way to create a list, but it will give us something to
use for testing parts until we learn how.
*/
Node* newNode = new Node(7);
head = newNode;
newNode = new Node(3);
head->next = newNode;
newNode = new Node(12);
head->next->next = newNode;
tail = newNode;
}

string get_name() const {
return name;
}
void set_name(std::string){

name = name;
}

void LinkedList::print(char c, ostream& os) {
Node* cur = head;
while (cur != nullptr) {
os << *cur;
if (cur != tail)
os << c;
if (cur == tail)
os << endl;
cur = cur->next;
}
}

LinkedList.h----------

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <string>
#include "Node.h"

class LinkedList {
   Node* head;
   Node* tail;

   std::string name;

public:
   LinkedList();
   LinkedList(std::string);

/* not the real way to create a list, but it will give us something to
use for testing parts until we learn how.
*/
   void make_test_list();
   std::string get_name() const;
   void set_name(std::string );
   void print(char, std::ostream& os = std::cout);


};

#endif

Node.cpp----------

#include "Node.h"
#include <iostream>
std::ostream& operator<<(std::ostream& os, const Node& n) {
os << n.value;
return os;
}

Node.h-----------

#ifndef NODE_H
#define NODE_H
#include <iostream>

struct Node
{
   int value;
   Node* next;
   Node(int value) : value(value), next(nullptr) {}
   Node() : value(0), next(nullptr) {}
};

std::ostream& operator<<(std::ostream& os, const Node& n);


#endif

Main.cpp-------------

#include <iostream>
#include "LinkedList.h"
using namespace std;

int main() {
cout << endl << "Linked list print (traversal) lab" << endl;
   LinkedList ll("Test List");
   ll.make_test_list();
   // fills with { 7, 3, 12 }
cout << ll.get_name() << endl;
ll.make_test_list();
ll.set_name("My List");
cout << ll.get_name() << endl;

cout << endl << "ll.print('\\t');" << endl;
ll.print('\t');
cout << endl << "ll.print('\\n');" << endl;
ll.print('\n');
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here i am providing the answer. Hope it helps. please give me a like. it helps me a lot.

#include <iostream>
#include <string>

using namespace std;

#ifndef CDOG_H
#define CDOG_H

class CDog
{
    private:
        string name();    
        int weight();  
        bool rabid();    
    public:
        CDog();
        ~CDog();
    // Attributes
        void setRabid(bool x);
        bool getRabid();
        void setName(string x);
        string getName();
        void setWeight(int x);
        int getWeight();

    // Behaviours 
        void growl();
        void eat();
};

#endif // CDOG_H 
#include <iostream>
#include <string>
#include "CDog.h"

using namespace std;

CDog::CDog(int x, string x)
{
    rabid = false;
    weight = x;
    name = x;
}

CDog::~CDog()
{

}

void CDog::setRabid(bool myboolean)
{
    name = myboolean;
}

bool CDog::getRabid();
{
    return(rabid);
}

void CDog::setWeight(int y)
{
    weight = y;
}

int CDog::getWeight();
{
    return(weight);
}

void CDog::setName(string y)
{
    name = y;
}

string CDog::getName();
{
    return(name);
}
// Behaviours

void CDog::eat()
{
    cout << name << "is now eating" <<
            weight++ << endl;
}

void CDog::growl()
{
    cout << "Grrr" << endl;
}

Thank you. please upvote.

Add a comment
Know the answer?
Add Answer to:
For the LinkedList class, create a getter and setter for the private member 'name', constructing your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace...

    // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace std; class LinkedList; class Node { private: string data; Node* next; public: Node(string s, Node* n); string getData() {return data;} Node* getNext() {return next;} } Node::Node(string s, Node* n) { data = s; next = n; } class LinkedList { private: Node* head; public: LinkedList(); bool insert(string s); friend ostream& operator<<(ostream& os, LinkedList l); }; LinkedList::LinkedList() : head(nullptr) { } bool LinkedList::insert(string s) {...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • **HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

    **HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • C++ programming language: In this program you will create a simplified bag that acts like a...

    C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT