CPTG122 Introduction to Computer Science II La Sierra University C++ Classes 1. Classes Classes allow you to group not only the data elements together but also the operations that you want to have associated with the data. From the menu, select File | New Project. Under the Command Line Utility section, choose the C++ Tool template. Select the folder that you want to save your project in and give your project a name. In the Xcode main window under the Groups & Files section, make sure that you have highlighted the Source folder. From the menu, select File | New File. Under the C and C++ section choose the C++ File template. Page 1 of 6 Copyright Enoch Hwang 2009 CPTG122 Introduction to Computer Science II La Sierra University For the file name, type in Temperature.cpp. Make sure that there is a check mark next to “Also create Temperature.h.” Page 2 of 6 Copyright Enoch Hwang 2009 CPTG122 Introduction to Computer Science II La Sierra University In the Temperature.h file, type in the following declaration for the Temperature class: /* * Temperature.h * * Created by Enoch Hwang on 1/14/10. * Copyright 2010 La Sierra University. All rights reserved. * */ class Temperature { private: int degree; char scale; public: void setTemperature(int d, char s); void printTemperature(); }; Page 3 of 6 Copyright Enoch Hwang 2009 CPTG122 Introduction to Computer Science II La Sierra University In the Temperature.cpp file, type in the following definition for the Temperature class functions: /* * Temperature.cpp * * Created by Enoch Hwang on 1/14/10. * Copyright 2010 La Sierra University. All rights reserved. * */ #include <iostream> #include "Temperature.h" void Temperature::setTemperature(int d, char s){ degree = d; scale = s; } void Temperature::printTemperature(){ printf("%i%c",degree,scale); } In the main.cpp file, type in the following: /* * main.cpp * * Created by Enoch Hwang on 1/14/10. * Copyright 2010 La Sierra University. All rights reserved. * */ #include <iostream> #include "Temperature.h" int main (int argc, char * const argv[]) { Temperature today; Temperature tomorrow; today.setTemperature(23, 'C'); tomorrow.setTemperature(78, 'F'); printf("today's temperature is "); today.printTemperature(); printf("\ntomorrow's temperature is "); tomorrow.printTemperature(); return 0; } Page 4 of 6 Copyright Enoch Hwang 2009 CPTG122 Introduction to Computer Science II La Sierra University Sample output: today's temperature is 23C tomorrow's temperature is 78F Page 5 of 6 Copyright Enoch Hwang 2009 CPTG122 Introduction to Computer Science II La Sierra University 2. Exercises (Problems with an asterisk are more difficult) In all of the following questions, you need to write the complete class and main program to test out the class. 1. Implement the Temperature class as shown in section 1 above and make sure that it works. 2. Write a class called Circle that implements a circle shape object. It has an integer for storing the radius of the circle. Write three member functions, one function sets the radius of the circle, one function returns the area of the circle and one returns the circumference of the circle. 3. Write a class called Square that implements a square shape object. It has an integer for storing the length of a side of a square. Write three member functions, one function sets the side of the square, one function returns the area of the square and one returns the circumference of the square. 4. Write a class called Length that implements a “length” object. It should have two integers to store the feet and inches. Write two member functions, one sets the length and one prints out the length. 5. Write a class called Date that implements a “date” object. It should have three integers to store the year, month and day. Write two member functions, one sets the date and one prints out the date. Page 6 of 6 Copyright Enoch Hwang 2009