QuestionQuestion

Pointers and Dynamic Allocation With Padme
This is a relatively simple assignment to get you to practice using pointers and dynamic allocation.
Read the comments in the provided file ast04skeleton.cpp and write the code they ask for. Make sure you document your code with additional comments as well as test your code with extra cases as necessary to make sure everything works fine since as you will find out, when dealing with pointers, just because it compiles, it doesn’t mean it won’t crash the moment you execute it!
Most comments (aside from class related stuff) equate to one line of code per //comment. So use that as a basis. My final solution was 239 lines of code but I wrote all my methods inside the classes so yours may be a little bit longer. Not that it matters.
The output below is the minimum output you should get if you write all the code. Feel free to add more as you test your classes but make sure you put this output first so the grader can see that everything works.
This assignment should familiarize you with dynamic allocation and pointers (that’s the point!) so please explore and try out things, crash in the process, so you really learn how pointers work. Explore!
That’s the only way you will really learn them.
Required minimum output (the two memory addresses may vary):
Assignment04$ ./a.out
ptr Address: 0x7ffd003fce10 : 2
MPTR, Address: 0x7ffd003fce18, : Value 2
Animal> Name: Default, Age: 0
Dog> Name: Padme, Age: 2, Breed: Unknown
Animal> Name: Padme, Age: 2
Animal> Name: Padme, Age: 2
Dog> Name: Padme, Age: 2, Breed: Cavalier Spaniel
House:
Dog> Name: Padme, Age: 2, Breed: Cavalier Spaniel
Bird>1
Bird>2
Bird>3
Bird>4
Bird>5
Bird>6
Bird>7
Bird>8
Bird>9
Bird>10
F

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include<iostream> //cout
#include<stdlib.h> //Malloc/Free
#include<stdio.h> //printf
#include<cstring>   //strcpy
#include<string>   //Strings

using namespace std;

//Create a class called animal_t with two pointers, name and age (string and int pointers). All members of the class are public.
//Create constructor that allocates memory to name and age and then initializes them to "Default" and 0. Use the new keyword
//Create accessors and mutators for both member variables [ int setAge() , void getAge(int a) etc... ]
//Create a print function that prints: "Animal> Name: X, Age: Y\n" replace X and Y with name and age variables. ie: "Animal> Name: Default, Age 0"
//Create a Destructor that uses delete and sets age and name pointers to null.

class animal_t {
public:
    //Your Code Here
    // two pointers, name and age (string and int pointers)
    // All members of the class are public.
    string * name;
    int * age;

    //Create constructor that allocates memory to name and age and then initializes them to "Default" and 0. Use the new keyword

    animal_t() {
       name = new string;
       age = new int;
       *name = "Default";
       *age = 0;
    }

    //Create accessors and mutators for both member variables [ int setAge() , void getAge(int a) etc... ]

    int getAge() {
       return *age;
    }

    void setAge(int a) {
       *age = a;
    }

    string getName() {
       return *name;
    }

    void setName(string s) {
       *name = s;
    }

    //Create a print function that prints: "Animal> Name: X, Age: Y\n" replace X and Y with name and age variables. ie: "Animal> Name: Default, Age 0"

    void print() {
       cout << "Animal> Name: " << *name << ", Age: " << *age << "\n";
    }

    //Create a Destructor that uses delete and sets age and name pointers to null.

    ~animal_t() {
       delete age;
       delete name;
       age = NULL;
       name = NULL;
    }
};

//Create a class dog_t that publicly inherits animal_t
//Create a public char pointer called breed
//Create getBreed()accessor that returns a char pointer to breed.
//Create a mutator setBreed() that takes a string and converts it to char array and stores it in breed.
//To do this, you will need to delete the old breed using free and then allocate a new breed char array.
//Use str.length() to find length needed to allocate it. use strcpy(thestring, breed) to copy the string into the char array.
//This breed pointer will be an array of characters (null terminated) to store the name of the breed.
//Creater 2 constructors. A default constructor that dynamically allocates the breed array with a size of 8 and initalize it with "Unknown"
//After the 2nd n in Unknown. add a null character '\0' That's what we mean with null terminated.
//The second constructor should expect two parameters. the size to dynamically allocate our char array first,
//then the actual breed name to store in the array, passed as a char array. Make sure to make a deep copy.
//For this class you are not allowed to use NEW and DELETE, you must use malloc and free.
//Create a destructor that will free the memory and set pointer to null for breed.
//redefine the print function but this time have it print Dog> infront of the name and age. Also print the Breed after the age:
//For example: Dog> Name: Default, Age: 0, Breed: Unknown

class dog_t : public animal_t {
public:
    //Your Code Here
    //Create a public char pointer called breed
    char * breed...

By purchasing this solution you'll be able to access the following files:
main.cpp.

$25.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats