CS152
Chris Pollett
Mar. 30, 2009
Which of the following would produce a side-effect? (The function call is the statement under consideration).
#include <iostream>
using namespace std;
/*use namespace for input/output
syntax for namespace is
namespace MyName
{
}
Can use scope resolution MyName::
to refer to things in a namespace
*/
class MyContainer
/*
the syntax for subclasses
is class MyClass : Parent {};
can be comma separated list
of parent classes.
use virtual to say a function
can be overriden
*/
{
private:
int *myArray;
int size;
public:
MyContainer(int num, int length);
~MyContainer();
void printArray();
}; // notice the semi-colon
/* typically the class would be in a .h
file and the following
would be in a .cpp file
*/
MyContainer::MyContainer(int num, int length)
{
myArray = new int[length];
for(int i=0; i < length; i++)
{
myArray[i] = num;
}
size = length;
}
MyContainer::~MyContainer() //destructor
{
delete[] myArray;
}
void MyContainer::printArray()
{
for(int i = 0; i < size; i++)
{
cout << myArray[i];
// cout is C++ way of printing things.
}
cout << endl; // a newline
}
int main()
{
MyContainer test(3, 7);
// allocates on the stack
test.printArray();
MyContainer *test2 = new MyContainer(4,20);
test2->printArray();
// allocates on the heap.
return 0;
}
// To compile: g++ filename.cpp
void my_class(int msg_code, ...);
void my_class(int msg_code, ...)
{ static int my_field1; //... other fields
switch(msg_code) {
case method1:
break;
...
default:
}
}