lecture_28 - 1 lecture_28.txt 2010-12-01 19:21 David Nicol...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 lecture_28.txt 2010-12-01 19:21 David Nicol Introduction to C++--------------------- C++ grew out of C, pionoeered by Bjarne Stroustrup at Bell Labs (now Texas A&M) Provides OBJECT ORIENTED programming model Many features inherited from C, but then a number of new constructs We'll highlight some essential differences CLASSES------- A class generalizes a struct- a struct gathers logically related data together- a class gathers logically related data together AND allows one to-- control "who" can access that data-- provide functions SPECIFIC TO THE CLASS Example : A vector has direction and magnitude. In C we might have a structure struct VectorStruct { double angle; double length; }; typedef struct VectorStruct vector; and we might have functions that scale vectors, or add vectors void scaleVector(vector *v, double alpha) { v->angle *= alpha; } vector addVectors(vector a, vector b) { vector c; double ax = a.length*cos(a.angle); double bx = b.length*cos(b.angle); double ay = a.length*sin(a.angle); double by = b.length*sin(b.angle); double cx = ax+bx; double cy = ay+by; c.length = sqrt(cx*cx+cy*cy); c.angle = arcos( cx/c.length ); return c; } In C++ we could bundle this all together------------------------------------------------------------------------------------ #include <iostream> // used for IO using namespace std; // creates textual container for variables and functions class vector { // class is generalization of struct // class constructors---do initialization public: vector(double a, double l) { angle = a; length = l; } vector() { angle =0.0; length = 0.0; } // notice "overloading" of function name // functions tied to variables of this class double getAngle() { return angle; } 2 lecture_28.txt 2010-12-01 19:21 David Nicol double getLength() { return length; } void scale(double a) { length *= a; } // extend or contract vector vector add(vector b) {...
View Full Document

This note was uploaded on 08/29/2011 for the course ECE 265 taught by Professor Kosbar during the Fall '09 term at Missouri S&T.

Page1 / 5

lecture_28 - 1 lecture_28.txt 2010-12-01 19:21 David Nicol...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online