c Plus Plus Review

download c Plus Plus Review

of 107

description

C++ REVIEW

Transcript of c Plus Plus Review

  • CSCE 3110Data Structures &Algorithms

    C++ warm-up

  • History and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructor, copy constructor, and assignment operatorConstTemplate

  • History of C++1972: C language developed at Bell LabsDennis Ritchie wrote C for Unix OSNeeded C for work with Unixlate 70s: C becomes popular for OS development by many vendorsMany variants of the language developedANSI standard C in 1987-89

  • History of C++ (continued)early 80s: Bjarne Stroustrup adds OO features to C creating C++90s: continued evolution of the language and its applicationspreferred language for OS and low level programmingpopular language for application developmentlow level control and high level power

  • Conceptually what is C++Alternatives:is it C, with lots more options and features?is it an OO programming language with C as its core?is it a development environment?On most systems it is a development environment, language, and library, used for both procedural and object oriented programming, that can be customized and extended as desired

  • Versions of C++ANSI C++Microsoft C++ (MS Visual C++ 6.0)Other vendors: Borland, Symantec, Turbo, Many older versions (almost annual) including different version of C tooMany vendor specific versionsMany platform specific versionsFor this class: Unix / Linux based versionsg++

  • Characteristics of C++ as a Computer LanguageProceduralObject OrientedExtensible...

  • Other OO LanguagesSmalltalkpure OO language developed at PARCJava built on C/C++objects and data typesEifel and others

  • What you can do with C++Apps (standalone, Web apps, components)Active desktop (Dynamic HTML, incl Web)Create graphical appsData access (e-mail, files, ODBC)Integrate components w/ other languages

  • Disadvantages of C++Tends to be one of the less portable languagesComplicated? 40 operators, intricate precedence, pointers, etc.can control everythingmany exceptions and special casestremendous libraries both standard, vendor specific, and available for purchase, but all are intricateAspects above can result in high maintenance costs

  • Advantages of C++Available on most machinesCan get good performanceCan get small sizeCan manage memory effectivelyCan control everythingGood supply of programmersSuitable for almost any type of program (from systems programs to applications)

  • History and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • Primitive Typesbooltrue or false (only C++)char8/16-bit short 16-bit signed integerint32-bit signed integerunsigned32-bit unsigned integerlong32 / 64-bit signed integerfloat32-bit floating pointdouble64-bit floating point

  • Operators and Precedence[] . to access arrays elements / to access object methods and fieldsexpr++ expr-- ++expr --expr ! new (type)expr* / %+ - > (integers only)< > >=
  • Operators and Precedence^|&& (booleans only)|| (booleans only)?:= += -= *= .

    C++ allows operator overloading

  • Precedence ExampleWhat is: 5 + 21 / 4 % 3= 5 + (21 / 4) % 3= 5 + ( 5 % 3)= 5 + 2= 7

  • Explicit Casting(type) expressionPossible among all integer and float typesPossible among some class references

    E.g. int i = (int) ( (double)5 / (double)3 )

  • Implicit CastingApplied automatically provided there is no loss of precisionfloat doubleint doubleExampleint iresult, i=3; double dresult, d=3.2;dresult = i/d => implicit casting dresult=0.9375iresult = i/d => error! Why? Loss in precision, needs explicit casting

  • Control Flowif (boolean)statement;else if(boolean)statement2;else statement3;

    Booleans only, not integers!if (i > 0) correctif (i = 2) correct / incorrect ?

  • Switch / caseswitch (controlVar){ case 'a' :statement-1break; case 'b' :statement-2break; default :statement-3break; }Do not forget the break command to avoid surprise result!

  • Loopswhile() statement;

    do statement;while()

    for(init-expr; ; incr-expr) statement;

  • Loop RefresherWhich loops must execute their statements at least once?Which loops can choose to never execute their statements?Which value of the boolean indicates to do the statements again?

  • Some Conventions for Variable Names Use letters and numbersDo not use special characters including spaces, dots, underlines, pound signs, etc.The first letter will be lower caseUse variable names that are meaningful (except for occasional counters that we might call i, j, x, etc.)You can concatenate words, and capitalize each after the first, e.g., bankBal, thisAcctNum, totAmtIf you abbreviate, be consistent. For example do not use both bankBal and totalBalance as variable names.

  • Some Conventions for Struct and Class NamesIn creating names of structs and classes, apply the same rules as for variable names, except the first character will be upper caseExample:an object's name: myCarthe struct or class name: CarAnother Example: aPerson and Person

  • OverviewHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • Passing ParametersC++ allows for three different ways of passing parameters:Pass by valueE.g. foo (int n)Appropriate for small objects (usually primitive types) that should not be altered by the function callPass by constant referenceE.g. foo(const T& myT)Appropriate for large objects that should not be altered by the function callPass by referenceE.g. foo(bool & errFlag)Appropriate for small objects that can be altered by the function callArray types are always passed by reference

  • Passing by valuevoid square(int i){ i = i*i;}int main(){ int i = 5; square(i); cout
  • Passing by referencevoid square(int& i){ i = i*i;}int main(){ int i = 5; square(i); cout
  • Passing by constant referencevoid square(const int& i){ i = i*i;}int main(){ int i = 5; square(i); cout
  • Passing by constant referenceint square(const int& i){ return i*i;}int main(){ int i = 5; cout
  • What is a reference?An alias another name for an object. int x = 5; int &y = x; // y is a // reference to x y = 10;

    What happened to x?What happened to y? y is x.

  • Why are they useful?When passing argument of large size (class type), can save spaceSometimes need to change a value of an argumentCan be used to return more than one value (pass multiple parameters by reference)

  • How are references different from Pointers?

    Reference Pointer int a = 10; int b = 20; int &c = a; c = b;

    What is the value of a? int a = 10; int b = 20; int *c = &a; c = &b;

  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • ClassesProvide a mechanism for defining classes of objects.We can define the class of all computers to have certain characteristics.An instance of a computer is your home PC.Classes contain member variables and member functions.

  • Classes in C++:Why Create Classes / Objects?Keeps all related info (i.e., data) togetherRefer to all the related info by one nameProtect the informationHide methods that use or change the infoKeep methods together with their related info

  • Example of Benefits of Creating an ObjectKeeps all related info (i.e., data) togetherPerson thisPerson;Person thisPerson = new Person ("Bill", "Clinton", 52);Refer to all the related info by one namethisPersonProtect the informationlastName = "Dole"; //normally data members are private, and member functions are public

  • Classes and ObjectsMammalsHumansTigersHankPeggyTonyclassclassclassinheritsinheritsinstance-ofinstance-of

  • Example of a Simple Classclass Change{private:int quarters;int dimes;public:int getQuarters() {return quarters;} int getDimes() {return dimes;}void setQuarters(int aQuarters) {quarters = aQuarters;}...void printChange(){cout
  • More Class Exampleclass human{// this data is private to instances of the classint height;char name[];int weight;

    public:void setHeight(int heightValue);int getHeight();};

  • Function Definitionsvoid human::setHeight(int heightValue){if (heightValue > 0)height = heightValue;elseheight = 0;}

    int human::getHeight(){return(height);}

  • Example// first we define the variables.int height = 72;int result = 0;human hank;

    //set our humans heighthank.setHeight(height);

    //get his heightresult = hank.getHeight();

    cout

  • Instantiating an ObjectThe class definition does not create any objectsInstantiating and constructing are equivalent words for building a new object based on the model (i.e., template) of the classInstantiating is done just like declaring a variable of a built in data typeInstantiating is done by a constructor (sometimes called a constructor method)If the "class provider" does not provide a constructor, then the C++ compiler provides a default one automaticallyThe default constructor does not provide values to the data members (i.e. the instance variables)

  • Instantiating an Object (more)When the object is instantiated, memory is allocatedExample of instantiation (implicit call of constructor)Car myCar;Elephant oneElephant, twoElephant;No initialization takes placeEach object has its own memory allocationoneElephant and twoElephant are separate objects in different locations in memoryEach is addressed individually by name or locationEach data member is addressed individually using the object name and the data member name, for example: oneElephant.agetwoElephant.name

  • Referencing an ObjectEach object has a name (or a location) which is assigned when the object is instantiatedprivate data members are accessible only within the classsince most data members are private, that means that these data items are accessed generally by means of member functionsmyElephant.age = 72; //won't work, assuming age //is declared as privatemyElephant.setAge(72); // will work

  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • InheritanceThe power of object-oriented languagesEnables reuse of fields/methodsAll parent fields included in child instantiationProtected and public fields and methods directly accessible to childParent methods may be overriddenNew fields and methods may be added to the childMultiple inheritance

  • Inheritance (contd)class classname: public parentname {private: .;public:.;//access to parent methods through// parentname::methodname }

  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • Header fileFor complex classes, the member functions are declared in a header file and the member functions are implemented in a separate file.This allows people to look at the class definitions, and their member functions separatelyThe header file needs to be included in your program when you use the classes defined in the head file

  • #include Segment.H

    #include

    #includeInsert header file at this point.Use library header.

  • Header Guards#ifndef __SEGMENT_HEADER__#define __SEGMENT_HEADER__

    // contents of Segment.H//...

    #endif

    To ensure it is safe to include a file more than once.

  • Header Guards#ifndef __SEGMENT_HEADER__#define __SEGMENT_HEADER__

    // contents of segment.H//...

    #endif

    To ensure it is safe to include a file more than once.If this variable is not definedDefine it.End of guarded area.

  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • Output#includeTell compiler that we are doing I/OcoutObject to which we can send data.
  • Formatting Outputios::leftleft justify the outputios::rightright justify the outputios::scientificuse scientific notation for numbersios::hexprint numbers in hexadecimal baseios::decprint numbers in decimal baseios::uppercaseprint all characters in upper casecout.setf(long flag)cout.unsetf(long flag)Set different formatting parameters for next output.Disable these formatting parameters.

  • Example#includemain(){cout.width(10); //sets width to 10cout
  • Input#include Tell the linker we are doing basic I/OcinThe input object. It retrieves input from the keyboard>>The extractor operator.

  • Example#include

    main (){int userInput;cout > userInput;cout

  • I/O From a FileI/O from a file is done in a similar way.#include #include main(){int inputNumber;

    ofstream myOutputFile(outfile);ifstream myInputFile(infile);myOutputFile inputNumber;myOutputFile.close();myInputFile.close();}

  • #include #include #include #include using namespace std;

    int main(int argc, char *argv[]){ // Check input if(argc

  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • What is a pointer?int x = 10;int *p;

    p = &x;

    p gets the address of x in memory.px10

  • What is a pointer?int x = 10;int *p;

    p = &x;

    *p = 20;

    *p is the value at the address p.px20

  • What is a pointer?int x = 10;int *p;

    p = &x;

    *p = 20;

    Declares a pointer to an integer& is address operator gets address of x * dereference operator gets value at p

  • A Pointer Exampleint main(){int i, j;int *pi, *pj;

    i = 5;j = i;pi = &i;pj = pi;*pj = 4;cout

  • Allocating memory using newPoint *p = new Point(5, 5);Point is a class already definednew can be thought of a function with slightly strange syntaxnew allocates space to hold the object.new calls the objects constructor.new returns a pointer to that object.

  • Memory Allocation Examplesnew returns a pointer to the dynamically created object.

    #include Cow.h #include using namespace std;

    int main(){int *i = new int(12);Cow *c = new Cow;...delete i;delete c;

    return 0;}

  • ProblemsDangling pointersPointers to memory that has already been deallocatedsegmentation fault (core dump)... or worse....Memory leakLoosing pointers to dynamically allocated memorySubstantial problem in many commercial productsSee Windows 98C++ HAS NO GARBAGE COLLECTION!

  • Dangling pointer examplesint main(){int *myNum = new int(12);int *myOtherNum = myNum;

    delete myNum;cout

  • Memory Leak Examplesint main(){int *myNum = new int(12);myNum = new int(10);// Oops...

    delete myNum;return 0;}

    int evilFunction(){int *i = new int(9);return *i;}

    int main(){int num = evilFunction();

    // Im loosing my memory!!

    return 0;}

  • Deallocating memory using delete // allocate memoryPoint *p = new Point(5, 5);

    ...// free the memorydelete p;

    For every call to new, there must beexactly one call to delete.

  • Using new with arraysint x = 10;int* nums1 = new int[10]; // okint* nums2 = new int[x]; // ok

    Initializes an array of 10 integers on the heap.C++ equivalent of Cint* nums = (int*)malloc(x * sizeof(int));

  • Using new with multidimensional arraysint x = 3, y = 4;int* nums3 = new int[x][4][5];// okint* nums4 = new int[x][y][5];// BAD!

    Initializes a multidimensional arrayOnly the first dimension can be a variable. The rest must be constants.Use single dimension arrays to fake multidimensional ones

  • Using delete on arrays// allocate memoryint* nums1 = new int[10];int* nums3 = new int[x][4][5];

    ...// free the memorydelete[] nums1;delete[] nums3;

    Have to use delete[].

  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • Class DestructorsIf a class dynamically allocates memory, we need a way to deallocate it when its destroyed.Distructors called upon distruction of an objectclass MyClass{public:MyClass(){// Constructor}~MyClass(){ // Destructor}...};

  • Destructors

    delete calls the objects destructor.delete frees space occupied by the object.

    A destructor cleans up after the object.Releases resources such as memory.

  • Destructors an Exampleclass Segment{public: Segment(); virtual ~Segment();private: Point *m_p0, *m_p1;};

  • Destructors an ExampleSegment::Segment(){ m_p0 = new Point(0, 0); m_p1 = new Point(1, 1);}Segment::~Segment(){ delete m_p0; delete m_p1;}

  • Copy Constructor and Assignment OperatorCopy Constructor: class Rooster{public:...Rooster(const Rooster &rhs){// Do your deep copy}...};...// UsageRooster r(12);Rooster s(r);Assignment Operator:class Rooster{public:...Rooster& operator=(const Rooster &rhs){ // Copy stuff}...};...// UsageRooster r(12), s(10);r = s;

  • Canonical FormAll classes should have each of the following:Default constructorCopy constructorAssignment operatorDestructor// Canonical Cowclass Cow{public:Cow(){...}Cow(const Cow &rhs){...}Cow& operator=(const Cow &c){...}~Cow(){...}

    ...};

  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructore, copy constructor, and assignment operatorConstTemplate

  • Introducing: constvoid Math::printSquare(const int& i){ i = i*i; cout
  • How does const work here?void Math::printSquares(const int& j, int& k){ k = k*k; // Does this compile? cout
  • Returning const references is OKclass Point{point: const double& getX() const; const double& getY() const; void move(double dx, double dy);private: double m_x, m_y;}const double& Point::getX() const{ return m_x;}Constant function, also called accessorReturn a reference to a constant double

  • NamespacesNamespaces are kind of like packages in JavaReduces naming conflictsMost standards C++ routines and classes and under the std namespace

  • using namespace#include ...std::string question = How do I prevent RSI?;std::cout
  • OutlineHistory and overviewBasic featuresParameter passingClassesInheritance and virtualHeader fileIOMemory ManagementBig three: destructor, copy constructor, and assignment operatorConstTemplate

  • TemplateWhat exactly are templates for, and why learn them? Limited Generic Programming (polymorphism)Some functions have the same semantic meaning for some (if not all) data types. For instance, a function print() should display a sensible representation of anything passed in. Ideally, it shouldnt need to be rewritten for each possible type.

    Less repetitive codeCode that only differs in the data type it handles does not have to berewritten for each and every data type you want to handle. Its easier toread and maintain since one piece of code is used for everything

  • Example: a swap functionNaive method write an overloaded function for each typevoid swap(int &a, int &b) { int c = a; a = b; b = c;}void swap(T &a, T &b) { T c = a; a = b; b = c;}Swap for integersSwap for an arbitrary type Ttemplate void swap(T &a, T &b) { T c = a; a = b; b = c;}This function can be used with any type that supports assignment and can be passed in as a non-const reference. Problem: Oftentimes, it is nice to be able to swap the values of two variables. This functions behavior is similar for all data types. Templated functions let you do that in most cases without any syntax changes.Template method write one templated function

  • Template Syntax: swap dissectedtemplate void swap(T &a, T &b) { T c = a; a = b; b = c;}The template line states that everything in the following declaration or definition is under the subject of the template. (In this case, the definition is the function swap)In here goes a list of placeholders variables. In almost all cases, they will be specified with either the typename or class keywords. These two keywords are equivalent. Placeholder variables have one value within each template declaration. Think of them as being replaced by whatever type you specify the template to be.

  • Template Syntax: Using ittemplate void swap(T &a, T &b) { T c = a; a = b; b = c;}Example: double d1 = 4.5, d2 = 6.7;swap(d1, d2);Syntax

  • Class Templates: ExampleExample: A templated, dynamic, 2 dimensional array (Matrix)*#ifndef MATRIX_H#define MATRIX_H

    template class Matrix {public: Matrix(int rows, int cols); Matrix(const Matrix &other); virtual ~Matrix();

    Matrix& operator=(const Matrix &rhs); T* operator[](int i); int getRows() const; int getCols() const;

    protected: void copy(const Matrix &other);

    private: Matrix(); int m_rows; int m_cols; T *m_linArray;};

    #endif /* MATRIX_H */File: Matrix.hNotice the only addition to the class definition is the line: template Within the the definition block, the placeholder has can be used as a data type. When the template is specialized, it takes on the value of the specialization.

  • template T* Matrix::operator[](int i) { return m_linArray + (i*m_cols);}

    template void Matrix::copy(const Matrix &other) { m_rows = other.m_rows; m_cols = other.m_cols;

    int size = m_rows * m_cols; m_linArray = new T[size]; for( int i=0; i < size; i++ ) { m_linArray[i] =other.m_linArray[i]; }}

    template int Matrix::getRows() const { return m_rows;}

    template int Matrix::getCols() const { return m_cols;}

    Class Templates: Example contd#include "Matrix.h"

    template Matrix::Matrix(){}

    template Matrix::Matrix(int rows, int cols) { m_rows = rows; m_cols = cols; m_linArray = new T[m_rows * m_cols];}

    template Matrix::Matrix(const Matrix &other) { copy(other);}

    template Matrix::~Matrix() { delete[] m_linArray;}

    template Matrix& Matrix::operator=(const Matrix &other) { if( this != &other ) { delete[] m_linArray; copy(other); }

    return *this;}

    File: Matrix.cc

  • template Matrix& Matrix::operator=(const Matrix &other) { if( this != &other ) { this->~Matrix(); copy(other); }

    return *this;}Class Templates: Member Functions DissectedAgain, a templated class name by itself has no meaning (eg. Matrix by itself means nothing). It only gets meaning through specialization, explicit or implicit. Thus, when referring to an instance of a templated class (a specific specialization), the class name must be explicitly specialized.Here, the template has been implicitly specialized by its context. It is within the specialization region of the class scope. Thus it does not need the template arguments. For a class definition, the specialization region is the class block.specialization region of Matrix::Notice that the specialization region does not include the return type. Thus the return type needs explicit specializationThis may be obvious, but remember that though constructors and destructors have the same name as a the class template, they are functions and do not need to be specialized.

  • Class Templates: usageTemplated classes must be explicitly specialized. Thus, to create a 2 dimensional Matrix of doubles using the last example, the syntax would be: Matrix m(3,3);Syntax

  • STLAllows you to easily store anything without writing a container yourselfWill give you the most hideous compile errors ever if you use them incorrectly.

  • STL exampleusing namespace std;typedef list intlist;typedef intlist::iterator intlistIter;

    intlist v;v.push_back(4);intlistIter a;for(a = v.begin(); a != v.end(); ++a){int c = (*a);}

  • Now compile and run a simple c++ program

  • Compilation ModelPreprocessorResolves all preprocessor directives#include, #define macros, #ifdef, etc.CompilerConverts text into object filesMay have unresolved interobject referencesLinkerResolves all interobject references (or gives you a linker error)Creates the binary executableLoaderLoads the program into RAM and runs the main() function

  • CompilationObject filesExecutableExternal Libraries, libc.so, libcs123.so

  • HelloWorld.cpp#include // For coutusing namespace std;

    int main(){ cout

  • Compiling with g++ix$ lshello.cppix$ ls hello.cppix$ g++ hello.cpp ix$ lsa.out* hello.cppix$ g++ -c hello.cppix$ lsa.out* hello.cpp hello.oix$ g++ -o hello hello.cpp ix$ ./hello Hello World!

  • makefile

    > make

  • Simple makefileAll: hellohello: hello.o g++ -o hello hello.ohello.o: hello.cpp g++ -c hello.cpp

  • ix$ lshello.cpp makefileix$ makeg++ -c hello.cppg++ -o hello hello.oix$ lshello* hello.cpp hello.o makefileix$ ./hello Hello World!

    *Whats the output? 5, i was copied.*Whats the output now? 25, i was changed.*Whats the output now? 25, i was changed.*Whats the output now? 25, i was changed.*What happened to x? it is now 10What happened to y? silly question? y is x. eg: professor Black is Michael.*avoid copying big objects.makes syntax work out better.wont slice*Cant have a reference to nothing.cant reseat/reassign reference.

    ****Much like a java reference (but more explicit)A pointer is a variable that contains the address of an object in memory.

    Line 1 x is an integer, give it value 10Line 3 p gets the address of x***Just like java.But you have to think about pointers.*No garbage collection. **Ask about virtual. (whats wrong here)**If you can, you should make this guarantee.const extends the type to allow you to specify which data is read-onlyWhy is this good? --- gets the compiler to help make sure certain values dont change.

    *Just because theres a reference to something const, doesnt mean that it wont change.it simply wont be changed through that reference.*note const is part of the return type, and so is in both declaration and definition*Does this go here???**Discuss make/makefilesErrors can occure in any of the three above mentioned units