In: Computer Science
CIS247C WEEK 2 LAB
The following problem should be created as a project in Visual Studio, compiled and debugged. Copy your code into a Word document named with your last name included such as: CIS247_Lab2_Smith. Also include screen prints of your test runs. Be sure to run enough tests to show the full operation of your code. Submit this Word document and the project file (zipped).
The Zoo Class
This class describes a zoo including some general visitor information. We will code this class using the UML below. There is a video in the announcements on how to code a class from a UML diagram. There is also an example shown by the Exercise we complete together in the Week 2 class.
It is required to use a header file for your class definition that contains the private attributes and prototypes for the functions (no code in the header file!) Use a .cpp file for the code of the functions. Then create a separate .cpp file for the main function.
The file with the main function should have a documentation header:
/*
Programmer Name: Your Name
Program: Zoo Class
Date: Current date
Purpose: Demonstrate coding a simple class and creating objects from it.
*/
Coding the Zoo Class
Create a header file named: Zoo.h and a .cpp file named: Zoo.cpp. The easiest way to do this is to use the “Add Class” function of Visual Studio.
In the header file, put your class definition, following the UML as a guide. In the private access section, declare your variables. In the public access section, list prototypes (no code) for the two constructors, setters and getters and the other functions shown.
Zoo |
-zooName : string //example: Brookfield Zoo -zooLocation : string //city, state -yearlyVisitors : int -adultAdmissionCost : double |
+Zoo() //default constructor should zero numbers and set strings to “unknown” +Zoo(name : string, place : string, visitors: int, cost : double) +getName() : string +getLoc() : string +getVisitors() : int +getAdmission() : double +setName(name : string) : void +setLoc(place : string) : void +setVisitors(visitors : int) : void +setAdmission(cost : double) : void +printInfo() : void //prints the name, location, visitors and adult admission cost |
In the Zoo.cpp file, put the code for all the functions. Remember to put the class name and scope resolution operator in front of all function names.
Example setter:
void Zoo::setVisitors(int visitors) {yearlyVisitors = visitors;}
Example getter:
int Zoo:getVisitors() {return yearlyVisitors;}
In the .cpp file for the main function, be sure to add this to your includes:
#include “Zoo.h”
Here is some high level pseudocode for your main function:
main
declare 2 string variables for the name and location
declare int variable for the number of visitors
declare double variable for the adult admission cost
declare a Zoo object with no parentheses (uses default constructor)
set a name for the Zoo (name of your choice such as “Brookfield Zoo”)
set a location for the Zoo in the format: city, state (example: “Chicago, IL”)
set the yearly number of visitors (any amount)
set the adult admission cost (any amount)
//use proper money formatting for the following
use the printInfo() method to print the Zoo object’s information
//Preparing for the second Zoo object
//Suggestion: use getline(cin, variable) for the strings instead of cin
Ask the user for a zoo name and input name into local variable declared above
Ask the user for the zoo location and input the location into local variable
Ask the user for the zoo yearly number of visitors and input into local variable
Ask the user for the adult admission cost and input into local variable
//Using the constructor with parameters to create an object
declare a Zoo object and pass the four local variables in the parentheses
use the printInfo() method to print the Zoo object’s information
end main function
SUBMITTING YOUR PROGRAM
When you are done testing your program, check that you have used proper documentation. Then copy and paste your code from Visual Studio into a Word document. Be sure to copy all three files. Take a screenshot of the console window with your program output. Paste this into the same document.
Save your Word document as: CIS247_Week2Lab_YourLastName.
Submit your Word document and also the properly zipped project folder.
C++ language
// Zoo.h
#ifndef ZOO_H
#define ZOO_H
class Zoo
{
private :
//Declaring instance variables
string zooName;
string zooLocation;
int yearlyVisitors;
double adultAdmissionCost;
public :
//Zero argumented constructor
Zoo();
//Parameterized constructor
Zoo(string name ,string place,int visitors,double
cost);
// getters and setters
string getName();
string getLoc();
int getVisitors();
double getAdmission();
void setName(string name);
void setLoc(string place);
void setVisitors(int visitors);
void setAdmission(double cost);
//This function is used to display the contents of
an object inside it
void printInfo() ;
};
#endif
________________________
// Zoo.cpp
#include <iostream>
using namespace std;
#include "Zoo.h"
Zoo::Zoo() //default constructor should zero numbers
and set strings to “unknown”
{
this->zooName="unknown";
this->zooLocation="unknown";
this->yearlyVisitors=0;
this->adultAdmissionCost=0;
}
//Parameterized constructor
Zoo::Zoo(string name ,string place,int visitors,double cost)
{
this->zooName=name;
this->zooLocation=place;
this->yearlyVisitors=visitors;
this->adultAdmissionCost=cost;
}
// getters and setters
string Zoo::getName()
{
return zooName;
}
string Zoo::getLoc()
{
return zooLocation;
}
int Zoo::getVisitors()
{
return yearlyVisitors;
}
double Zoo::getAdmission()
{
return adultAdmissionCost;
}
void Zoo::setName(string name)
{
this->zooName=name;
}
void Zoo::setLoc(string place)
{
this->zooLocation=place;
}
void Zoo::setVisitors(int visitors)
{
this->yearlyVisitors=visitors;
}
void Zoo::setAdmission(double cost)
{
this->adultAdmissionCost=cost;
}
//This function is used to display the contents of
an object inside it
void Zoo::printInfo()
{
cout<<"Zoo Name
:"<<zooName<<endl;
cout<<"Zoo Location
:"<<zooLocation<<endl;
cout<<"No of Yearly Visitors
:"<<yearlyVisitors<<endl;
cout<<"Adult Admission Cost
:$"<<adultAdmissionCost<<endl;
}
_____________________________
// main.cpp
#include <iostream>
using namespace std;
#include "Zoo.h"
int main()
{
//Declaring variables
string zooName;
string zooLocation;
int yearlyVisitors;
double adultAdmissionCost;
//Creating an instance of Zoo class
Zoo z1;
//calling the setters
z1.setName("Zambo Zoo");
z1.setLoc("CL");
z1.setVisitors(150000);
z1.setAdmission(120);
z1.printInfo();
//Getting the input entered by the user
cout<<"\nEnter Zoo name :";
getline(cin,zooName);
cout<<"Enter Zoo Location :";
getline(cin,zooLocation);
cout<<"Enter yearly visitors :";
cin>>yearlyVisitors;
cout<<"Enter Adult Admission cost :$";
cin>>adultAdmissionCost;
cout<<endl;
//Creating an instance of Zoo class
Zoo
z2(zooName,zooLocation,yearlyVisitors,adultAdmissionCost);
z2.printInfo();
return 0;
}
_____________________________
Output: