Question
COMP 53: Pointers Lab, part 2 Instructions: In this lab, we are going to review pointers and how to avoid memory leaks. Get into groups
COMP 53: Pointers Lab, part 2 Instructions: In this lab, we are going to review pointers and how to avoid memory leaks. Get into groups of at most two people to accomplish this lab. At the top of your source code files list the group members as a comment. Each member of the group must individually submit the lab in Canvas. This lab includes 27 points in aggregate. The details are given in the following. 1. City and CoastalCity Extend city.h from the previous lab as follows: 1. Add a default constructor that assigns N/A and 0 to citys name and population (2 points). 2. Add a second constructor that receives name and population as input, and assigns them to citys name and population (2 points). Extend coastalcity.h from the previous lab as follows: 1. Add a new private data component which is a pointer to some City. Call it adjCity. This points to some adjacent city of a coastal city (2 points). 2. Initialize adjCity in the default constructor by assigning N/A to its name, 0 to its population (2 points). 3. Add two extra input arguments to the second constructor that demonstrate the adjacent citys name and population. Then in the body of the constructor, initialize adjCity with the input name and population (2 points). 4. Extend printInfo() function by printing the details about the adjacent city. For this purpose, invoke the printInfo() function of the adjacent city (2 points). 5. Define a getter function that returns the pointer to adjacent city (2 points). Call the function getAdjCity(). 2. Function main Include city.h and coastalcity.h in main.cpp. In function main() do the following step by step: 1. Allocate memory for a coastal city object in the heap, and assign the address of it to a pointer. Lets call this pointer ccityPtr. The object must have the following details: Name to be Miami, population to be 500000, body of water name to be Atlantic ocean, number of beaches 8, and the adjacent city to be Coral Gables with population 50000 (4 points).
2. Print the address of the coastal city object from previous step (2 points).
3. Print the address of the adjacent city to the coastal city from previous step (2 points).
4. Print the information about the coastal city, by calling printInfo() (2 points).
The output of the program may look like the following:
address of coastal city: 0x1a36c20 address of adjacent city: 0x1a36c90 Name: Miami Population: 500000 Water: Atlantic Ocean No. of Beaches: 8 Adjacent City: Name: Coral Gables Population: 50000 Note that the addresses may be different than this, based on the memory regions that your Operating System allocates for your program.
3. Memory leak In main(), if you delete ccityPtr, then the object will be removed from the heap. However, adjacent city data will not be removed, since it resides in another place in the heap. This causes memory leak. To avoid memory leak, you need to explicitly define a destructor. 1. Define the destructor for CoastalCity. Within the destructor delete the memory allocated for adjacent city (3 points). Now, when you delete ccityPtr, destructor will be invoked, which avoids causing memory leaks.
STARTER CODE
MAIN.CPP
#include "city.h" #include "coastalcity.h" #include
int CoastalCity::coastalCityCount = 0;
int main(){ CoastalCity ccity1("San Diego", 15000000, "Pacific Ocean", 5);
cout <<"The address of ccity1: " <<&ccity1< //stack memory CoastalCity *ccityPtr1 = NULL; //print the address of ccityPtr1 which NULL cout <<"The address stored in ccityPtr1: " < cout <<"ccityPtr1 used to call printInfo()"< cout <<"Using * syntax:"< //allocate memory using new from heap CoastalCity *ccityPtr2 = new CoastalCity("Miami", 500000, "Atlantic Ocean", 8); cout <<"The address stored in ccityPtr2: " < //heap memory cout <<"ccityPtr2 used to call printInfo()"< ccityPtr2->printInfo(); cout< cout <<"Using * syntax:"< (*ccityPtr2).printInfo(); cout< //free the allocated memory delete ccityPtr2; cout <<"The address stored in ccityPtr2: " < return 0; } CITY.H #ifndef CITY_H #define CITY_H #include class City { public: void setName(string name) {this -> name = name;} void setPopulation(unsigned int population) { this -> population = population; } string getName() const {return this-> name;} unsigned int getPopulation() const {return this -> population;} virtual void printInfo() const { cout<<"Name: "< } protected: string name; unsigned int population; }; #endif #ifndef COASTALCITY_H #define COASTALCITY_H #include "city.h" #include using namespace std; class CoastalCity : public City { private: string waterName; int beachNum; static int coastalCityCount; public: CoastalCity(){ name = "N/A"; population = 0; waterName = "N/A"; beachNum=0; } CoastalCity(string cityName, unsigned int cityPop, string waterN, int beachN){ name = cityName; population = cityPop; waterName = waterN; beachNum=beachN; } void setWaterName (string waterN){ waterName = waterN; } void setBeachNum (int beachN){ beachNum = beachN; } string getWaterName(){ return waterName; } int getBeachNum(){ return beachNum; } CoastalCity operator*(int val) { CoastalCity new_city; new_city.setName(this->name); new_city.setPopulation(this->population); new_city.setWaterName(this->waterName); new_city.setBeachNum(this->beachNum * val); return new_city; } CoastalCity operator+(CoastalCity c){ CoastalCity new_city; new_city.setName(this->name.append(c.getName())); new_city.setPopulation(this->population + c.getPopulation()); new_city.setWaterName(this->waterName.append(c.getWaterName())); new_city.setBeachNum(this->beachNum + c.getBeachNum()); return new_city; } static int getCoastalCityCount() { return coastalCityCount; } void printInfo() { cout << "Name: "< } }; #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started