1 / 21

An Introduction to C++

CS225: Data Structures and Software Principles Lectures 2, 3, and 4. An Introduction to C++. TA Info. Name: Matthew Critchlow Sections: Thursday 11 – 1 and 1 -3 Office Hours: TBA Email: mcritch2@uiuc.edu Office: 0212 Siebel. Outine. Admin. stuff Lecture review Code review.

elsa
Download Presentation

An Introduction to C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS225: Data Structures and Software PrinciplesLectures 2, 3, and 4 An Introduction to C++

  2. TA Info • Name: Matthew Critchlow • Sections: Thursday 11 – 1 and 1 -3 • Office Hours: TBA • Email: mcritch2@uiuc.edu • Office: 0212 Siebel

  3. Outine • Admin. stuff • Lecture review • Code review

  4. Admin. stuff • “Lab” format • Goals of lab • Prepare for MPs • Prepare for Exam • Course Website • Newsgroups class.cs225 AND class.cs225.announce • http://tsg.cs.uiuc.edu/services/newsgroups/ • Use Sun machines • http://www.ews.uiuc.edu/ • Source code library: ~cs225/src/library/01-c++basic/

  5. Motivation • Why C++ • Mainly superset of C • Efficient & powerful • Object Oriented (OOP) • Why Data Structures • Choosing the appropriate data structures is key to efficiency of your code • Know data structures, don’t re-invent the wheel • Some future courses rely on this material

  6. Java vs. C++ Similarities • Language primitives • Control statements • Expressions, operators and statement syntax Differences • I/O facilities • Libraries • Inheritance (differs appreciably) • Global variables • Pointers • Explicit de-allocation of memory • Templates

  7. Declarations • Declaring variables of built-in types • int n; (C++ and Java) • Declaring variables of user-defined types • Coord c1; (C++ only) In Java, instance is not created unless new is used. • Allocating objects of built-in types • int* nPtr = new int; (C++ only) • Allocating objects of user-defined types • Coord* cPtr = new Coord(); (C++) • Coord mycoord = new Coord(); (Java)

  8. Classes in C++ Similar to those in Java except that • there is a semi-colon at the end of the declaration • there is grouping of variable access permissions • classes need not be themselves named public,private,etc. • there is file organization (convention) • header files need not have the same name as the class

  9. File Organization • main() function usually in a separate file (driver) • .h file • class declaration and function headers • #ifndef … #endif * • Remember preprocessor directives • .cpp or .cc or .C file • #include • class implementation • remember scope resolution operator: class::classMethod()

  10. Preprocessor directives • #define #define PI 3.1418 #define AREA(radius) PI*(radius)*(radius) • #include • library files (#include <file1>) • local files (#include “file2.h”) Need to give the full path • No semi-colon at the end of a directive • Example: When we want to ensure that COORD_H is defined only once #ifndef COORD_H #define COORD_H // class definition #endif

  11. I/O in C++ • You have to include the standard library #include <iostream> • cin standard input stream cin >> num; • cout standard output stream cout << “The value of num is :“<<num<<endl;

  12. Pointers in C++ • Concept of pointers absent in Java • A pointer is a variable which holds a memory address. Usually represented in hex format. & -- address of * -- dereference (what is the object being pointed to?) • & and * are inverses of each other: • (*(&num)) = num • (&(*numPtr)) = numPtr

  13. Pointer Example # include <iostream> int main() { int* ptr; // Pointer to an integer int a; // Just a normal integer a = 5; // Store the value 5 in a ptr = &a; // Store’s a’s address in ptr cout << “&a = “ << &a << endl; cout << “ptr = “ << ptr << endl << endl; cout << “a = “ << a << endl; cout << “*ptr = “ << *ptr << endl << endl; cout << “&ptr =“ << &ptr << endl << endl; return 0; }

  14. Pointers and Classes Use of -> operator (‘-’ followed by a ‘>’) #include “date.h” void main() { Date* dPtr = new Date(); dPtr->SetDate(9, 1, 2000); //same as (*dPtr).SetDate(9,1,2000); dPtr->Print(); //same as (*dPtr).Print(); delete dPtr; }

  15. Memory Operations • Heap and Stack Memory • The new operator • The delete operator • Every new should match a delete and every new[] should match a delete[] • Every delete should be followed by a NULL assignment • Pitfalls • Dangling pointers • Memory leaks

  16. Dangling Pointer – How and Why int *myPointer = new int(); *myPointer = 10; cout << "The content of myPointer is " << *myPointer << endl;delete myPointer; /* Note that even after delete, myPointer points to a valid address. But that address/memory location might be assigned to some other process (for example – another user program.) It is unsafe to leave our pointer “dangling” in this way, since it could be accidentaly used to change the value of that memory location in use by another process*/ cout << "The value of myPointer is " << *myPointer << endl; • Solution : use NULL

  17. Memory Leak – Example If a function or piece of code allocates memory for its use and does not deallocate before exiting, that memory is never going to be reallocated, because the operating system/ compiler never comes to know that the memory is no longer in use. (Note: In Java, there is automatic garbage collection, which takes care of this.) Example: void f() { int a[] = new int[1000]; for (int I=0; I<100000; I++) { /* do_something */ } delete a[]; }

  18. About the makefile • A "makefile" is typically a list of rules and dependencies with corresponding commands to create "targets" using the rules and dependencies specified • Typing make at the command line will build and run your project • make -f MyMakefile selects the makefile of choice

  19. How to… • The makefile is composed of: target: dependencies [tab] system command • Example: project.exe : main.o io.o • A macro definition line is a makefile line with a macro name, an equals sign “=”, and a macro value. In the makefile, expressions of the form $(name) or ${name} are replaced with value

  20. Code Review • Important aspect from the Software Engineering perspective • More efficient than testing • Problems and not symptoms are detected by reviews unlike in testing • Testing can only prove the presence of errors and not their absence

  21. Common Coding Errors? What will be of immediate use for us? CHECKLIST • “;” after class • arrays indexed correctly • loops iterate correct # of times? • include all needed files • variable spelling • delete dynamic data • Scope resolution • #ifndef/#define/#endif for every header file

More Related