This section gives step by step instructions for new users. (If you know another way, don't complain, just do it your way.) Visual C++ 6.0 on UMBC PC's (Make your own small changes for your personal PC) 1) Find a PC on the ground floor or first floor of the Engineering and Computer Science, ECS, building. When not being used for classes, there are more PC labs on first floor, e.g. ECS104, Third floor, e.g. ECS333. 2) It should have a Windows NT logo. Press ctrl alt del keys all at the same time. (click on any stupid boxes that come up) 3) type your user name (same on gl and PC's) press tab and type your password (same on gl and PC's) press enter. (wait!) 4) press "start", move mouse to programs , hold left mouse button down and move to MSDOS command prompt, release mouse button. You should see a rather plain window with something like S:\> (You might want to create a directory for this class and go there: md cs291 cd cs291 ) 5) Type in your first C++ program using notepad, type notepad hello.cpp then type the following (proofread it!) // hello.cpp #include <iostream> using namespace std; int main() { cout << "Hello." << endl; return 0; } 6) Click on file. Click on exit, Click on YES to save your typing. 7) type cl /GX /ML hello.cpp If this says 'cl not found' then you need to run a .bat file: C:\program files\microsoft visual studio\VC98\BIN\VCVARS32.BAT go to 7) (or use system icon, environment, path - and add to path.) 8) type hello 9) pat yourself on the back if line 8) responded Hello. When you log off, your file is not lost. Your S:\ directory is available on any UMBC networked PC, Linux, SGI, SUN, etc. . (the full name of your directory is: /afs/umbc.edu/users/x/y/xyz/home where xyz is your last name x is first letter of your last name, y is second letter of last name ) 10) Now click on the Netscape icon to the left of the screen Click twice on the text line that says http://www.umbc.edu Edit this line to say http://www.csee.umbc.edu/~squire and press return. You should see your instructor watching you. Click on CMSC 291 to see the course WEB page. Click on syllabus, homework, etc. for valuable information. 11) To get files you read on the WEB into your directory, right click on the link, click on save link as , move up in the directory box to get to where you see S: and click on S: which is your home directory on the network. Click on open. Click on save. (Now isn't that easy :-)) 12) compile .cc files with cl /GX /ML /TP xxx.cc 13) edit files with notepad xxx.cc or whatever file name 14) Use the "Visual" full screen VC++ if you know how, just don't ask the instructor how to do it. (Take the class in VC++) V1) Using Visual C++, use Programs, Visual Studio, Visual C++ V2) Use File, New A new window should appear V3) click on "Project" tab if not in front V4) click on WIN32 Console Application and pick a name for project, hw1. Click OK. You can experiment by clicking on Hello project, Build, rebuild all, and Build, execute to see what a compile and execute looks like. V5) You may want to delete #include "stdafx.h" it should not be needed. V6) To keep things simple, you will need a new project for each homework.
0) Use a Lab machine, SGI - just log in, PC - reboot, down arrow to Linux. Log in from home or office: telnet irix.gl.umbc.edu for SGI Unix telnet linux.gl.umbc.edu for Linux Unix 1) Editors include emacs and vi and others. Use your favorite editor to type in the file hello.cc containing // hello.cc #include <iostream> using namespace std; int main() { cout << "Hello." << endl; return 0; } 2) type g++ -o hello hello.cc (ignore warning about some library file) 3) type hello 4) Pat yourself on the back if line 3) responded Hello. 5) Use Netscape to get the CMSC 291 WEB page at http://www.csee.umbc.edu/~squire/cs291.shtml (NOT a typo!) 6) Download files with right click, save link as, open, save.
1) Use your favorite editor to type in the file hello.C containing // hello.C #include <iostream> using namespace std; int main() { cout << "Hello." << endl; return 0; } 2) Don't try to compile yet! You need some include files first. Use Netscape to get include_gl.tar to your working directory. type tar -xvf include_gl.tar This has created a subdirectory named include with the files needed to make SGI look like a ISO Standard C++ compiler. Read chaotic compilers for more info. 3) type CC -n32 -Iinclude -o hello hello.C 4) type hello 5) Pat yourself on the back if line 4) responded Hello. 6) Use Netscape to get the CMSC 291 WEB page at http://www.csee.umbc.edu/~squire/cs291.shtml (NOT a typo!) 7) Download files with right click, save link as, open, save. 2CS) On Retriever or machines on the CS server, there is a different version of SGI CC (but being updated to be same) so ... Use Netscape to get include_cs.tar to your working directory. type tar -xvf include_cs.tar This has created a subdirectory named include with the files needed to make SGI look like a ISO Standard C++ compiler.
The most important item on all homework is YOUR NAME! within the first few lines of each file submitted. use the following command line in a Unix shell window on any gl machine submit cs291 HW1 <your-file-name> Note: "submit cs" are lower case, "HW" is upper case, fill in homework number and use the name of your file forHomework must be submitted when due. You loose 10%, one grade, the first day homework is late. Then 10% more per week, each week homework is late. Max of 50% off. A ZERO really hurts your final points. Do and turn in all homework, even if it is late. Homework is always due by 11:59 pm on the scheduled day. In case of gl computer outage on the due day, automatic extension of homework due date until one day after gl computers are back online. If "submit" does not work, then EMail your homework to the instructor with each file as an attachment. PLAIN TEXT ATTACHMENT! EMail only plain text! No word processor formats. You may use a word processor or other software tools but only submit plain text (there should be some "save as" that gives plain text). Special instructions if your are on a PC in a lab, at home on an ISP or anywhere on the Internet: Get your file ready to submit on your local machine. In a MSDOS window or Unix shell window (they are the same for this) type the statements ftp gl.umbc.edu <enter your UMBC user name> <enter your UMBC password> put <your-file-name> quit Obviously, do not type the greater than or less than symbols, they mean you have to fill in something. End every line by pressing Enter. Repeat the 'put' line for as many files as you are going to submit. Use ASCII mode for text files, binary mode for graphics and binary files. Now, do the actual 'submit' by typing the statements telnet gl.umbc.edu <enter your UMBC user name> <enter your UMBC password> submit cs291 HW1 hw1.txt ^D You can type the 'submit' line for more files or add more files to the end of the 'submit' command. ^D stands for holding down the ctrl key and pressing the "d" key. ^D is read control D. This example was for homework 1. Homework 2 would use submit cs291 HW2 change.cpp
See the C++ Compact Summary And pair the reserved words with their short definition. Your answer file is just the numbers from 1 to 26 followed by the answer letter a to z in the correct order. 1) bool 2) catch 3) class 4) delete 5) dynamic_cast 6) explicit 7) export 8) false 9) friend 10) mutable 11) namespace 12) new 13) operator 14) private 15) protected 16) public 17) template 18) this 19) throw 20) true 21) try 22) typeid 23) typename 24) using 25) virtual 26) wchar_t a) followed by an operator symbol, can define overloaded functions b) function for getting the type of a typename, == and != comparison c) specifies the following name is a type d) free space created by 'new' ( do not use 'free' ) e) catch block that handles a thrown exception f) value of type bool, meaning yes g) basic declaration for type wide character (internationalization) h) try block that precedes a catch block i) cast a pointer type if legal, else return null j) override const member functions in classes k) the stuff after this is visible outside the class l) the stuff after this is not visible outside the class m) the stuff after this is for classes that inherit this class n) a scope for declarations and function prototypes o) basic declaration of type Boolean p) throw an exception q) start a class definition r) defines template class or function s) the class object, used in 'return' statements and others t) get storage, free storage later with 'delete' ( do not use malloc) u) makes an entity in a namespace directly visible v) a function that can see internal stuff in a class w) restricts constructors to not allow myclass a = 7; x) precedes a template that can be used by other files y) value of type bool, meaning no z) this type of function is hidden if defined by an inheritor make the file name be hw1.txt submit the file on a gl machine using submit cs291 HW1 hw1.txt
Write a Standard C++ program (NOT a C program, NOT old C++) that reads an integer and for integers in the range 1 through 99 prints the (non-zero) number of US coins for this amount of money. e.g. Input: 99 Output: 3 quarters 2 dimes 4 pennies Input: 25 Output: 1 quarter Input: 10 Output: 1 dime Input: 5 Output: 1 nickel Input: 1 Output: 1 penny For an input less than one or greater than 99 (or any other garbage) print "no change possible". ABSOLUTELY do not let your program die or go into a infinite loop when it reads bad data. This applies to all homework. This is standard C++ philosophy, good quality software. Do NOT print zero(0) for any coin. Output such as 0 pennies will loose points. You can use the starter file change.cpp or do your own program from scratch. Compile and execute this before changing. See instructions above Getting Started for how to compile. Get the file change.dat that has the following data 99 94 25 10 5 1 -5 100 ABC 11any comment 74 no more Test and check your program interactively. Type change followed by pressing the Enter key Type 99 followed by pressing the Enter key Type ABC followed by pressing the Enter key, get no change possible Type 11a followed by pressing the Enter key, get normal output Type ^Z (ctrl and Z) to stop on MS Windows, ^D to stop on Unix Now, test and check your program using the data file change.dat Type change < change.dat > change.out Now look at change.out with your favorite editor and be sure it is correct. Submit only your file change.cpp (for Visual C++) or change.cc (for g++) or change.C (for SGI CC) No matter what you called the file during development, rename it to one of the three names above. Your program will be automatically graded by a script. It will appear you did not submit a program if you do not use one of the three standard names. (It really does not matter which compiler you used during development, all programs should work on all compilers.) Note The C and C++ "if" statements are identical. See the C++ Compact Summary The operator % used in a statement such as x = x % 25; (or x %= 25;) computes x modulo 25 or in other words removes all multiples of 25 from x. This is equivalent to x = x - (x/25)*25; Other operators are in C++ Compact Summary
Write a very simple class for a Rectangle that has a constructor and Set, Show and Move member functions. (To avoid conflicts with other library names, do not use the specific names: rectangle, Rectangle, Height, height, Width, width.) The Rectangle shall have an X, Y, height and width in its data structure. Write a main program to test your class with at least two rectangle objects. Use every member function at least twice. Observe: You should have three files. rectangle.h that has the class definition rectangle.cpp that implements the test_rectangle.cpp the main program to test rectangle.* Here is an example for a Circle class, all as one file: circle_all.cpp // circle_all.cpp has files circle.h test_circle.cpp circle.cpp // // Demonstrate a very simple class and its test program all in one file // circle.h should be in a file by itself // // Define a class that can be used to get objects of type Circle. // A class defines a data structure and the member functions // that operate on the data structure. // The name of the class becomes a type name. class Circle // the 'public' part should be first, the user interface // the 'private' part should be last, the safe data { public: Circle(double X, double Y, double R); // a constructor void Show(); // a member function private: double Xcenter; double Ycenter; double radius; }; // test_circle.cpp should be in a file by itself #include <iostream> // #include "circle.h" remove // when in a file by itself using namespace std; int main() // keep Circle stuff, add Rectangle stuff { Circle c1(1.0, 2.0, 0.5); // construct an object named 'c1' of type 'Circle' Circle circle2(2.5, 3.0, 0.1); // another object named 'circle2' c1.Show(); // tell the object c1 to execute the member function Show circle2.Show(); // circle2 runs its member function Show return 0; } // circle.cpp should be in a file by itself // implement the member functions of the class Circle #include <iostream> // #include "circle.h" remove // when in a file by itself using namespace std; Circle::Circle(double X, double Y, double R) { Xcenter = X; Ycenter = Y; radius = R; } void Circle::Show() { cout << "X, Y, R " << Xcenter << " " << Ycenter << " " << radius << endl; } The file above can be saved to your directory and compiled then executed with: on gl SGI CC -n32 -Iinclude -o circle_all circle_all.C circle_all on Unix g++ -o circle_all circle_all.cc circle_all on PC VC++ cl /GX /ML circle_all.cpp circle_all Having everything in one file makes it convenient to edit but is not convenient to be able to reuse classes. A more complete program than above is shown again as three files followed by the compilation commands. (This can be used as a starter file for your homework.) // circle.h #ifndef CIRCLE_H // be sure file only included once per compilation #define CIRCLE_H // // Define a class that can be used to get objects of type Circle. // A class defines a data structure and the member functions // that operate on the data structure. // The name of the class becomes a type name. class Circle // the 'public' part should be first, the user interface // the 'private' part should be last, the safe data { public: Circle(double X, double Y, double R); // a constructor void Show(); // a member function void Set(double R); // change the radius void Move(double X, double Y); // move the circle private: double Xcenter; double Ycenter; double radius; }; #endif // CIRCLE_H nothing should be added after this line // circle.cpp // implement the member functions of the class Circle #include <iostream> #include "circle.h" using namespace std; Circle::Circle(double X, double Y, double R) { Xcenter = X; Ycenter = Y; radius = R; } void Circle::Show() { cout << "X, Y, R " << Xcenter << " " << Ycenter << " " << radius << endl; } void Circle::Set(double R) { radius = R; } void Circle::Move(double X, double Y) { Xcenter = X; Ycenter = Y; } // test_circle.cpp #include <iostream> #include "circle.h" using namespace std; int main() { Circle c1(1.0, 2.0, 0.5); // construct an object named 'c1' of type 'Circle' Circle circle2(2.5, 3.0, 0.1); // another object named 'circle2' c1.Show(); // tell the object c1 to execute the member function Show circle2.Show(); // circle2 runs its member function Show c1.Move(1.1, 2.1); // move center c1.Show(); circle2.Set(0.2); // set a new radius circle2.Show(); return 0; } The files above can be saved to your directory and compiled then executed with: on gl SGI CC -n32 -Iinclude -o test_circle test_circle.C circle.C test_circle on Unix g++ -o test_circle test_circle.cc circle.cc test_circle on PC VC++ cl /GX /ML test_circle.cpp circle.cpp test_circle
Start with the file inherit.cpp Copy it to a file named inherit_e.cpp (or .cc or .C) Create an additional class with the name "E" that has integers e1 in the public section, e2 in the protected section and e3 in the private section. Class E is to have a member function test() similar to class D but with the correct variables in the return statement. (Hint: make a copy of class D and change all the D(d)'s to E(e)'s and fix up the inheritance.) Make class E inherit the existing classes A and D. Although class D contains class A, we want class E to contain only one copy of class A. (Lots of diagnostics if more than one A!) Problem: You will have to figure out what classes need to be inherited virtual. You will have to figure out what classes need to be inherited public, protected and private. Fix up E::test to sum the variables visible inside of E. Problem: Change the D object_d; to E object_e; in the main program. Output a number that is the number of int's in object_e. (warning: sizeof may not give the correct answer!) Problem: Fix the return statement in the main program to sum all the variables in object_e that are visible. (Model after the variables in object_d in inherit.cpp. Submit the file inherit_e.cpp (or .cc .C) submit cs291 HW4 inherit_e.cpp Start from: // inherit.cpp public: protected: private: // // define three classes A, B, C with variables 1, 2, 3 in // public: protected: and private: respectively // Then class D inherits public A, protected B and private C class A { public: int a1; protected: int a2; private: int a3; }; class B { public: int b1; protected: int b2; private: int b3; }; class C { public: int c1; protected: int c2; private: int c3; }; class D: public A, protected B, private C { public: int d1; // also here a1 int test(); protected: int d2; // also here a2 b1 b2 private: int d3; // also here a3 b3 c1 c2 c3 }; int D::test() { return d1 + a1 + d2 + a2 + b1 + b2 + d3; // all visible inside D // not visible a3 b3 c1 c2 c3 inside D } int main() { D object_d; // object_d has 12 values of type int in memory return object_d.d1 + object_d.a1; // only these are visible outside D // not visible object_d. a2 a3 b1 b2 b3 c1 c2 c3 d2 d3 }
Open book. Open note. Bring copies of the homework you submitted. Be organized, the classroom is crowded and desktops are small. Multiple choice questions based on: lectures, reading assignments and homework. Exam covers lectures 1-9 Exam covers book: Reading assignments yet focus on: pages 260-265 267-269.5 270.5-274 278.5-280 490-498 and class handouts (all came from WEB page.) Exam covers homework: HW1-HW4
Start with the file friends.cpp and add a class C1 modeled after the class B1. A1 is to declare C1 to be a friend. Use the technique in cout_friend.cpp to have the operator << defined for your class C1. The variables to output to stream are c1pub and c1pri In the main program, test your class by adding statements such as: C1 c_object(6,7); c_object.C1_out(a); cout << "6,7 " << c_object << endl;
Use test1_template.cc and template_class.cc as a guide. submit cs291 HW6 my_class.h This just includes your template and class and implementation. NO main() You should, of course, check out your header file by compiling and executing with the following test program. // test_my_class.cc test program for HW6 #include "my_class.h" #include <iostream> using namespace std; int main() { My_class<double> a_double; My_class<int> a_int(3); My_class<float> a_float(2.5F,3.2F); a_double.set(3.5,3.6); a_int.set(4); double d_test = a_double.get_sum(); int i_test = a_int.get_sum(); float f_test = a_float.get_sum(); cout << d_test << " " << i_test << " " << f_test << endl; cout << "7.1 7 5.7 should be printed above" << endl; return 0; } Specifications for the file my_class.h 1) This file will define one template class with the name My_class that takes one type as a parameter, possibly called T. 2) There will be two variables in the private part of type T. 3) there will be three constructors having: no parameters one parameter of type T that goes into the first variable two parameters of type T that go into the two variables 4) There will be a member function named get_sum() that has no parameters and returns the sum (just use + ) of the two variables in the private part. Obviously the return is of type T. 5) There will be member functions named set that: take one parameter and put the value in the second variable, take two parameters and put the two values in the two variables. You can resolve any details of the specification by studying the code above for test_my_class.cc Download the test program to check your my_class.h file. Compilation can be of the forms: g++ -o test_my_class test_my_class.cc CC -n32 -Iinclude -o test_my_class test_my_class.cc cl /GX /ML /TP test_my_class.cc Then type test_my_class to check the execution results. Important note: Never include a header file on the compile command. The #include brings in the header file. Some compilers give strange diagnostics when putting a .h file in the command line.
Last updated 1/29/01