1 Lab Exercises: LAB 1 (Input/Output, Strings, Classes, Inheritance) General guidance: 1. You will need to log on to your Linux environment for this lab. 2. Use a text editor of your preference to edit the code. For today's lab you will not need anything more complicated. 3. For quick on-line reference use (a book is preferable): http://www.cppreference.com/ 4. Use the g++ compiler at the command line: For example g++ ex1.cc -Wall -o ex1 will compile the source file ex1.cc and will generate the executable code in the program file ex1. 5. You can run a program from the directory where you compiled it by typing (for the above example): ./ex1 6. Use the mkdir command to create a directory structure like, e.g. your_home/CIP/labs/lab1. If unsure ask for help. Then cd to lab1. Exercise 1. Edit the code below so that the program prints "Hello World!!" when ran from the command line. Use the function cout instead of printf for doing your output. #includeusing namespace std; /* EXERCISE 1: HELLO WORLD */ /* INSERT THE CODE FOR OUTPUTTING "Hello World!!"*/ /*USE THE cout FUNCTION INSTEAD OF PRINTF*/ /*NOTE THE NEW WAY OF USING THE #include STATMENT: */ /*THE using KEYWORD, AND THE USE OF namespace REPLACE */ /*THE NEED FOR USING THE .h NOTATION THAT WE SAW IN THE LECTURE*/ /*THIS IS THE NEW STYLE NOTATION, AND THE ONE YOU SHOULD USE*/ { } - Create a source file ex1.cc in your directory with the above code - Edit the source file so that is outputs "Hello World!!" - Compile it using the g++ compiler and following the instructions above - Run the program from the command line using the instructions above Exercise 2. Input and output in C++ is pretty easy and fun. Look at the code given below: #include using namespace std; /* EXERCISE 2: INPUT & OUTPUT EXAMPLE */ int main() { int i; cout << "Type a number: "; cin >> i; cout << i << " times 3 is " << (i*3) << '\n'; } – Can you figure out how cin and cout work? – Create a file ex2.cc with the above code, compile it, and see the output. 2 – How does the last line of cout work? Experiment with different formatting options, and make sure you understand how cin and cout work. – At the on-line reference URL, have a look at functions available at the C++ I/O section. Exercise 3. Based on the previous example, write a program (ex3.cc) that reads two (2) numbers from the input, and outputs the bigger of the two numbers. Compile it and run it. Exercise 4. The standard C++ library provides a string type: string s = "Joe"; cout << s; cin >> s; There are a number of operators provided with strings. For example: The + operator is overloaded on strings: s = s + " and Mary"; etc. The same applies to other operators, e.g. +=, ==, <, etc. A difference with Java: strings are modifiable s.erase(); /*Make s empty*/ Using the on-line reference URL given at the start of these notes, look at the available functions for the C++ string type. Look at the code below: #include #include using namespace std; /* EXERCISE 4: C++ STRINGS */ int main() { string s; while (cin >> s) cout << s << '\n'; return 0; } - What does this program do? - Create a source file ex4.cc with the above code, compile it and run it. - Modify the code so that the length of each string is printed, together with the string - Modify the code so that the first letter of each word in the input is replaced in the output with -, so that for example if you type "hair" in the input, you will get "-air" in the output 4.a - Experiment with the +, =, != etc. operators for C++ strings by adding appropriate code in the example given below. #include #include using namespace std; /* EXERCISE 4a: C++ STRINGS */ 3 /* THE AIM IS TO WRITE CODE THAT TESTS HOW THE OVERLOADED OPERATORS */ /* WORK WITH STRINGS IN C++ */ int main() { string s1, s2, s3; } - Create a source file ex4a.cc with the above code in your directory - Modify it at will to experiment with the +, =, != etc. operators for C++ strings - How do the < and > operators work for strings? Exercise 5. Let us consider classes in C++ now. We will create a class of our own, which we will call vector2d. The class is supposed to represent 2-dimensional objects, that will have x and y co-ordinates of type double. Let us not worry about what x and y actually represent for now. - Write the code for the class vector2d. Assume that the variables x and y will be public. - Create 2 constructors: The default constructor vector2d() that sets x and y to zero The constructor vector2d(double X, double Y) that initialises the variables using X and Y. - Write a member function for the class vector2d that returns the length of the vector: double length(); Question: How is the length of a 2-D vector defined? - Create a main function that initialises vector2d objects and prints their co-ordinates and their respective length. HINTS a) Refer back to the lecture notes about how to create the classes, the best strategy for separating prototypes and implementation, etc. b) Include the library for the sqrt function ñ you will need it. Exercise 6. Create a class building with variables int rooms, int floors, int area. Write the code for the constructor(s) for the class, and for getters-setters for all the variables. - Create a subclass house of building. house has three more variables: string name, int bedrooms, int bathrooms. - Write the appropriate code for constructors and getters/setters for the subclass. - Include a main function in your code, where you create two houses with different properties. - Output the rooms, floors, area, names, bathrooms and bedrooms of the two houses you created. - Output the name of the house with the largest area.