(Like you) , I'm a physicist, not a computer scientistNo stratospheric abstractions, complicated data structures or algorithms (you can look those up). But at some point, one has to learn how to code C++ programs.C++ is similar to physics (and any non-trivial subject) in that it is easiest learned helically - with details and subtleties revealed as time goes on, and overgeneralizations qualified. Helical.Success of the class will, among other things, rely on your own interaction, for which we've provided newsgroups and email (and encouragement to discuss exercises and projects as much as you wish, subject to the "indep. work" caveat.)This isn't computational physics And this isn't a computer class either - something in between. You will learn the nitty-gritty of coding in C++, but we will have the time to cover "higher-level" concepts such as virtual functions, multiple inheritance, etc. Bottom-line goal is that you end up being able to:
High-level features
#define VecSize 20
struct FixedVector {
double data[VecSize];
};
struct Vector {
double *data;
int dim;
}
main()
{
...
struct FixedVector a,b;
struct Vector x;
int i;
// could move this to a function
for (i=0; i < VecSize; ++i)
a.data[i] = b.data[i];
/* in defs.h */
double foo(int x, double y); // declaration
/* in prog.c */
#include "defs.h"
main()
{
double x=4;
int y=1;
double z = foo(x,y); /* error!!!! */
C++ analogy with operating system: several users, several "levels" of user - from novice to sys. admin, all need protection - from self, from others; several levels of program (driver-level to application). All need modularity and level-modularity for system design
What is C++
Blurb
Stroustrup: "Better organization of programs" over C, where
"computation ... (is) considered a problem solved by C." (The Design and Evolution of C++)
Classes and objects
Addition of a Class - a user-defined type, almost as nice as built-in
types, includes operators, type conversion, ...
Aside on "users"
object - a variable (or const) of type Class
class Vector {
private:
Index dim_; // dimension of vector
Data *data; // pointer to where data is stored
public:
Vector(Dim dim); // allocate only
Vector(Index dim, Data val); // allocate&initialize data[i]=val
Vector(const Vector& rhs); // copy constructor
~Vector();
// assignment operators
const Vector& operator=(const Vector& rhs);
const Vector& operator+=(const Vector& rhs);
// member data access
const Data& operator[](Index position) const; // read-only
Data& operator[](Index position); // write-access
Index dim() const;
// arithmetic
const Vector operator+(const Vector& rhs) const;
Data dot(const Vector& rhs) const; // dot product (++++... metric)
Data operator*(const Vector& rhs) const; //
Data operator*(const Vector& rhs) const; //
// other operations
friend ostream& operator<<(ostream& output, const Vector& rhs);
friend istream& operator>>(istream& input, Vector& rhs);
};
Comments:
Vector x( Dim(4) );
Vector y( Dim(4), Data(3.0));
Vector z = y;
Vector z= y+x;* "natural" output/input possible
z += z+y;
double dotprod = z*y;
cout << "Z vector is "<< z << ", while y is "<< y <<"\n";
cout <<"enter z: "; cin >>z; cout <<" You entered: "<<z<<"\n";
class ball: public Vector {Other nice things:
// stuff //
}
template <class Function, class Number>
Number integrate(Function f, Number a, Number b)
{
// integrate function f from a to b
}
template <class T> class stackFinally, C++ was not designed to be the monolithic answer - should be
{
private:
T* t1; //beginning of stack
T* tn; // end of stack
public:
stack(int s);
~stack();
void push(T a);
T pop();
}
reasonably easy to hook up to C and Fortran (and Mathematica):
extern "C" {
// everything in here has as C-type interface
}
C++ Primer, 2nd edition, Stanley Lippman. Available from Schuler's
Handouts on Unix,...
htmls:
davechao@msupa.pa.msu.edu (usually I log onto cteq07.pa.msu.edu)
news:msu.phy.class.405,
ftp://ftp.pa.msu.edu/ ... to be decided
unix/vms:
class will be "run" off unix (pads1.pa.msu.edu)
you can run on msupa.pa.msu.edu (VMS), will become significantly
more UNIX-like when POSIX is installed (soon ).
editor suggested - Emacs (or vi if you must )
compiler will be GNU gcc/g++ 2.7.2
Information I need
[ Phy 405/905 Home Page] [ Lecturer ]