CS 455 Lecture 1 – Meta and Learn C++ in 75 Minutes
Agenda
- meta
- C++ review
TODO
- Read the syllabus
- Complete the lesson I send out later today via email
About you
- Your name?
- Last unassigned book you read?
- Where are you from?
- If not computers, what?
This course…
- is about interactive 3-D graphics
- uses OpenGL
- has one long-running project
- is archived on this blog
- is flipped
- is folded
- has a presentation project
- may or may not have a final
C++
- doesn’t have to be object-oriented (add a main)
- is compiled (I’ll use g++, we’ll use Visual Studio in class)
- main must return a number (0 on success)
- flow control looks the same as Java (except non-booleans allowed in condition)
- bool, not boolean
- namespaces vs. packages
- I/O using iostream
- call-by-value vs. call-by-address vs. call-by-reference
- why pointers and references?
- allow functions to reference caller memory without much transactional overhead
- pass parameters more cheaply than call-by-value
- const reference everything you can
- two ways to ask for memory
- declaration (when you know memory needs ahead of time)
- allocation (when you only know needs at runtime)
- delete or delete[] allocated memory
- classes end in semi-colons
- public and private sections instead of modifiers
- interface goes in *.h header
- implementation goes in *.cpp body
- a Vector3 class
- constructors
- deconstructor
- getX, getY, getZ
- const
- getLength
- normalize
- operator[] (no array bounds checking)
- operator+
- operator<<
- operator+= (on you)
first.cpp
#include <iostream>
void f(int *a) {
std::cout << "a1: " << *a << std::endl;
*a = 7;
std::cout << "a2: " << *a << std::endl;
}
void g(int &a) {
std::cout << "a4: " << a << std::endl;
a = 8;
std::cout << "a5: " << a << std::endl;
}
int main(int argc, char **argv) {
int a = 6;
bool isOff = true;
std::cout << a << std::endl;
if (a) {
std::cout << "oh yeah!" << std::endl;
} else {
std::cout << "boo!" << std::endl;
}
f(&a);
std::cout << "a3: " << a << std::endl;
g(a);
std::cout << "a6: " << a << std::endl;
int *nums = new int[10];
nums[10] = 100;
std::cout << "nums[10]: " << nums[10] << std::endl;
2[nums] = 3;
std::cout << "nums[2]: " << nums[2] << std::endl;
delete[] nums;
int *num = new int(10);
delete num;
return 0;
}
Vector3.h
#ifndef VECTOR3_H
#define VECTOR3_H
class Vector3 {
public:
Vector3();
Vector3(float x, float y, float z);
~Vector3();
float GetLength();
float& operator[](int index);
/* float operator+=(int index); */
private:
float xyz[3];
};
#endif
Vector3.cpp
#include <cassert>
#include <cmath>
#include "Vector3.h"
/* ------------------------------------------------------------------------- */
Vector3::Vector3() {
xyz[0] = xyz[1] = xyz[2] = 0.0f;
}
/* ------------------------------------------------------------------------- */
Vector3::Vector3(float x, float y, float z) {
xyz[0] = x;
xyz[1] = y;
xyz[2] = z;
}
/* ------------------------------------------------------------------------- */
Vector3::~Vector3() {
}
/* ------------------------------------------------------------------------- */
float Vector3::GetLength() {
return sqrtf(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2]);
}
/* ------------------------------------------------------------------------- */
float& Vector3::operator[](int index) {
assert(index >= 0 && index <= 2);
return xyz[index];
}
/* ------------------------------------------------------------------------- */
vecfun.cpp
#include <iostream>
#include "Vector3.h"
int main(int argc, char **argv) {
Vector3 v(3.0f, 4.0f, 5.0f);
std::cout << "v.GetLength(): " << v.GetLength() << std::endl;
v[0] = 18.0f;
std::cout << "v[0]: " << v[0] << std::endl;
std::cout << "v[1]: " << v[1] << std::endl;
std::cout << "v[2]: " << v[2] << std::endl;
/* std::cout << "v[17]: " << v[17] << std::endl; */
/* std::cout << "v.GetX(): " << v.GetX() << std::endl; */
/* std::cout << "v.GetY(): " << v.GetY() << std::endl; */
/* std::cout << "v.GetZ(): " << v.GetZ() << std::endl; */
}
Haiku
“Impose no morals”
C++ wags no finger
It pulls the trigger
C++ wags no finger
It pulls the trigger