Computer science C++ Data structures
CS 240 1 Fall 2020
Exam #1 (Monday)
Part II (50 points)
The questions that follow are based on the following C++ program: /*Program to demonstrate memory allocation Written by Ron September 2019 Language: C++ (Cygwin 64-bit g++ compiler)
Identifier names taken from characters on Happy Days (1974-1984) */
#include <iostream> Fonzie
using namespace std;
int main(void) { int richie_cunningham, // primary character
joanie_cunningham, // Richie's sister fonzie; // lived with the Cunninghams and
// stole the show int
*howard_cunningham, // the father
*marion_cunningham, // the mother
Ralph Malph *potsie_weber, // a friend of Richie *ralph_malph; // also a friend of Richie
richie_cunningham=61; joanie_cunningham=8; fonzie=richie_cunningham%joanie_cunningham*4-11; howard_cunningham=&richie_cunningham;
marion_cunningham=&joanie_cunningham; potsie_weber=&fonzie; ralph_malph=new int;
*ralph_malph=fonzie+richie_cunningham-joanie_cunningham;
cout<<"richie_cunningham is stored in location "<< &richie_cunningham<<" and its value is "<< Richie Cunningham
richie_cunningham<<endl; (left) and Potsie cout<<"joanie_cunningham is stored in "<< Weber (right) &joanie_cunningham<<endl;
cout<<"howard cunningham is stored in "<< &howard_cunningham<<" and its value is "<< howard_cunningham<<endl;
cout<<"ralph_malph is stored in "<< &ralph_malph<<" and its value is "<<
ralph_malph<<endl; cout<<"the value pointed to by ralph_malph is "<< *ralph_malph<<endl; return 0; } When this code is compiled and executed the following output is obtained:
CS 240 2 Fall 2020
Exam #1 (Monday)
richie_cunningham is stored in location cbf8 and its value is 61 joanie_cunningham is stored in cbf4
howard
cunningham is stored in cbe8 and its value is cbf8 ralph_malph is stored in cbd0 and its value is 10460 the value pointed to by ralph_malph is 62
a. A partially filled in memory map for this program is shown below. Fill in the missing
elements (4 points each, 40 points total)
b. Explain why seven of the data elements are all in consecutive memory spaces in the cb
memory range, while one data element is in a completely different memory space (10460). 10 points.
Part IV. Trace the code that follows showing all output in the order that it appears on the screen (50
points). Note, there are two files shown here: main.cpp and exam1.h. The squirrel class is fully
defined within the exam1.h file. For the sake of simplicity the code for all the functions that are in
the class (not just the headers) are in the exam1.h file.
===============================main.cpp follows=====================
/*Object Oriented Trace
Address Identifier Stored
Contents
cbf8 61
cbf4 joanie_cunningham
cbf0
cbe8 howard_cunningham cbf8
cbe0
cbd8
cbd0 ralph_malph
10460 none
CS 240 3 Fall 2020
Exam #1 (Monday)
Written By Ron
September 2020
Language: C++ (Cygwin 64-bit g++
compiler)
*/
#include <iostream>
using namespace std; #include "exam1.h"
int main(void)
{ squirrel rocky;
squirrel ricky(16,3);
int sum;
sum=0;
cout<<"In rocky, bw is "<<rocky.get_bw()<<endl; cout<<"In
rocky, boris is "<<rocky.get_boris()<<endl; cout<<"In rocky,
peachfuzz is "<<rocky.get_peachfuzz()<<endl;
cout<<"In rocky values of cloyd are: ";
for (int i=0;i<5;i++)
{cout<<rocky.get_cloyd(i)<<" ";
sum=sum+rocky.get_cloyd(i);
}
cout<<endl;
cout<<"sum is now "<<sum<<endl;
cout<<"In ricky, rocky is "<<ricky.get_rocky()<<endl;
cout<<"In ricky, boris is "<<ricky.get_boris()<<endl;
cout<<"In ricky, peachfuzz is "<<ricky.get_peachfuzz()<<endl;
for (int i=0; i<5;i++)
{cout<<ricky.get_cloyd(i)<<" ";
sum=sum+ricky.get_cloyd(i);
}
cout<<"At end sum is "<<sum<<endl;
return 0;
}
//exam1.h file is shown on the next page
=========================exam1.h follows (2 pages)================
/*Header file exam1.h
Written by Ron
September 2020
Language: C++ (Cygwin 64-bit g++ compiler
CS 240 4 Fall 2020
Exam #1 (Monday)
*/ #include
<iostream> using
namespace std;
class squirrel
{ public:
squirrel() /*Default constructor for squirrel class*/
{int i;
cout<<"Entering default squirrel constructor"<<endl;
rocky=12;
bw=5;
boris=rocky*(bw-bw);
peachfuzz=rocky+boris-bw;
for (i=0;i<5;i++)
cloyd[i]=i+boris+peachfuzz;
}
squirrel(int peabody, int sherman)
{int i;
cout<<"Entering explicit value constructor"<<endl;
rocky=peabody;
bw=sherman;
boris=rocky+bw;
peachfuzz=boris*rocky;
for (i=0;i<5;i++)
cloyd[i]=i+peachfuzz; }
//continued on next page
//Accessor functions
int get_rocky(void)
/*Accessor function to return value of rocky data member*/
{return rocky;
}
int get_bw(void)
/*Accessor function to return value of bw data member */
{return bw;
}
int get_boris(void)
CS 240 5 Fall 2020
Exam #1 (Monday)
/*Accessor function to return value of boris data member */
{return boris;
}
int get_peachfuzz(void)
/*Accessor function to return value of peachfuzz data member*/
{return peachfuzz;
}
int get_cloyd(int i)
/*Accessor function to return value of one element of cloyd
data member*/
{return cloyd[i];
}
private: int rocky, bw, boris,
peachfuzz, cloyd[5];
};
//end of class definition
Capt. Peter Peachfuzz Bullwinke, Rocky,Cloyd and Gidney
Boris Badenov