Lecture 9-2 notes

Lecture 9-2 notes - { pos = 0; //generates pointer history...

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

View Full Document Right Arrow Icon
#include <iostream> #include <string> using namespace std; class Target //only difference between struct and class is struct defaults public and class defaults private { public: //means any other part of program can use Target(); //constructor . puts into valid state void init();//not needed anymore then lol void move(char dir); int position(); void replayHistory(); private: //if private then only parts of program that can use it are init move replayhistory //Invariant: history only contains only Rs and Ls //pos equals Rs - Ls int pos; string history; }; class string { public: string(); string(const char* text); string(int n, char ch); //basically can have more than one constructor int Target::position() { //return this->pos; return pos; //either work } void Target::Target()
Background image of page 1

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

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

Unformatted text preview: { pos = 0; //generates pointer history = ""; } void Target::move(char dir) { switch (dir) { case 'R': case 'r': pos++; break; case 'L': case 'l': pos--; break; } this->history += toupper(dir): } t: pos:0 history: "RL" t2: pos:-1 history "L" int main() { Target t; t.init(); //in order to make sure that it is properly initialized, change init to target t.move('R'); Target t2; t2.init(); t2.move('L'); t.move('L'); //All Errors!: //init(); //init(t); //init(&t); } int main() { Target t; t.pos = 0; t.history = ""; t.pos++; t.history += '$'; Target t2; t2.pos = 0; t2.history = ""; t2.pos--; t2.history += 'L'; t.pos--; t.history += 'L'; } u can write strings like string t("Hello");...
View Full Document

This note was uploaded on 11/10/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 2

Lecture 9-2 notes - { pos = 0; //generates pointer history...

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

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