Docsity

Lecture Slides for C++ | Data Structures | CMSC 420, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Lecture Slides for C++ | Data Structures | CMSC 420 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

What you should know

about C++

Lecture 1.5: CMSC 420

C++

  • Almost every C program is a C++ program.
  • That was an explicit goal of the design of C++.
  • Why are we covering C++?
    • Important language, nearly the^ lingua franca^ of computer science.
    • Languages like Python, Ruby, C#, Java are used more and more, but there’s still a huge C++ code base.
    • Many features of C++ are designed to support abstract data types and general data structures.
  • If you want, think of C++ as C with some features to make

your life easier.

Major Addition to C: Classes

class IntStack { public : IntStack( int max=100); ~IntStack(); void push( int ); int pop(); protected : int * stack; int top; public : int size(); }; Constructor has same name as class. Called when object is created. Destructor called ~ClassName; Called when object is deleted. Functions declared inside class are called member functions. They can access the data in the class. protected things can only be seen by subclasses; public things can be seen by everyone; private things can only be seen by this class.

Comparison to Java

class IntStack { public : IntStack( int max=100); ~IntStack(); void push( int ); int pop(); protected : int * stack; int top; public : int size(); }; class IntStack { public IntStack( int max) { stack = new int [max]; top = -1; } protected void finalize() { // nothing to do here } public void push( int k) { stack[++top] = k; } // ... protected int [] stack; protected int top; }; C++ Java

Classes – function implementations

IntStack::InStack( int max=100) { stack = new int [max]; top = -1; } IntStack::~IntStack() { delete [] stack; } void IntStack::push( int k) { top++; stack[top] = k } Syntax “new TYPE[SIZE]” creates a new array of length SIZE containing objects of type TYPE. Member functions can access class variables without any special syntax. “delete [] X” frees the memory for the array pointed to by X. To free a single object, omit the “[]”.

  • Stored as a local variable:
  • Stored on heap:

Classes – Example use

IntStack S(10000); S.push(10); S.push(12); } // ~InStack automatically called { IntStack * S = new IntStack(10000); S->push(10); S->push(12); delete S; }

I/O

  • You can use all C functions for input or output.
  • OR you can use C++^ streams^ (but don’t mix the two). - Standard streams: - stdin^ is called^ cin. - stdout^ is called^ cout.
  • Reading values from^ stdin^ (whitespace is ignored) :
  • Writing same to^ stdout : int i; float f; string s; cin >> i >> f >> s; cout << i << “ “ << f << “ “ << s << endl;

Strings

#include using namespace std; int main() { string s = “abcdefg”; string s2 = “cat”; cout << s[0] << s[2] << endl; // “ac” s.append(s2); cout << s << endl; // “abcdefg cat ” s.insert(2, s2); cout << s << endl; // “ab cat cdefgcat” cout << s.find(“tcd”); // 4 } http://www.sgi.com/tech/stl/basic_string.html A “make it work” instruction.

References – Pass by Reference

  • References are most commonly used to pass variables to

functions so that the function can change them:

  • Common case: want to pass a big object to a function, so

don’t want to copy, but want to be sure object isn’t

changed:

int add1(int * x) { (x) += 1; } / C-style / int add1(int & x) { x += 1; } // C++-style int foo( const Image & pict) {/...*/}

Operator Overloading

struct Point { int x, y; Point( int xx, int yy) { x=xx; y=yy; } }; bool operator ==( const Point & A, const Point & B) { return A.x == B.x && A.y == B.y; } int main() { Point p1(10, 4); Point p2(-12, -100); Point p3(10, 4); if (p1 == p2) { /* FALSE / } if (p1 == p3) { / TRUE */ } }

Minor Differences From C

  • Comments: // until the end of line (in addition to /* */) - bool^ is a built-in type, with values^ true^ and^ false. - namespace s: collect functions into groups. Probably you’ll

only use to say:

- Doesn’t support^ int^ foo(a,b)^ int^ a,^ int^ b { /* ... */ }

syntax.

- Function arguments can have default values: int foo( int a=0) { /* ... */ }.

  • “g++” instead of “gcc”, .cc extension instead of .c using namespace std;

Other differences

  • Templates: write code to work with any type of variables.
    • In practice, can be hard to get to work right.
    • Won’t need for this class (but can use if you want). - const^ modifier means variable cannot be changed.
    • Good idea in theory, except that const “infects” everything it touches
    • e.g. can’t pass a const variable to any function that hasn’t explicitly labeled the parameter const.
    • Just as well to avoid using it.