CISC 3142 — Lab #3

CISC 3142
Programming Paradigms in C++
Lab #3
Some More Basics — CodeLab Exercises

How to Develop and Submit your Labs

Lab 3.1 — Some CodeLab Exercises

Unlike most of the other labs that consist of small programs or class definitions (designed specifically for 3142), this lab is composed of a bunch of smaller code fragments from the standard CodeLab inventory, dealing with pointers, fairly trivial class declarations and definitions, and some simple examples of inheritance (without polymorphism). Your existing knowledge of Java class definitions (and the examples in the lecture notes) should get you most of the way through the class exercises; there's just a bit of code organization that we alluded to, that we'll elaborate upon here.

Declaration vs Definition

We've discussed this to some extent: a declaration is simply an announcement of the existence of a class, variable, or function, while a definition provides the details (the set of data members and member functions for classes, a possible initial value for a variable, and the function body for a function). When specifying a class, there are several alternatives as to where to place the various components of the class definition (i.e., the class declaration + the member function definitions). We'll address this in more detail in the chapter in Chapter 15. For the moment, we just give crude guidelines to allow you to do these simple exercises. (when doing the CodeLab exercises, you will not have to deal with files or filenames, but the following presentation will make the terminology understandable).

For illustrative purposes, we'll focus on a class named Simple with a single int data member named val, a default constructor, and a single member function name getVal.

Function Definitions within the Class Declaration in the Header (.h) File
// Simple.h

class Simple {
public:
	Simple(int val) : val(val) {}		// outside val is data member; inside val is parameter
	int getVal() {return val;}		// inline function
private:
	int val;						// data members are part of class declaration
};			
Function Definitions Separated from the Class Declarations
// Simple.h (interface/header) file
class Simple {
public:
	Simple(int val);			// headers only
	int getVal();
private:
	int val;						// data members are part of class declaration
};			
//Simple.cpp (implementation/source) file

Simple::Simple() : val(val) {}
int Simple::getVal() {return val;}
Mixtures of the Above

The Class Definition Exercises in CodeLab

static (Class) Members

  • Just a whole bunch of exercises to familiarize you with the basic syntax and semantics.

Lab 3.2 — A Class Template

Code a template named Simple that is based on the Simple class illustrated above, with the following additions:
  • a setVal method
  • a print method that prints the val data member to cout

The Demo Application

You should write an app (i.e., a file containing a main that tests your template, i.e., instantiates several Simple objects of different types and plays with them. (Lab 3.2.2 of CodeLab asks you to submit something of this sort). I've supplied you with the app code for the int instantiation of Simple — you can get it via the link at the bottom of the page.

File Organization and Compiling

I was originally going to have you place the template and the app in the same file (i would mean you don't have to know about file names or do any #includes), that approach is quite artificial, and doing it right is barely any extra work:
  • Place the template in a file named Simple.h,
  • The only restriction on the name of the app file (the file containing your main) is that it should end in .cpp (For example, I used app.cpp)
  • To access the template from your app, make sure you have a #include <Simple.h>
If you are using an IDE, simply adding the above two files to your project should be sufficient to build the executable. If you are compiling from the command line, you can either write g++ *.cpp or g++ app.cpp (assuming your app was source file also named app.cpp

Submitting to Codelab

This lab is broken up in CodeLab into two separate exercises:
  • 3.2.1 — the Simple template
  • 3.2.2 — an app that uses the template. The app you are to submit here should produce the same output used by my app in 3.2.1 that tests your template, i.e.:
    		Working with int
    		After initialization to 10 si: 10
    		After setVal(23) si: 23
    		Calling print: 23
    
    		Working with double
    		After initialization to 10.5 sd: 10.5
    		After setVal(23.5) sd: 23.5
    		Calling print: 23.5
    
    		Working with string
    		After initialization to "Hello" ss: Hello
    		After setVal("Goodbye") ss: Goodbye
    		Calling print: Goodbye
    		
    • A simple example of a class template, to get you warmed up for more interesting stuff later

Lab 3.3 — A Pair of Function Templates

Code a pair of function templates, each one having a single type parameter representing the element type of an array:
  • reverse — accepts an array and its size, and reverses it (returns void)
  • print — accepts an array and its size, and prints it out in the form {e1, e2, ...., en-1} (returns void)
Place both of these in the file reverse.h

The Demo Application

You should write an app (i.e., a file containing a main that tests your templates, i.e., calls them with arrays of varying types. (Again as with the previous lab, Lab 3.3.2 of CodeLab asks you to submit something of this sort).

File Organization and Compiling

The organization is basically the same as in 3.2, except the file containing the function templates should be named reverse.h

Submitting to CodeLab

This lab is also broken up in CodeLab into two separate exercises:
  • 3.3.1 — the function templates
  • 3.3.2 — an app that uses the templates. The app you are to submit here should produce the same output used by my app in 3.3.1 that tests your templates, i.e.:
    		Code an app for the templates of 3.3.1, and which produces the following output:
    
    		Playing with int
    		Before: {10, 20, 30, 40, 50}
    		After: {50, 40, 30, 20, 10}
    
    		Playing with double
    		Before: {10.5, 20.5, 30.5, 40.5, 50.5, 60.5}
    		After: {60.5, 50.5, 40.5, 30.5, 20.5, 10.5}
    
    		Playing with string
    		Before: {ABC, DE, FGHI, JK}
    		After: {JK, FGHI, DE, ABC}
    		
    • A simple example of a class template, to get you warmed up for more interesting stuff later

Code Used in this Lecture