Table of Contents

Part 3

Entries

November 7th, 2011

Worked through some Array programs that we made in Structured Orientation and Problem Solving.

November 15th, 2011

started Purpose: A simple program demonstrating the use of pointers. #include <iostream> using namespace std; int main() { declare an integer and a float variable

int IntNum; 
float FloatNum;

// declare integer and float pointers
int *pIntNum;
float *pFloatNum;

// initialize the integer and float variables
IntNum = 10;
FloatNum = 12.34;

// store addresses in pointers
pIntNum = &IntNum;
pFloatNum = &FloatNum;

November 16th, 2011

// print out the original values
      cout << "Before increment: " << endl;
cout << "\t IntNum is: " << IntNum << endl;
cout << "\t FloatNum is: " << FloatNum << endl;

// note that we need to dereference a pointer in order
// to extract the value it contains.
cout << "\t pIntNum contains: " << *pIntNum << endl;
cout << "\t pFloatNum contains: " << *pFloatNum << endl;
// increment values of the integer and float variables 
(*pIntNum)++;  // dereference and then increment
(*pFloatNum)++;
// print out the values after increment
      cout << "After increment: " << endl;
cout << "\t IntNum is: " << IntNum << endl;
cout << "\t FloatNum is: " << FloatNum << endl;
cout << "\t pIntNum contains: " << *pIntNum << endl;
cout << "\t pFloatNum contains: " << *pFloatNum << endl;
return 0;

}

November 29, 2011

Worked on Opus, studied the point program a little bit (used example from a website that guided me through the process)

cprog Topics

Function Overloading

You overload a function name f by declaring more than one function with the name f in the same scope.

#include <iostream>
using namespace std;
 
void print(int i) {
  cout << " Here is int " << i << endl;
}
void print(double  f) {
  cout << " Here is float " << f << endl;
}
 
void print(char* c) {
  cout << " Here is char* " << c << endl;
}
 
int main() {
  print(10);
  print(10.10);
  print("ten");
}

Command-line arguments

In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.

int main ( int argc, char *argv[] )

“this” pointer

The type of the this pointer for a member function of a class type X, is X* const. If the member function is declared with the const qualifier, the type of the this pointer for that member function for class X, is const X* const

struct A {
  int a;
  int f() const { return a++; }
};

Inheritance

Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.

Polymorphism

Polymorphism is the ability for objects of different classes related by inheritance to respond differently to the same member function call

Operator overloading

Operator overloading is a special kind of Polymorphism which is defined by the programmer or the program itself.

Exception Handing

Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called handlers.

// exceptions
#include <iostream>
using namespace std;
 
int main () {
  try
  {
    throw 20;
  }
  catch (int e)
  {
    cout << "An exception occurred. Exception Nr. " << e << endl;
  }
  return 0;
}

Abstract Base Class

An abstract class is a class that is designed to be specifically used as a base class

class AB {
public:
  virtual void f() = 0;
};

Standard Template Library

Standard Template Library; it provides many of the basic algorithms and data structures of computer science

vector<int> v(3);            // Declare a vector of 3 elements.
      v[0] = 7;
      v[1] = v[0] + 3;
      v[2] = v[0] + v[1];          // v[0] == 7, v[1] == 10, v[2] == 17 

Templates

Function templates are special functions that can operate with generic types

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}

cprog Objective

Objective

know the difference between structures and classes

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.

The class is the template or plate of a program or code, while the structure is sort of like the meat and potatoes of a programs, they're both important to a program, but both very unique to one another.

Experiments

Experiment 1

Question

What will happen if you do not declare a variable in a pointer program?

Resources

Using google I collected a guide to making a pointer program with undefined variables.

Hypothesis

The program will not run because the variables are undefined and the program will not know what to do.

Experiment

Using the program,

#include <iostream>
using namespace std;

int main()
{
	// declare an integer and a float variable
	int IntNum; 
	float FloatNum;
	
	// declare integer and float pointers
	int *pIntNum;
	float *pFloatNum;
	
	// initialize the integer and float variables
	IntNum = 10;
	FloatNum = 12.34;
	
	// store addresses in pointers
	pIntNum = &IntNum;
	pFloatNum = &FloatNum;

	// print out the original values
        cout << "Before increment: " << endl;
	cout << "\t IntNum is: " << IntNum << endl;
	cout << "\t FloatNum is: " << FloatNum << endl;
	
	// note that we need to dereference a pointer in order
	// to extract the value it contains.
	cout << "\t pIntNum contains: " << *pIntNum << endl;
	cout << "\t pFloatNum contains: " << *pFloatNum << endl;

	// increment values of the integer and float variables 
	(*pIntNum)++;  // dereference and then increment
	(*pFloatNum)++;

	// print out the values after increment
        cout << "After increment: " << endl;
	cout << "\t IntNum is: " << IntNum << endl;
	cout << "\t FloatNum is: " << FloatNum << endl;

	cout << "\t pIntNum contains: " << *pIntNum << endl;
	cout << "\t pFloatNum contains: " << *pFloatNum << endl;

	return 0;
}

Data

The program was put into Putty and compiled. The program did not run, it gave the following results, ./pointer.c: In function 'main': ./pointer.c:26: error: 'cout' undeclared (first use in this function) ./pointer.c:26: error: (Each undeclared identifier is reported only once ./pointer.c:26: error: for each function it appears in.) ./pointer.c:26: error: 'endl' undeclared (first use in this function)

Analysis

Based on the data collected:

Conclusions

Variables need to be defined in any program you write in order to operate properly