This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
haas:summer2017:cprog:projects:oop0 [2017/07/15 16:32] – created wedge | haas:summer2017:cprog:projects:oop0 [2017/08/01 14:05] (current) – wedge | ||
---|---|---|---|
Line 12: | Line 12: | ||
=====Background===== | =====Background===== | ||
- | When we look at a problem logically, we can often break it down into discrete parts, each of which is simple to solve, and when cooperating together, can help solve the big task at hand. Familiarity with C or most shell scripting | + | When we have a task or problem at hand we wish to solve, there are often many different ways of solving it. If the task is labor-intensive, |
+ | |||
+ | When we look at a problem logically, we can often break it down into discrete parts, each of which is simple | ||
This is all fine and dandy. In fact, many problems are best solved by using imperative techniques. (Best tool for the job sort of thing). However, there exist other paradigms, such as " | This is all fine and dandy. In fact, many problems are best solved by using imperative techniques. (Best tool for the job sort of thing). However, there exist other paradigms, such as " | ||
Line 18: | Line 20: | ||
So what IS Object-Oriented programming? | So what IS Object-Oriented programming? | ||
+ | ====Abstraction==== | ||
Abstraction can be defined as " | Abstraction can be defined as " | ||
<WRAP round warning box> | <WRAP round warning box> | ||
+ | ====Terminology==== | ||
With this abstracted approach to problem solving, we've got some new terminology that must be presented: | With this abstracted approach to problem solving, we've got some new terminology that must be presented: | ||
- | * encapsulation- to package data and/or operations into a single well-defined programming unit. | + | * **__encapsulation__**- to package data and/or operations into a single well-defined programming unit. |
- | * data abstraction- the property of an ADT that allows you to work with the data elements without concern for how the data elements are stored inside the computer or how the data operations are performed inside the computer. | + | * **__data abstraction__**- the property of an ADT that allows you to work with the data elements without concern for how the data elements are stored inside the computer or how the data operations are performed inside the computer. |
- | * class- an interface that defines the behavior of its objects. (in C++, it is a syntactical unit that describes a set of data and related operations that are common to its objects). | + | * **__class__**- an interface that defines the behavior of its objects. (in C++, it is a syntactical unit that describes a set of data and related operations that are common to its objects). |
- | * object- an instance, or specimen, of a given class. An object of a given class has the attributes and behavior described by the class that is common to all objects of the same class. | + | * **__object__**- an instance, or specimen, of a given class. An object of a given class has the attributes and behavior described by the class that is common to all objects of the same class. |
- | So the purpose here is organization and abstraction. We can package our data and look at our data in a manner independent of what a computer may do with it (for example, if we want to look at " | + | ====Organization==== |
+ | The purpose here is organization and abstraction. We can package our data and look at our data in a manner independent of what a computer may do with it (for example, if we want to look at " | ||
- | Organization of data has always played an important part in things. You run an office, you need to keep your paperwork organized. Filing cabinets can be quite useful for this purposes... and can be subdivided into units such as file folders, drawers, etc. Here we are looking at things on the top level- abstracting away things like ink-printed papers, 11x8.5" | + | Organization of data has always played an important part in things. You run an office, you need to keep your paperwork organized. Filing cabinets can be quite useful for these purposes... and can be subdivided into units such as file folders, drawers, etc. Here we are looking at things on the top level- abstracting away things like ink-printed papers, 11x8.5" |
- | In programming languages, such as C++, the class is an important packaging mechanism that lets you organize your data. For those who are familiar with the "**struct**" in C, a class in C++ (or Java) is going to seem very similar. And very appropriately so. | + | In programming languages, such as C++, the class is an important packaging mechanism that lets you organize your data. For those who are familiar with the " |
The structure of a class is as follows: | The structure of a class is as follows: | ||
< | < | ||
- | Class | + | Class |
- | | + | Function Members |
- | Data Members | + | Data Members |
</ | </ | ||
- | The " | + | The " |
====Member Functions==== | ====Member Functions==== | ||
+ | The " | ||
+ | |||
So a class is made up of the various function members and data members. Think of this as the object you are creating- each object has properties, both in values that must be set, as well as the ability to manipulate and view those values. This is exactly the sort of stuff you can do with classes in C++. | So a class is made up of the various function members and data members. Think of this as the object you are creating- each object has properties, both in values that must be set, as well as the ability to manipulate and view those values. This is exactly the sort of stuff you can do with classes in C++. | ||
====Elements of syntax==== | ====Elements of syntax==== | ||
- | This whole idea of classes and abstraction may be worthwhile, but ultimately, when dealing with a computer, we've still got to take into account some level of detail. To make the computer happy, we must provide mechanisms for starting up and shutting down an instance of a class. The terms used are "**constructor**" and "**destructor**". Basically- a constructor is a set of instructions that are called when you are creating an instance (or copy) of this class. A constructor will initialize data members to values, set conditions, etc. so that you can proceed to use the class. | + | This whole idea of classes and abstraction may be worthwhile, but ultimately, when dealing with a computer, we've still got to take into account some level of detail. To make the computer happy, we must provide mechanisms for starting up and shutting down an instance of a class. The terms used are " |
A destructor is called when you are finished with this instance of your class. It cleans up. | A destructor is called when you are finished with this instance of your class. It cleans up. | ||
====Message Passing==== | ====Message Passing==== | ||
- | The concept of " | + | The concept of " |
The idea behind message passing is to establish some means of communication (once again, the details of this communication link are abstracted away) and to pass the appropriate information through this link, irrelevant of how it is actually transferred or represented by the underlying hardware. | The idea behind message passing is to establish some means of communication (once again, the details of this communication link are abstracted away) and to pass the appropriate information through this link, irrelevant of how it is actually transferred or represented by the underlying hardware. | ||
In your programs, you will be performing message passing when dealing with your classes. | In your programs, you will be performing message passing when dealing with your classes. | ||
+ | |||
+ | ====Access Control==== | ||
+ | Using classes we also have the " | ||
+ | |||
+ | Public access control is just that-- available for all to see. Private access means only accessible to the other class members WITHIN that particular class. | ||
+ | |||
+ | It is sometimes preferable for different levels of a program to be programmed by different people. Large database applications can often have several different groups of people working on different tasks. | ||
+ | |||
+ | Instead of giving every programmer the entire program to do with as they please, each group can work on their specific component, and release it to the other groups to use in the implementation of their component. Distributing such things in binary form is not uncommon. Look at the world of proprietary software, where no source code is available. You must be able to use their binary code and any documentation the vendor may provide in order to design your solution. You cannot view the source code of the various function calls to see how they work. Instead, you must rely on the documentation, | ||
+ | |||
+ | In the Open Source world, lack of source code isn't as much of a problem, but the concepts of data access are still very important. A program may still have several components to it, and for security purposes the file-save module probably SHOULDN' | ||
+ | |||
+ | There are some additional access control attributes available: **protected** and **friend**. Protected access control is utilized more in inheritance, | ||
=====Programming in C++===== | =====Programming in C++===== | ||
Line 121: | Line 141: | ||
====hello2.cc==== | ====hello2.cc==== | ||
- | A strictly C++ version of the same thing (also how you'd likely see 99% of all C++ references on the planet portray this program): | + | A strictly C++ version of the same thing: |
<code c++> | <code c++> | ||
/* | /* | ||
* hello2.cc - a simple " | * hello2.cc - a simple " | ||
+ | */ | ||
+ | |||
+ | // include standard I/O stream functions | ||
+ | // | ||
+ | #include < | ||
+ | |||
+ | // main() function | ||
+ | // | ||
+ | int main() | ||
+ | { | ||
+ | std :: cout << " | ||
+ | return (0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Compare the similarities, | ||
+ | |||
+ | ====hello3.cc==== | ||
+ | A strictly C++ version of the same thing (also how you'd likely see 99% of all C++ references on the planet portray this program): | ||
+ | |||
+ | <code c++> | ||
+ | /* | ||
+ | * hello3.cc - a more C++-y and simple " | ||
*/ | */ | ||
Line 142: | Line 185: | ||
</ | </ | ||
- | Compare | + | Take specific note of the "using namespace" |
+ | |||
+ | This is but another form of abstraction, | ||
=====g++: The C++ compiler===== | =====g++: The C++ compiler===== | ||
Line 217: | Line 262: | ||
====Using the compiler==== | ====Using the compiler==== | ||
- | |||
To compile a single source file, you would do the following, just as how you've been using **gcc**: | To compile a single source file, you would do the following, just as how you've been using **gcc**: | ||
Line 228: | Line 272: | ||
The **-o** argument to **g++** indicates the name of the output file. Since there is only one file in this case, the compiler automatically performs the assembling and linking steps for us. | The **-o** argument to **g++** indicates the name of the output file. Since there is only one file in this case, the compiler automatically performs the assembling and linking steps for us. | ||
- | =====Shapes===== | + | =====rectangle class===== |
- | In the **shapes/** subdirectory of the CPROG Public Directory (**/ | + | In the **oop0/** subdirectory of the CPROG Public Directory (**/ |
<cli> | <cli> | ||
- | lab46:~$ cd ~/src/cprog | + | lab46: |
- | lab46:~/src/cprog$ cp / | + | lab46: |
- | lab46: | + | |
</ | </ | ||
Please read through this source code, compile, and run the program. Make sure you understand what is going on. | Please read through this source code, compile, and run the program. Make sure you understand what is going on. | ||
- | =====Questions===== | + | =====Program===== |
- | - What are the member | + | We've got the accessor |
- | - If dealing with a triangle, what are the formulas for perimeter | + | |
- | - With a trapezoid, what are the formulas for perimeter | + | |
- | =====Program 1===== | + | * implement the member functions (keeping any function parameter names identical to how you received them) |
+ | * duplicate the output code block in main(), adapting as appropriate for box2, box3, and box4. | ||
+ | * Modify the program by adding **triangle** and **trapezoid** classes. Be sure to define the appropriate member functions and member data. To avoid confusion, be sure to employ the scoping operator (::). Also be sure that each " | ||
- | We've got the accessor functions of **getLength()** and **getWidth()** for checking the current values of the particular rectangle' | + | =====Review of Compiling/ |
- | + | ||
- | * define and implement two new member functions- **setLength()** and **setWidth()** and adjust your output accordingly to prove that they do what they are supposed to do. | + | |
- | * Modify the program by adding **Triangle** and **Trapezoid** classes. Be sure to define the appropriate member functions and member data. To avoid confusion, be sure to employ the scoping operator (::). Also be sure that each " | + | |
- | * First make your changes to have everything in one big source file. Make sure this compiles and works as expected. Once you have this done, break apart your code into separate files (.h and .cc files), breaking all the code up as efficiently as you see fit (perhaps per each shape). Submit both the single (all-in-one) file, and the multi-file versions of your programs. | + | |
- | + | ||
- | + | ||
- | ======Access Control====== | + | |
- | Using classes we also have the " | + | |
- | + | ||
- | It is sometimes preferable for different levels of a program to be programmed by different people. Large database applications can often have several different groups of people working on different tasks. | + | |
- | + | ||
- | Instead of giving every programmer the entire program to do with as they please, each group can work on their specific component, and release it to the other groups to use in the implementation of their component. Distributing such things in binary form is not uncommon. Look at the world of proprietary software, where no source code is available. You must be able to use their binary code and any documentation the vendor may provide in order to design your solution. You cannot view the source code of the various function calls to see how they work. Instead, you must rely on the documentation, | + | |
- | + | ||
- | In the Open Source world, lack of source code isn't as much of a problem, but the concepts of data access are still very important. A program may still have several components to it, and for security purposes the file-save module probably SHOULDN' | + | |
- | + | ||
- | =====Questions===== | + | |
- | + | ||
- | In the **incomplete/ | + | |
- | + | ||
- | This file implements a simple class and a **main()** function. It doesn' | + | |
- | + | ||
- | Please copy this file into your own **~/ | + | |
- | + | ||
- | < | + | |
- | lab46:~$ cd ~/ | + | |
- | lab46: | + | |
- | lab46: | + | |
- | </ | + | |
- | + | ||
- | Read through it, and answer the following questions: | + | |
- | + | ||
- | * What errors do you receive when trying to compile? | + | |
- | * What is the reason the program does not compile in its original state? | + | |
- | * How can you modify the class to fix the compilation problem (and have the program work as expected)? | + | |
- | * Now assume you cannot modify the class, how could you fix the problem by changing code in **main()** and referencing the original class definition? | + | |
- | * Why is it a good thing to not have direct access to the private member data of a class? Explain your reasoning. | + | |
- | * Is there any reason you'd prefer to have direct access to all your data? If so, would the problem be better suited for a non Object-Oriented solution? Justify your answer. | + | |
- | + | ||
- | =====Program 2===== | + | |
- | Along with your responses to the above questions, please submit a working version of the **tryme.cc** program as corrected by the specifications of modifying only **main()** (leave the original class implementation intact). | + | |
- | ======Review of Compiling/ | + | |
Just to review the compilation/ | Just to review the compilation/ | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
lab46: | lab46: | ||
</ | </ | ||
Line 300: | Line 302: | ||
< | < | ||
- | g++ -o BINARY_FILE SOURCE_FILE | + | g++ -Wall --std=c99 |
</ | </ | ||
Line 313: | Line 315: | ||
</ | </ | ||
- | =====Optimization===== | + | =====Submission===== |
- | If you find yourself experiencing " | + | To successfully complete this project, the following criteria must be met: |
- | Compiler optimizations invoke additional functionality present in the compiler that can do some alterations of your compiled code, reordering things for more efficiency, and even correcting aberrant behavior | + | * Code must compile cleanly (no warnings or errors) |
+ | * Use the **-Wall** and **--std=c99** flags when compiling. | ||
+ | * Output must be correct, and resemble the form given in the sample output above. | ||
+ | * Code must be nicely and consistently indented | ||
+ | * Code must utilize the algorithm presented above | ||
+ | * Code must establish and utilize the functions described above | ||
+ | * Code must be commented (and those comments relevant) | ||
+ | * Track/ | ||
+ | * Submit a copy of your source code to me using the **submit** tool. | ||
- | To use compiler optimizations, | + | To submit |
- | + | ||
- | ^ option | + | |
- | | -O0 | no optimization (this is the default, it happens if you don't specify anything) | + | |
- | | -O | reduce code size and execution time, plus some non-expensive optimizations | + | |
- | | -O1 | same as -O | | + | |
- | | -O2 | optimize more. Compile time increases for the result of better code and execution | + | |
- | | -O3 | yet more optimizations. Long compile time, perhaps more efficient code | | + | |
- | | -Os | optimize for size. Uses a lot of -O2 optimizations so long as it does not impact code size | | + | |
- | + | ||
- | So, if you'd like to compile your code with level 1 optimizations: | + | |
- | + | ||
- | < | + | |
- | g++ -O1 -o BINARY_FILE SOURCE_FILE | + | |
- | </ | + | |
- | + | ||
- | As your programs get bigger and more complex, the utilization of compiler optimizations **can** make a significant impact on the resulting performance of your program. For most of the stuff we're doing now, you're not likely | + | |
- | ======Copying files to your submit directory====== | + | |
- | As you write your code, hopefully you've developed | + | |
- | + | ||
- | But, in order to complete your tasks, you've been requested to place it in your **~/src/submit** | + | |
- | + | ||
- | What to do?! | + | |
- | + | ||
- | We'll simply make a **copy** of your code! Assuming we're working with a source file called **myprog.cc** in our **~/ | + | |
- | + | ||
- | To do that we use the **cp** | + | |
<cli> | <cli> | ||
- | lab46: | + | $ submit |
- | lab46:~/ | + | Submitting cprog project " |
- | </cli> | + | -> oop0.cc(OK) |
- | We can then hop over to our submit directory and add/commit it: | + | SUCCESSFULLY SUBMITTED |
- | + | ||
- | < | + | |
- | lab46: | + | |
- | lab46: | + | |
- | contact.info | + | |
- | lab46: | + | |
- | Added | + | |
- | lab46: | + | |
- | ... | + | |
</ | </ | ||
- | ======Submission====== | ||
- | All questions in this assignment require an action or response. Please organize your responses into an easily readable format and submit the final results to your instructor per the appropriate methods. | ||
- | |||
- | Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy. | ||
- | |||
- | When complete, questions requiring a response can be electronically submit using the following form: | ||
- | |||
- | < | ||
- | \\ | ||
- | |||
- | Additionally, | ||
- | |||
- | * placement of the code you created to solve this task in your **~/ | ||
- | * name that file: **task6.cc** | ||
- | * addition/ | ||
- | * create a directory **task6/** in your **~/ | ||
- | * place your multi-file implementation of task6.c in this **task6/** directory | ||
- | * addition/ | ||
- | |||
- | As always, the class mailing list and class IRC channel are available for assistance, but not answers. | ||
+ | You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. |