CS 4448 - Fall 1998
Object-Oriented Programming and Design
Homework #3
Due on Monday, September 21 at the start of class.
Print out this C++ assignment with a sample run and the one
question at the bottom of this assignment/web page and turn it in at
the start of class. Also tar and gzip the necessary files (.h .cc
makefile etc.) for the TA to compile and test your code. Do not send
the executable. Email this tgz(tar/gzip) file to the TA before the start
of class.
C++ program - And you are?
For this assignment you need to create the classes below and they
must respond to the methods in the way specified. Your
implementation may respond to additional methods but the methods and
and responses should be appropriate for that class. First the
instantiation method is given and then examples of method responses follow.
Create the classes below and the constructor
- Dog - sasha = Dog(40,"Sasha","Shepard");
- Cat - romeo = Cat(20,"Romeo","Mix");
- Sprinter - carl = Sprinter(180,"Carl Lewis")
- Molecule - water = Molecule("Water")
- Hydrogen - h = Hydrogen()
- Krypton - kr = Krypton()
- Pine - pine = Pine(25,20)
Methods to implement and the result
- sasha.name() -> "Sasha"
- romeo.name() -> "Romeo"
- romeo.mass() -> 20
- romeo.mass(25) -> 25
- romeo.mass() -> 25
- sasha.speak() -> "Whof, Whoph"
- romeo.speak() -> "Meow"
- sasha.legs() -> 4
- sasha.hasLiveYoung() -> True
- carl.legs() -> 2
- carl.speak() -> "Sorry, gotta run"
- carl.mass() -> 180
- pine.name() -> "Pine"
- pine.height() -> 20
- pine.deciduous() -> False
- water.mass() -> 18.01528
- water.symbol() -> "H2O"
- water.name() -> "Water"
- h.name() -> "Hydrogen"
- h.mass() -> 1.00794
- kr.mass() -> 83.8
- kr.family() -> "Noble Gas"
- kr.symbol() -> "Kr"
More
Below is the main you should use and the associated output.
Note: I create some instances using new just to show you a bit more,
no other reason.
----------------------------main.cc-----------------------------
#include < stdio.h > //the includes string.h, etc..
#include < iostream.h >
#include < iomanip.h >
#include <"yourincludes.h">
// this is the main that creates object instances and calls their methods
int
main(int argc, char *argv[]) {
Dog sasha(40,"Sasha","Shepard"); // the memory is automatically
allocated when entering the basic block and will be released
upon exit
Cat *romeo;
Sprinter carl(180,"Carl Lewis");
Molecule water("water");
Hydrogen *h;
Krypton kr;
Pine pine(25,20);
int count=0;
romeo = new Cat(20,"Romeo","Mix");
h = new Hydrogen(); // you are allocating the memory
cout << ++count << ". " << "sasha.name() ->" << sasha.name() << endl;
cout << ++count << ". " << "romeo->name() ->" << romeo->name() << endl;
cout << ++count << ". " << "romeo->mass() ->" << romeo->mass() << endl;
cout << ++count << ". " << "romeo->mass(25) ->" << romeo->mass(25) << endl;
cout << ++count << ". " << "romeo->mass() ->" << romeo->mass() << endl;
cout << ++count << ". " << "sasha.speak() ->" << sasha.speak() << endl;
cout << ++count << ". " << "romeo->speak() ->" << romeo->speak() << endl;
cout << ++count << ". " << "sasha.legs() ->" << sasha.legs() << endl;
cout << ++count << ". " << "sasha.hasLiveYoung() ->"
<< sasha.hasLiveYoung() << endl;
cout << ++count << ". " << "carl.legs() ->" << carl.legs() << endl;
cout << ++count << ". " << "carl.speak() ->" << carl.speak() << endl;
cout << ++count << ". " << "carl.mass() ->" << carl.mass() << endl;
cout << ++count << ". " << "pine.name() ->" << pine.name() << endl;
cout << ++count << ". " << "pine.height() ->" << pine.height() << endl;
cout << ++count << ". " << "pine.deciduous() ->" << pine.deciduous() << endl;
cout << ++count << ". " << "water.mass() ->" << setiosflags(ios::fixed)
<< setw(9) << setiosflags(ios::showpoint) << setprecision(5)
<< water.mass() << endl;
cout << ++count << ". " << "water.symbol() ->" << water.symbol() << endl;
cout << ++count << ". " << "water.name() ->" << water.name() << endl;
cout << ++count << ". " << "h->name() ->" << h->name() << endl;
cout << ++count << ". " << "h->mass() ->" << h->mass() << endl;
cout << ++count << ". " << "kr.mass() ->"
<< setprecision(1) << kr.mass() << endl;
cout << ++count << ". " << "kr.family() ->" << kr.family() << endl;
cout << ++count << ". " << "kr.symbol() ->" << kr.symbol() << endl;
delete romeo;
delete h; // you must therefore release the memory
};
------------Output---------------
1. sasha.name() -> Sasha
2. romeo->name() -> Romeo
3. romeo->mass() -> 20
4. romeo->mass(25) -> 25
5. romeo->mass() -> 25
6. sasha.speak() -> Whof, Whoph
7. romeo->speak() -> Meow
8. sasha.legs() -> 4
9. sasha.hasLiveYoung() -> True
10. carl.legs() -> 2
11. carl.speak() -> Sorry, gotta run
12. carl.mass() -> 180
13. pine.name() -> Pine
14. pine.height() -> 20
15. pine.deciduous() -> False
16. water.mass() -> 18.01528
17. water.symbol() -> H2O
18. water.name() -> Water
19. h->name() -> Hydrogen
20. h->mass() -> 1.00794
21. kr.mass() -> 83.8
22. kr.family() -> Noble Gas
23. kr.symbol() -> Kr
You must have a Makefile to compile your code:
------------example------------
sources = Sprinter.cc Cat.cc Dog.cc main.cc
hw3 : $(sources)
g++ -g -o hw3 $(sources)
clean:
rm -f hw3 *~
======================================
Questions
Answer this question after you have completed the preceding.
- Describe how your implementation does and does not conform to the
OO paradigm.
Adam Jonathan Griff,
computer@griffmonster.com
Copyright © University of Colorado. All rights reserved.
Revised: September 14, 1998