Chapter 1 C++ Basics Review by hcj

VIEWS: 0 PAGES: 11

									                      Chapter 1
             C++ Basics Review
Sections 1.4 and 1.5 and additional material from
                  these slides


            See examples under Lec3



                                                    1
            Interface Vs. Implementation
•   Interface typically defined in     Interface
    .h files
     – #included in .cpp file


•   Preprocessor commands
     – Guards against multiple
       inclusion of .h files




                                               2
        Interface Vs. Implementation (contd.)
                                       Implementation
•   Scoping operator
    – To identify the class
      corresponding to each
      function


•   Remember
    – Function signatures must
      match in both interface and
      implementation
    – Default parameters are
      specified only in the
      interface




                                                 3
           Problems without Information Hiding
•   Change in implementation affects the user of a class
     – What will happen if the name ‘OtherData’ is changed to ‘Info’?
•   Debugging is difficult
     – Where will you place breakpoints to figure out when results went
       wrong?
class NHRecord
{                                       #include <iostream>
 public:                                #include "nhrecord.h"
  int SocialSecurityNumber;             using namespace std;
  int OtherData;
  NHRecord(int SSN, int Data);          int main()
};                                      {
                                          NHRecord nhr(12345678, 1);
#include "nhrecord.h"
                                          cout << "InitialInfo " << nhr.OtherData <<
NHRecord::NHRecord(int SSN, int Data)           '\n';
{                                         nhr.OtherData = 2;
  SocialSecurityNumber = SSN;             cout << "UpdatedInfo " << nhr.OtherData
  OtherData = Data;                             << '\n';
}                                       }
                                                                                   4
             Information Hiding and Accessors
•   Get/Set with private data provide access while overcoming the
    limitations mentioned above
     – The user is not affected if the name ‘OtherData’ is changed to ‘Info’.
     – Breakpoints on the accessors help with debugging

class HRecord
{
  int SocialSecurityNumber;                  int main()
  int OtherData;                             {
public:                                        HRecord hr(12345678, 1);
  HRecord(int SSN, int Data);                  cout << "InitialInfo " << hr.GetData() <<
  void SetData￿￿Data);
               (int                                  '\n';
  int GetData();                               hr.SetData(2);
};                                             cout << "UpdatedInfo " << hr.GetData() <<
                                                     '\n';

void HRecord::SetData(int Data)
                                                 return 0;
{OtherData = Data;}
                                             }

int HRecord::GetData()
{ return OtherData;}                                                                 5
                 vector and string in C++ STL
•   Replace built-in C++ arrays and
    strings, respectively
     – Built-in arrays/string do not act as
       proper C++ objects
•   Standard vector class
     – Gives a size() function
     – Can be assigned using =
•   Standard string class
     – Compared with ==, <, etc.
     – Can be assigned using =
     – Gives length() function
•   Avoid C++ built-in arrays and
    strings
     – Instead, use vector and string
       classes
     – Exception: code optimized for
       speed


                                                6
                      Common Errors with Pointers
•      Many of these errors may not be detected when you run your program, which
       make them particularly dangerous
int *i;
*i = 1; // Wrong. No memory has been assigned to i.

    i = new int;
    *i = 1; // OK

    delete i;
    if(i != 0)
      cout << *i << "\n\n"; // Wrong. i is still non-zero, but memory it points to has been
            deallocated.

    i = 0; // It is safer to assign 0 after deallocation.
    cout << *i; // Wrong. Dereferencing the null pointer.
    if(i != 0)
      cout << *i; // Not executed

    i = new int[10];
    delete [] i; // Do not use delete i;
                                                                                              7
                                 More Errors with Pointers

void swap(int t1, int t2)                   int i1=1, i2=2;
{                                           swap(i1, i2);
  int t = t1; t1 = t2; t2 = t;              cout << i1 << " " << i2 << "\n\n";
}                                           swap(&i1, &i2);
                                            cout << i1 << " " << i2 << "\n\n";
void swap(int *t1, int *t2)                 i1 = 1; i2 = 2;
{                                           swapr(i1, i2);
  int t = *t1; *t1 = *t2; *t2 = t;          cout << i1 << " " << i2 << "\n\n";
}

void swapr(int &t1, int &t2)
{
  int t = t1; t1 = t2; t2 = t;
}




                                                                                 8
         Don’t Return Pointers to Temporary Locations


int *BadReturn() // Dangerous error.   i = BadReturn(); // Call to a dangerous function
{                                      cout << *i << "\n\n"; // Wrong. Dereferencing an
  int Answer = 2;                            invalid pointer.


    return &Answer;                    i = OKReturn();
}                                      cout << *i << "\n\n";
                                       delete i;
int *OKReturn()                        i = 0;
{
  int *Answer;

    Answer = new int;
    *Answer = 2;

    return Answer;
}

                                                                                      9
                                      Shallow Copy
    •   Shallow copy can have unexpected consequences with
        dynamically allocated data
class Node                                   Node n1("Gates", 50), *n2 = new Node(n1);
{                                            cout << n1.name << " " << n1.age << " " << n2->name
public:                                             << " " << n2->age << "\n";
  char *name; int age;
  Node(char *n, int a)                       strcpy(n2->name, "Bill"); n2->age = 55;
  {                                          cout << n1.name << " " << n1.age << " " << n2->name
    name = new char[strlen(n) + 1];                 << " " << n2->age << "\n";
    strcpy(name, n); age = a;
  }                                          delete n2; // The destructor frees name, which is
                                                    common to n1 and to n2

 ~Node()                                                           Output
 {                                                      Gates 50 Gates 50
   cout << "Deleting " << (void *) name <<              Bill 50 Bill 55
        "\n";                                           Deleting 0x191d0010
   delete [] name;                                      Deleting 0x191d0010
 }
};
                                                                                            10
                                    Deep Copy
Node(const Node &n)
                                         Node n1("Gates", 50), *n2 = new Node(n1);
{
                                         cout << n1.name << " " << n1.age << " " << n2->name
  cout << "In copy constructor\n";
                                                << " " << n2->age << "\n";
  name = new char[strlen(n.name)+1];
  strcpy(name, n.name);
                                         strcpy(n2->name, "Bill"); n2->age = 55;
  age = n.age;
                                         cout << n1.name << " " << n1.age << " " << n2->name
}                                               << " " << n2->age << "\n";

Node &operator=(const Node &n){          delete n2; // The destructor frees name, which is
  cout << "In assignment\n";                    common to n1 and to n2
  if(this != &n)
  {
                                                              Output
    if(name)
                                                     In copy constructor
      delete [] name;
                                                     Gates 50 Gates 50
    name = new char[strlen(n.name)+1];
                                                     Gates 50 Bill 55
      strcpy(name, n.name);
                                                     Deleting 0x4f87050
      age = n.age;
                                                     Deleting 0x4f87010
  }
  return *this;
}
                                                                                       11

								
To top